HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
staticTokens.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 //todo: cleanup: TF_DEFINE_PRIVATE_TOKENS, should use the public versions
8 //todo: cleanup: document each macro extensively
9 //todo: cleanup: order macros, so that it is easier to see the structure
10 
11 #ifndef PXR_BASE_TF_STATIC_TOKENS_H
12 #define PXR_BASE_TF_STATIC_TOKENS_H
13 
14 /// \file tf/staticTokens.h
15 ///
16 /// This file defines some macros that are useful for declaring and using
17 /// static TfTokens. Typically, using static TfTokens is either cumbersome,
18 /// unsafe, or slow. These macros aim to solve many of the common problems.
19 ///
20 /// The following is an example of how they can be used.
21 ///
22 /// In header file:
23 ///
24 /// \code
25 /// #define MYLIB_TOKENS \. <--- please ignore '.'
26 /// (token1) \.
27 /// (token2) \.
28 ///
29 /// // Syntax when string name differs from symbol.
30 /// ((foo, "bar"))
31 ///
32 /// // Use this form if the symbols for the static tokens must be exported,
33 /// // passing in the associated import/export macro (e.g. MY_LIB_API) as
34 /// // the second argument.
35 /// TF_DECLARE_PUBLIC_TOKENS(MyLibTokens, MYLIB_API, MYLIB_TOKENS);
36 /// // Use this form otherwise.
37 /// // TF_DECLARE_PUBLIC_TOKENS(MyLibTokens, MYLIB_TOKENS);
38 /// \endcode
39 ///
40 /// In cpp file:
41 ///
42 /// \code
43 /// TF_DEFINE_PUBLIC_TOKENS(MyLibTokens, MYLIB_TOKENS);
44 /// \endcode
45 ///
46 /// Access the token by using the key as though it were a pointer, like this:
47 ///
48 /// \code
49 /// MyLibTokens->token1;
50 /// \endcode
51 ///
52 /// An additional member, allTokens, is a std::vector<TfToken> populated
53 /// with all of the generated token members.
54 ///
55 /// There are PUBLIC and PRIVATE versions of the macros. The PRIVATE ones are
56 /// intended to be used when the tokens will only be used in a single .cpp
57 /// file, in which case they can be made file static. In the case of the
58 /// PRIVATE, you only need to use the DEFINE macro.
59 
60 #include "pxr/pxr.h"
61 #include "pxr/base/arch/defines.h"
63 #include "pxr/base/tf/staticData.h"
64 #include "pxr/base/tf/token.h"
65 
66 #include <vector>
67 
69 
70 // TF_DECLARE_PUBLIC_TOKENS use these macros to handle two or three arguments.
71 // The three argument version takes an export/import macro (e.g. TF_API)
72 // while the two argument version does not export the tokens.
73 
74 #define _TF_DECLARE_PUBLIC_TOKENS3(key, eiapi, seq) \
75  _TF_DECLARE_TOKENS3(key, seq, eiapi) \
76  extern eiapi TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
77 #define _TF_DECLARE_PUBLIC_TOKENS2(key, seq) \
78  _TF_DECLARE_TOKENS2(key, seq) \
79  extern TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
80 #define _TF_DECLARE_PUBLIC_TOKENS(N) _TF_DECLARE_PUBLIC_TOKENS##N
81 #define _TF_DECLARE_PUBLIC_TOKENS_EVAL(N) _TF_DECLARE_PUBLIC_TOKENS(N)
82 #define _TF_DECLARE_PUBLIC_TOKENS_EXPAND(x) x
83 
84 /// Macro to define public tokens. This declares a list of tokens that can be
85 /// used globally. Use in conjunction with TF_DEFINE_PUBLIC_TOKENS.
86 /// \hideinitializer
87 // This macro expansion is disabled for Intellisense to avoid severe
88 // slowdowns when using MSVC's non-conforming preprocessor.
89 #if defined(ARCH_PREPROCESSOR_MSVC_TRADITIONAL) && defined(__INTELLISENSE__)
90 #define TF_DECLARE_PUBLIC_TOKENS(...)
91 #else
92 #define TF_DECLARE_PUBLIC_TOKENS(...) _TF_DECLARE_PUBLIC_TOKENS_EXPAND( _TF_DECLARE_PUBLIC_TOKENS_EVAL(_TF_DECLARE_PUBLIC_TOKENS_EXPAND( TF_PP_VARIADIC_SIZE(__VA_ARGS__) ))(__VA_ARGS__) )
93 #endif
94 
95 /// Macro to define public tokens. Use in conjunction with
96 /// TF_DECLARE_PUBLIC_TOKENS.
97 /// \hideinitializer
98 #define TF_DEFINE_PUBLIC_TOKENS(key, seq) \
99  _TF_DEFINE_TOKENS(key) \
100  TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
101 
102 /// Macro to define private tokens.
103 /// \hideinitializer
104 #define TF_DEFINE_PRIVATE_TOKENS(key, seq) \
105  namespace { \
106  struct _TF_TOKENS_STRUCT_NAME_PRIVATE(key) { \
107  _TF_TOKENS_STRUCT_NAME_PRIVATE(key)() = default; \
108  _TF_TOKENS_DECLARE_MEMBERS(seq) \
109  }; \
110  } \
111  static TfStaticData<_TF_TOKENS_STRUCT_NAME_PRIVATE(key)> key
112 
113 ///////////////////////////////////////////////////////////////////////////////
114 // Private Macros
115 
116 // Private macro to generate struct name from key.
117 //
118 // Note that this needs to be a unique struct name for each translation unit.
119 //
120 #define _TF_TOKENS_STRUCT_NAME_PRIVATE(key) \
121  TF_PP_CAT(key, _PrivateStaticTokenType)
122 
123 // Private macro to generate struct name from key. This version is used
124 // by the public token declarations, and so key must be unique for the entire
125 // namespace.
126 //
127 #define _TF_TOKENS_STRUCT_NAME(key) \
128  TF_PP_CAT(key, _StaticTokenType)
129 
130 ///////////////////////////////////////////////////////////////////////////////
131 // Declaration Macros
132 
133 // Private macro used to generate TfToken member variables. elem can either
134 // be a tuple on the form (name, value) or just a name.
135 //
136 #define _TF_TOKENS_DECLARE_MEMBER(unused, elem) \
137  TfToken _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
138  TF_PP_TUPLE_ELEM(0, elem), elem){ \
139  _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
140  TF_PP_TUPLE_ELEM(1, elem), TF_PP_STRINGIZE(elem)), \
141  TfToken::Immortal};
142 #define _TF_TOKENS_DECLARE_TOKEN_MEMBERS(seq) \
143  TF_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_MEMBER, ~, seq)
144 
145 #define _TF_TOKENS_FORWARD_TOKEN(unused, elem) TF_PP_TUPLE_ELEM(0, elem),
146 #define _TF_TOKENS_DECLARE_ALL_TOKENS(seq) \
147  std::vector<TfToken> allTokens = \
148  {TF_PP_SEQ_FOR_EACH(_TF_TOKENS_FORWARD_TOKEN, ~, seq)};
149 
150 // Private macro used to declare the list of members as TfTokens
151 //
152 #define _TF_TOKENS_DECLARE_MEMBERS(seq) \
153  _TF_TOKENS_DECLARE_TOKEN_MEMBERS(seq) \
154  _TF_TOKENS_DECLARE_ALL_TOKENS(seq) \
155 
156 // Private macro used to generate a struct of TfTokens.
157 //
158 #define _TF_DECLARE_TOKENS3(key, seq, eiapi) \
159  struct _TF_TOKENS_STRUCT_NAME(key) { \
160  eiapi _TF_TOKENS_STRUCT_NAME(key)(); \
161  eiapi ~_TF_TOKENS_STRUCT_NAME(key)(); \
162  _TF_TOKENS_DECLARE_MEMBERS(seq) \
163  };
164 
165 #define _TF_DECLARE_TOKENS2(key, seq) \
166  struct _TF_TOKENS_STRUCT_NAME(key) { \
167  _TF_TOKENS_STRUCT_NAME(key)(); \
168  ~_TF_TOKENS_STRUCT_NAME(key)(); \
169  _TF_TOKENS_DECLARE_MEMBERS(seq) \
170  };
171 
172 ///////////////////////////////////////////////////////////////////////////////
173 // Definition Macros
174 
175 // Private macro to define the struct of tokens.
176 //
177 #define _TF_DEFINE_TOKENS(key) \
178  _TF_TOKENS_STRUCT_NAME(key)::~_TF_TOKENS_STRUCT_NAME(key)() = default; \
179  _TF_TOKENS_STRUCT_NAME(key)::_TF_TOKENS_STRUCT_NAME(key)() = default; \
180 
182 
183 #endif // PXR_BASE_TF_STATIC_TOKENS_H
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74