HDK
 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 PXR_BASE_TF_PY_STATIC_TOKENS_H
26 #define PXR_BASE_TF_PY_STATIC_TOKENS_H
27 
28 /// \file tf/pyStaticTokens.h
29 
30 #include "pxr/pxr.h"
31 
32 #include <locale>
33 
36 
37 #include <hboost/python/class.hpp>
38 #include <hboost/python/scope.hpp>
39 
41 
42 // TODO: Should wrap token arrays to Python.
43 
44 /// Macro to wrap static tokens defined with \c TF_DEFINE_PUBLIC_TOKENS to
45 /// Python. It creates a class of name \p name in the current scope
46 /// containing just the tokens in \p seq in the static tokens named by \p key.
47 /// Arrays are not wrapped but their components are.
48 ///
49 /// \hideinitializer
50 #define TF_PY_WRAP_PUBLIC_TOKENS(name, key, seq) \
51  hboost::python::class_<_TF_TOKENS_STRUCT_NAME(key), hboost::noncopyable>( \
52  name, hboost::python::no_init) \
53  _TF_PY_TOKENS_WRAP_SEQ(key, _TF_PY_TOKENS_EXPAND(seq))
54 
55 /// Macro to wrap static tokens defined with \c TF_DEFINE_PUBLIC_TOKENS to
56 /// Python. This wraps tokens in \p seq in the static tokens named by \p key
57 /// as attributes on the current hboost python scope. Arrays are not wrapped
58 /// but their components are.
59 ///
60 /// \hideinitializer
61 #define TF_PY_WRAP_PUBLIC_TOKENS_IN_CURRENT_SCOPE(key, seq) \
62  _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, _TF_PY_TOKENS_EXPAND(seq))
63 
64 // Helper to return a static token as a string. We wrap tokens as Python
65 // strings and for some reason simply wrapping the token using def_readonly
66 // bypasses to-Python conversion, leading to the error that there's no
67 // Python type for the C++ TfToken type. So we wrap this functor instead.
69 public:
70  _TfPyWrapStaticToken(const TfToken* token) : _token(token) { }
71 
73  {
74  return _token->GetString();
75  }
76 
77 private:
78  const TfToken* _token;
79 };
80 
81 // Private macros to add a single data member.
82 #define _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, name) \
83  hboost::python::scope().attr( \
84  TF_PP_STRINGIZE(name)) = key->name.GetString();
85 
86 #define _TF_PY_TOKENS_WRAP_MEMBER(r, key, name) \
87  .add_static_property(TF_PP_STRINGIZE(name), \
88  hboost::python::make_function(_TfPyWrapStaticToken((&key->name)), \
89  hboost::python::return_value_policy< \
90  hboost::python::return_by_value>(), \
91  hboost::mpl::vector1<std::string>()))
92 
93 #define _TF_PY_TOKENS_EXPAND(seq) \
94  HBOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
95  _TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)
96 
97 // Private macros to wrap a single element in a sequence.
98 #define _TF_PY_TOKENS_WRAP_ELEMENT(r, key, elem) \
99  _TF_PY_TOKENS_WRAP_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
100 
101 #define _TF_PY_TOKENS_WRAP_ATTR_ELEMENT(r, key, elem) \
102  _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
103 
104 #define _TF_PY_TOKEN_GET_ELEM(elem) \
105  HBOOST_PP_IIF(TF_PP_IS_TUPLE(elem), \
106  HBOOST_PP_TUPLE_ELEM(2, 0, elem), elem)
107 
108 // Private macros to wrap a sequence.
109 #define _TF_PY_TOKENS_WRAP_SEQ(key, seq) \
110  HBOOST_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ELEMENT, key, seq)
111 
112 #define _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq) \
113  HBOOST_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ATTR_ELEMENT, key, seq)
114 
116 
117 #endif // PXR_BASE_TF_PY_STATIC_TOKENS_H
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
std::string const & GetString() const
Return the string that this token represents.
Definition: token.h:207
Definition: token.h:87
_TfPyWrapStaticToken(const TfToken *token)
std::string operator()() const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1441
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91