All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
12
13#include "pxr/pxr.h"
14
15#include <locale>
16
18#include "pxr/base/tf/preprocessorUtilsLite.h"
19
20#include "pxr/external/boost/python/class.hpp"
21#include "pxr/external/boost/python/scope.hpp"
22
23PXR_NAMESPACE_OPEN_SCOPE
24
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
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
78PXR_NAMESPACE_CLOSE_SCOPE
79
80#endif // PXR_BASE_TF_PY_STATIC_TOKENS_H
This file defines some macros that are useful for declaring and using static TfTokens.