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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TF_PY_STATIC_TOKENS_H
9 #define PXR_BASE_TF_PY_STATIC_TOKENS_H
10 
11 /// \file tf/pyStaticTokens.h
12 
13 #include "pxr/pxr.h"
14 
15 #include <locale>
16 
19 
20 #include "pxr/external/boost/python/class.hpp"
21 #include "pxr/external/boost/python/scope.hpp"
22 
24 
25 /// Macro to wrap static tokens defined with \c TF_DEFINE_PUBLIC_TOKENS to
26 /// Python. It creates a class of name \p name in the current scope
27 /// containing just the tokens in \p seq in the static tokens named by \p key.
28 ///
29 /// \hideinitializer
30 #define TF_PY_WRAP_PUBLIC_TOKENS(name, key, seq) \
31  pxr_boost::python::class_< \
32  _TF_TOKENS_STRUCT_NAME(key), pxr_boost::python::noncopyable>( \
33  name, pxr_boost::python::no_init) \
34  _TF_PY_TOKENS_WRAP_SEQ(key, seq)
35 
36 /// Macro to wrap static tokens defined with \c TF_DEFINE_PUBLIC_TOKENS to
37 /// Python. This wraps tokens in \p seq in the static tokens named by \p key
38 /// as attributes on the current boost python scope.
39 ///
40 /// \hideinitializer
41 #define TF_PY_WRAP_PUBLIC_TOKENS_IN_CURRENT_SCOPE(key, seq) \
42  _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq)
43 
44 // Private macros to add a single data member.
45 #define _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, name) \
46  pxr_boost::python::scope().attr( \
47  TF_PP_STRINGIZE(name)) = key->name.GetString();
48 
49 // We wrap tokens as Python strings, but simply wrapping the token using
50 // def_readonly bypasses to-Python conversion, leading to the error
51 // that there's no Python type for the C++ TfToken type. See:
52 // https://www.boost.org/doc/libs/release/libs/python/doc/html/faq/why_is_my_automatic_to_python_co.html
53 //
54 // So we use add_static_property and wrap a function that performs the
55 // conversion instead.
56 #define _TF_PY_TOKENS_WRAP_MEMBER(r, key, name) \
57  .add_static_property(TF_PP_STRINGIZE(name), \
58  +[]() { return key->name.GetString(); }) \
59 
60 // Private macros to wrap a single element in a sequence.
61 #define _TF_PY_TOKENS_WRAP_ELEMENT(key, elem) \
62  _TF_PY_TOKENS_WRAP_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
63 
64 #define _TF_PY_TOKENS_WRAP_ATTR_ELEMENT(key, elem) \
65  _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
66 
67 #define _TF_PY_TOKEN_GET_ELEM(elem) \
68  _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
69  TF_PP_TUPLE_ELEM(0, elem), elem)
70 
71 // Private macros to wrap a sequence.
72 #define _TF_PY_TOKENS_WRAP_SEQ(key, seq) \
73  TF_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ELEMENT, key, seq)
74 
75 #define _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq) \
76  TF_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ATTR_ELEMENT, key, seq)
77 
79 
80 #endif // PXR_BASE_TF_PY_STATIC_TOKENS_H
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74