All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyStaticTokens.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef TF_PYSTATICTOKENS_H
26 #define TF_PYSTATICTOKENS_H
27 
29 
30 #include "pxr/pxr.h"
31 
32 #include <locale>
33 
34 #include "pxr/base/tf/staticTokens.h"
35 
36 #include <boost/python/class.hpp>
37 #include <boost/python/scope.hpp>
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 // TODO: Should wrap token arrays to Python.
42 
49 #define TF_PY_WRAP_PUBLIC_TOKENS(name, key, seq) \
50  boost::python::class_<_TF_TOKENS_STRUCT_NAME(key), boost::noncopyable>( \
51  name, boost::python::no_init) \
52  _TF_PY_TOKENS_WRAP_SEQ(key, _TF_PY_TOKENS_EXPAND(seq))
53 
60 #define TF_PY_WRAP_PUBLIC_TOKENS_IN_CURRENT_SCOPE(key, seq) \
61  _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, _TF_PY_TOKENS_EXPAND(seq))
62 
63 // Helper to return a static token as a string. We wrap tokens as Python
64 // strings and for some reason simply wrapping the token using def_readonly
65 // bypasses to-Python conversion, leading to the error that there's no
66 // Python type for the C++ TfToken type. So we wrap this functor instead.
67 class _TfPyWrapStaticToken {
68 public:
69  _TfPyWrapStaticToken(const TfToken* token) : _token(token) { }
70 
71  std::string operator()() const
72  {
73  return _token->GetString();
74  }
75 
76 private:
77  const TfToken* _token;
78 };
79 
80 // Private macros to add a single data member.
81 #define _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, name) \
82  boost::python::scope().attr( \
83  BOOST_PP_STRINGIZE(name)) = key->name.GetString();
84 
85 #define _TF_PY_TOKENS_WRAP_MEMBER(r, key, name) \
86  .add_static_property(BOOST_PP_STRINGIZE(name), \
87  boost::python::make_function(_TfPyWrapStaticToken((&key->name)), \
88  boost::python::return_value_policy< \
89  boost::python::return_by_value>(), \
90  boost::mpl::vector1<std::string>()))
91 
92 #define _TF_PY_TOKENS_EXPAND(seq) \
93  BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
94  _TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)
95 
96 // Private macros to wrap a single element in a sequence.
97 #define _TF_PY_TOKENS_WRAP_ELEMENT(r, key, elem) \
98  _TF_PY_TOKENS_WRAP_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
99 
100 #define _TF_PY_TOKENS_WRAP_ATTR_ELEMENT(r, key, elem) \
101  _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
102 
103 #define _TF_PY_TOKEN_GET_ELEM(elem) \
104  BOOST_PP_IIF(TF_PP_IS_TUPLE(elem), \
105  BOOST_PP_TUPLE_ELEM(2, 0, elem), elem)
106 
107 // Private macros to wrap a sequence.
108 #define _TF_PY_TOKENS_WRAP_SEQ(key, seq) \
109  BOOST_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ELEMENT, key, seq)
110 
111 #define _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq) \
112  BOOST_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ATTR_ELEMENT, key, seq)
113 
114 PXR_NAMESPACE_CLOSE_SCOPE
115 
116 #endif // TF_PYSTATICTOKENS_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89