Loading...
Searching...
No Matches
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 <boost/python/class.hpp>
21#include <boost/python/scope.hpp>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
30#define TF_PY_WRAP_PUBLIC_TOKENS(name, key, seq) \
31 boost::python::class_<_TF_TOKENS_STRUCT_NAME(key), boost::noncopyable>( \
32 name, boost::python::no_init) \
33 _TF_PY_TOKENS_WRAP_SEQ(key, seq)
34
40#define TF_PY_WRAP_PUBLIC_TOKENS_IN_CURRENT_SCOPE(key, seq) \
41 _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq)
42
43// Helper to return a static token as a string. We wrap tokens as Python
44// strings and for some reason simply wrapping the token using def_readonly
45// bypasses to-Python conversion, leading to the error that there's no
46// Python type for the C++ TfToken type. So we wrap this functor instead.
47class _TfPyWrapStaticToken {
48public:
49 _TfPyWrapStaticToken(const TfToken* token) : _token(token) { }
50
51 std::string operator()() const
52 {
53 return _token->GetString();
54 }
55
56private:
57 const TfToken* _token;
58};
59
60// Private macros to add a single data member.
61#define _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, name) \
62 boost::python::scope().attr( \
63 TF_PP_STRINGIZE(name)) = key->name.GetString();
64
65#define _TF_PY_TOKENS_WRAP_MEMBER(r, key, name) \
66 .add_static_property(TF_PP_STRINGIZE(name), \
67 boost::python::make_function(_TfPyWrapStaticToken((&key->name)), \
68 boost::python::return_value_policy< \
69 boost::python::return_by_value>(), \
70 boost::mpl::vector1<std::string>()))
71
72// Private macros to wrap a single element in a sequence.
73#define _TF_PY_TOKENS_WRAP_ELEMENT(key, elem) \
74 _TF_PY_TOKENS_WRAP_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
75
76#define _TF_PY_TOKENS_WRAP_ATTR_ELEMENT(key, elem) \
77 _TF_PY_TOKENS_WRAP_ATTR_MEMBER(r, key, _TF_PY_TOKEN_GET_ELEM(elem))
78
79#define _TF_PY_TOKEN_GET_ELEM(elem) \
80 _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
81 TF_PP_TUPLE_ELEM(0, elem), elem)
82
83// Private macros to wrap a sequence.
84#define _TF_PY_TOKENS_WRAP_SEQ(key, seq) \
85 TF_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ELEMENT, key, seq)
86
87#define _TF_PY_TOKENS_WRAP_ATTR_SEQ(key, seq) \
88 TF_PP_SEQ_FOR_EACH(_TF_PY_TOKENS_WRAP_ATTR_ELEMENT, key, seq)
89
90PXR_NAMESPACE_CLOSE_SCOPE
91
92#endif // PXR_BASE_TF_PY_STATIC_TOKENS_H
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
This file defines some macros that are useful for declaring and using static TfTokens.