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 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 //todo: cleanup: TF_DEFINE_PRIVATE_TOKENS, should use the public versions
25 //todo: cleanup: document each macro extensivly
26 //todo: cleanup: order macros, so that it is easier to see the structure
27 //todo: simply syntax, we should get rid of braces for each array element and
28 // each element
29 
30 #ifndef PXR_BASE_TF_STATIC_TOKENS_H
31 #define PXR_BASE_TF_STATIC_TOKENS_H
32 
33 /// \file tf/staticTokens.h
34 ///
35 /// This file defines some macros that are useful for declaring and using
36 /// static TfTokens. Typically, using static TfTokens is either cumbersome,
37 /// unsafe, or slow. These macros aim to solve many of the common problems.
38 ///
39 /// The following is an example of how they can be used.
40 ///
41 /// In header file:
42 ///
43 /// \code
44 /// #define MF_TOKENS \. <--- please ignore '.'
45 /// (transform) \.
46 /// (moves) \.
47 ///
48 /// // Syntax when string name differs from symbol.
49 /// ((foo, "bar"))
50 ///
51 /// // Syntax when defining an array of tokens. Note that the tokens can
52 /// // be used either with amountTs[i] or directly as tx, ty, tz.
53 /// ((amountTs, ( (tx) (ty) (tz) )))
54 ///
55 /// TF_DECLARE_PUBLIC_TOKENS(MfTokens, MF_TOKENS);
56 /// \endcode
57 ///
58 /// In cpp file:
59 ///
60 /// \code
61 /// TF_DEFINE_PUBLIC_TOKENS(MfTokens, MF_TOKENS);
62 /// \endcode
63 ///
64 /// Access the token by using the key as though it were a pointer, like this:
65 ///
66 /// \code
67 /// MfTokens->transform;
68 /// \endcode
69 ///
70 /// An additional member, allTokens, is a std::vector<TfToken> populated
71 /// with all of the generated token members.
72 ///
73 /// There are PUBLIC and PRIVATE versions of the macros. The PRIVATE ones are
74 /// intended to be used when the tokens will only be used in a single .cpp
75 /// file, in which case they can be made file static. In the case of the
76 /// PRIVATE, you only need to use the DEFINE macro.
77 
78 #include "pxr/pxr.h"
80 #include "pxr/base/tf/staticData.h"
81 #include "pxr/base/tf/token.h"
82 
83 #include <vector>
84 
85 #include <hboost/preprocessor/seq/enum.hpp>
86 #include <hboost/preprocessor/seq/filter.hpp>
87 #include <hboost/preprocessor/seq/for_each.hpp>
88 
90 
91 // TF_DECLARE_PUBLIC_TOKENS use these macros to handle two or three arguments.
92 // The three argument version takes an export/import macro (e.g. TF_API)
93 // while the two argument version does not export the tokens.
94 
95 #define _TF_DECLARE_PUBLIC_TOKENS3(key, eiapi, seq) \
96  _TF_DECLARE_TOKENS3(key, seq, eiapi) \
97  extern eiapi TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
98 #define _TF_DECLARE_PUBLIC_TOKENS2(key, seq) \
99  _TF_DECLARE_TOKENS2(key, seq) \
100  extern TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
101 #define _TF_DECLARE_PUBLIC_TOKENS(N) _TF_DECLARE_PUBLIC_TOKENS##N
102 #define _TF_DECLARE_PUBLIC_TOKENS_EVAL(N) _TF_DECLARE_PUBLIC_TOKENS(N)
103 #define _TF_DECLARE_PUBLIC_TOKENS_EXPAND(x) x
104 
105 /// Macro to define public tokens. This declares a list of tokens that can be
106 /// used globally. Use in conjunction with TF_DEFINE_PUBLIC_TOKENS.
107 /// \hideinitializer
108 #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__) )
109 
110 /// Macro to define public tokens. Use in conjunction with
111 /// TF_DECLARE_PUBLIC_TOKENS.
112 /// \hideinitializer
113 #define TF_DEFINE_PUBLIC_TOKENS(key, seq) \
114  _TF_DEFINE_TOKENS(key) \
115  TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key
116 
117 /// Macro to define private tokens.
118 /// \hideinitializer
119 #define TF_DEFINE_PRIVATE_TOKENS(key, seq) \
120  namespace { \
121  struct _TF_TOKENS_STRUCT_NAME_PRIVATE(key) { \
122  _TF_TOKENS_STRUCT_NAME_PRIVATE(key)() = default; \
123  _TF_TOKENS_DECLARE_MEMBERS(seq) \
124  }; \
125  } \
126  static TfStaticData<_TF_TOKENS_STRUCT_NAME_PRIVATE(key)> key
127 
128 ///////////////////////////////////////////////////////////////////////////////
129 // Private Macros
130 
131 // Private macro to generate struct name from key.
132 //
133 // Note that this needs to be a unique struct name for each translation unit.
134 //
135 #define _TF_TOKENS_STRUCT_NAME_PRIVATE(key) \
136  TF_PP_CAT(key, _PrivateStaticTokenType)
137 
138 // Private macro to generate struct name from key. This version is used
139 // by the public token declarations, and so key must be unique for the entire
140 // namespace.
141 //
142 #define _TF_TOKENS_STRUCT_NAME(key) \
143  TF_PP_CAT(key, _StaticTokenType)
144 
145 ///////////////////////////////////////////////////////////////////////////////
146 // Declaration Macros
147 
148 // Private macro used to generate TfToken member variables. elem can either
149 // be a tuple on the form (name, value) or just a name.
150 //
151 #define _TF_TOKENS_DECLARE_MEMBER(unused, elem) \
152  TfToken _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
153  TF_PP_TUPLE_ELEM(0, elem), elem){ \
154  _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
155  TF_PP_TUPLE_ELEM(1, elem), TF_PP_STRINGIZE(elem)), \
156  TfToken::Immortal};
157 #define _TF_TOKENS_DECLARE_TOKEN_MEMBERS(seq) \
158  TF_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_MEMBER, ~, seq)
159 
160 #define _TF_TOKENS_FORWARD_ARRAY(elem) TF_PP_TUPLE_ELEM(0, elem),
161 #define _TF_TOKENS_DECLARE_ARRAY_MEMBER_IMPL(identifier, ...) \
162  TfToken identifier[TF_PP_VARIADIC_SIZE(__VA_ARGS__)] = { \
163  TF_PP_FOR_EACH(_TF_TOKENS_FORWARD_ARRAY, __VA_ARGS__)};
164 #define _TF_TOKENS_DECLARE_ARRAY_MEMBER(r, data, elem) \
165  _TF_TOKENS_DECLARE_ARRAY_MEMBER_IMPL( \
166  TF_PP_TUPLE_ELEM(0, elem), \
167  HBOOST_PP_SEQ_ENUM(TF_PP_EAT_PARENS(TF_PP_TUPLE_ELEM(1, elem))))
168 #define _TF_TOKENS_DECLARE_ARRAY_MEMBERS(seq) \
169  HBOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_ARRAY_MEMBER, ~, seq)
170 
171 #define _TF_TOKENS_FORWARD_TOKEN(unused, elem) TF_PP_TUPLE_ELEM(0, elem),
172 #define _TF_TOKENS_DECLARE_ALL_TOKENS(seq) \
173  std::vector<TfToken> allTokens = \
174  {TF_PP_SEQ_FOR_EACH(_TF_TOKENS_FORWARD_TOKEN, ~, seq)};
175 
176 // Private macro used to declare the list of members as TfTokens
177 //
178 #define _TF_TOKENS_DECLARE_MEMBERS(seq) \
179  _TF_TOKENS_DECLARE_TOKEN_MEMBERS( \
180  HBOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
181  _TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq) \
182  ) \
183  _TF_TOKENS_DECLARE_ARRAY_MEMBERS( \
184  HBOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_ARRAY, ~, seq)) \
185  _TF_TOKENS_DECLARE_ALL_TOKENS( \
186  HBOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
187  _TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq) \
188  )
189 
190 // Private macro that expands all array elements to make them members
191 // of the sequence.
192 //
193 #define _TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq) \
194  HBOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_APPEND_ARRAY_ELEMENTS, \
195  ~, \
196  HBOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_ARRAY, ~, seq)) \
197 
198 // Private macro used to generate a struct of TfTokens.
199 //
200 #define _TF_DECLARE_TOKENS3(key, seq, eiapi) \
201  struct _TF_TOKENS_STRUCT_NAME(key) { \
202  eiapi _TF_TOKENS_STRUCT_NAME(key)(); \
203  eiapi ~_TF_TOKENS_STRUCT_NAME(key)(); \
204  _TF_TOKENS_DECLARE_MEMBERS(seq) \
205  };
206 
207 #define _TF_DECLARE_TOKENS2(key, seq) \
208  struct _TF_TOKENS_STRUCT_NAME(key) { \
209  _TF_TOKENS_STRUCT_NAME(key)(); \
210  ~_TF_TOKENS_STRUCT_NAME(key)(); \
211  _TF_TOKENS_DECLARE_MEMBERS(seq) \
212  };
213 
214 ///////////////////////////////////////////////////////////////////////////////
215 // Filtering Macros
216 
217 // Private predicate macros to be used by SEQ_FILTER that determine if an
218 // element of a sequence is an array of tokens or not.
219 //
220 #define _TF_TOKENS_IS_ARRAY(s, data, elem) \
221  _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
222  TF_PP_IS_TUPLE(TF_PP_TUPLE_ELEM(1, elem)), 0)
223 
224 #define _TF_TOKENS_IS_NOT_ARRAY(s, data, elem) \
225  _TF_PP_IFF(_TF_TOKENS_IS_ARRAY(s, data, elem), 0, 1)
226 
227 // Private macro to append all array elements to a sequence.
228 //
229 #define _TF_TOKENS_APPEND_ARRAY_ELEMENTS(r, data, elem) \
230  TF_PP_TUPLE_ELEM(0, TF_PP_TUPLE_ELEM(1, elem))
231 
232 ///////////////////////////////////////////////////////////////////////////////
233 // Definition Macros
234 
235 // Private macro to define the struct of tokens.
236 //
237 #define _TF_DEFINE_TOKENS(key) \
238  _TF_TOKENS_STRUCT_NAME(key)::~_TF_TOKENS_STRUCT_NAME(key)() = default; \
239  _TF_TOKENS_STRUCT_NAME(key)::_TF_TOKENS_STRUCT_NAME(key)() = default; \
240 
242 
243 #endif // PXR_BASE_TF_STATIC_TOKENS_H
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91