Skip to content

Commit

Permalink
Use in class initializers to simplify token macros
Browse files Browse the repository at this point in the history
  • Loading branch information
nvmkuruc committed Nov 21, 2023
1 parent 0e11364 commit 883f839
Showing 1 changed file with 39 additions and 93 deletions.
132 changes: 39 additions & 93 deletions pxr/base/tf/staticTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@

#include <vector>

#include <boost/preprocessor/punctuation/comma_if.hpp>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/filter.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/seq/size.hpp>

PXR_NAMESPACE_OPEN_SCOPE

Expand All @@ -113,25 +111,15 @@ PXR_NAMESPACE_OPEN_SCOPE
/// TF_DECLARE_PUBLIC_TOKENS.
/// \hideinitializer
#define TF_DEFINE_PUBLIC_TOKENS(key, seq) \
_TF_DEFINE_TOKENS(key, seq) \
_TF_DEFINE_TOKENS(key) \
TfStaticData<_TF_TOKENS_STRUCT_NAME(key)> key

/// Macro to define private tokens.
/// \hideinitializer
#define TF_DEFINE_PRIVATE_TOKENS(key, seq) \
namespace { \
struct _TF_TOKENS_STRUCT_NAME_PRIVATE(key) { \
_TF_TOKENS_STRUCT_NAME_PRIVATE(key)() : \
_TF_TOKENS_INITIALIZE_SEQ( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)) \
{ \
_TF_TOKENS_ASSIGN_ARRAY_SEQ( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_ARRAY, ~, seq)) \
_TF_TOKENS_BUILD_ALLTOKENS_VECTOR( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)) \
} \
_TF_TOKENS_STRUCT_NAME_PRIVATE(key)() = default; \
_TF_TOKENS_DECLARE_MEMBERS(seq) \
}; \
} \
Expand Down Expand Up @@ -160,27 +148,44 @@ PXR_NAMESPACE_OPEN_SCOPE
// Private macro used to generate TfToken member variables. elem can either
// be a tuple on the form (name, value) or just a name.
//
#define _TF_TOKENS_DECLARE_MEMBER(r, data, elem) \
#define _TF_TOKENS_DECLARE_MEMBER(r, unused, elem) \
TfToken _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
TF_PP_TUPLE_ELEM(0, elem), elem);

#define _TF_TOKENS_DECLARE_ARRAY_MEMBER_IMPL(identifier, size) \
TfToken identifier[size];

TF_PP_TUPLE_ELEM(0, elem), elem){ \
_TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
TF_PP_TUPLE_ELEM(1, elem), TF_PP_STRINGIZE(elem)), \
TfToken::Immortal};
#define _TF_TOKENS_DECLARE_TOKEN_MEMBERS(seq) \
BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_MEMBER, ~, seq)

#define _TF_TOKENS_FORWARD_ARRAY(elem) TF_PP_TUPLE_ELEM(0, elem),
#define _TF_TOKENS_DECLARE_ARRAY_MEMBER_IMPL(identifier, ...) \
TfToken identifier[TF_PP_VARIADIC_SIZE(__VA_ARGS__)] = { \
TF_PP_FOR_EACH(_TF_TOKENS_FORWARD_ARRAY, __VA_ARGS__)};
#define _TF_TOKENS_DECLARE_ARRAY_MEMBER(r, data, elem) \
_TF_TOKENS_DECLARE_ARRAY_MEMBER_IMPL( \
TF_PP_TUPLE_ELEM(0, elem), \
BOOST_PP_SEQ_SIZE(TF_PP_EAT_PARENS(TF_PP_TUPLE_ELEM(1, elem))))
BOOST_PP_SEQ_ENUM(TF_PP_EAT_PARENS(TF_PP_TUPLE_ELEM(1, elem))))
#define _TF_TOKENS_DECLARE_ARRAY_MEMBERS(seq) \
BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_ARRAY_MEMBER, ~, seq)

#define _TF_TOKENS_FORWARD_TOKEN(r, unused, elem) TF_PP_TUPLE_ELEM(0, elem),
#define _TF_TOKENS_DECLARE_ALL_TOKENS(seq) \
std::vector<TfToken> allTokens = \
{BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_FORWARD_TOKEN, ~, seq)};

// Private macro used to declare the list of members as TfTokens
//
#define _TF_TOKENS_DECLARE_MEMBERS(seq) \
BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_MEMBER, ~, \
#define _TF_TOKENS_DECLARE_MEMBERS(seq) \
_TF_TOKENS_DECLARE_TOKEN_MEMBERS( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)) \
BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DECLARE_ARRAY_MEMBER, ~, \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq) \
) \
_TF_TOKENS_DECLARE_ARRAY_MEMBERS( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_ARRAY, ~, seq)) \
std::vector<TfToken> allTokens;
_TF_TOKENS_DECLARE_ALL_TOKENS( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq) \
)

// Private macro that expands all array elements to make them members
// of the sequence.
Expand All @@ -207,52 +212,7 @@ PXR_NAMESPACE_OPEN_SCOPE
};

///////////////////////////////////////////////////////////////////////////////
// Definition Macros

// Private macros to define members in the tokens struct.
//
#define _TF_TOKENS_DEFINE_MEMBER(r, data, i, elem) \
BOOST_PP_COMMA_IF(i) \
TF_PP_TUPLE_ELEM(0, _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
(_TF_TOKENS_INITIALIZE_MEMBER_TUPLE(elem)), \
(_TF_TOKENS_INITIALIZE_MEMBER(elem))))

#define _TF_TOKENS_INITIALIZE_MEMBER_TUPLE(elem) \
TF_PP_TUPLE_ELEM(0, elem)(TF_PP_TUPLE_ELEM(1, elem), TfToken::Immortal)

#define _TF_TOKENS_INITIALIZE_MEMBER(elem) \
elem(TF_PP_STRINGIZE(elem), TfToken::Immortal)

#define _TF_TOKENS_DEFINE_ARRAY_MEMBER(r, data, i, elem) \
data[i] = _TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
TF_PP_TUPLE_ELEM(0, elem), elem);

// Private macros to append tokens to the allTokens vector.
//
#define _TF_TOKENS_APPEND_MEMBER(r, data, i, elem) \
_TF_PP_IFF(TF_PP_IS_TUPLE(elem), \
_TF_TOKENS_APPEND_MEMBER_BODY(~, ~, \
TF_PP_TUPLE_ELEM(0, elem)), \
_TF_TOKENS_APPEND_MEMBER_BODY(~, ~, elem))

#define _TF_TOKENS_APPEND_MEMBER_BODY(r, data, elem) \
allTokens.push_back(elem);

#define _TF_TOKENS_BUILD_ALLTOKENS_VECTOR(seq) \
BOOST_PP_SEQ_FOR_EACH_I(_TF_TOKENS_APPEND_MEMBER, ~, seq)

// Private macros to generate the list of initialized members.
//
#define _TF_TOKENS_INITIALIZE_SEQ(seq) \
BOOST_PP_SEQ_FOR_EACH_I(_TF_TOKENS_DEFINE_MEMBER, ~, seq)

#define _TF_TOKENS_ASSIGN_ARRAY_SEQ(seq) \
BOOST_PP_SEQ_FOR_EACH(_TF_TOKENS_DEFINE_ARRAY_MEMBERS, ~, seq)

#define _TF_TOKENS_DEFINE_ARRAY_MEMBERS(r, data, elem) \
BOOST_PP_SEQ_FOR_EACH_I(_TF_TOKENS_DEFINE_ARRAY_MEMBER, \
TF_PP_TUPLE_ELEM(0, elem), \
TF_PP_TUPLE_ELEM(0, TF_PP_TUPLE_ELEM(1, elem)))
// Filtering Macros

// Private predicate macros to be used by SEQ_FILTER that determine if an
// element of a sequence is an array of tokens or not.
Expand All @@ -269,28 +229,14 @@ PXR_NAMESPACE_OPEN_SCOPE
#define _TF_TOKENS_APPEND_ARRAY_ELEMENTS(r, data, elem) \
TF_PP_TUPLE_ELEM(0, TF_PP_TUPLE_ELEM(1, elem))

// Private macro to define the struct of tokens.
//
// This works by filtering the incoming seq in two ways. For the body of the
// constructor, only array tokens are passed through (because they can't be
// initialized via initializer lists). The initializer list's items are all
// non-array seq elements _plus_ all array members themshelves. This way,
// array tokens are also accessible without using [] which proved to be
// a neat shortcut.
///////////////////////////////////////////////////////////////////////////////
// Definition Macros

// Private macro to define the struct of tokens.
//
#define _TF_DEFINE_TOKENS(key, seq) \
#define _TF_DEFINE_TOKENS(key) \
_TF_TOKENS_STRUCT_NAME(key)::~_TF_TOKENS_STRUCT_NAME(key)() = default; \
_TF_TOKENS_STRUCT_NAME(key)::_TF_TOKENS_STRUCT_NAME(key)() : \
_TF_TOKENS_INITIALIZE_SEQ( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)) \
{ \
_TF_TOKENS_ASSIGN_ARRAY_SEQ( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_ARRAY, ~, seq)) \
_TF_TOKENS_BUILD_ALLTOKENS_VECTOR( \
BOOST_PP_SEQ_FILTER(_TF_TOKENS_IS_NOT_ARRAY, ~, seq) \
_TF_TOKENS_EXPAND_ARRAY_ELEMENTS(seq)) \
}
_TF_TOKENS_STRUCT_NAME(key)::_TF_TOKENS_STRUCT_NAME(key)() = default; \

PXR_NAMESPACE_CLOSE_SCOPE

Expand Down

0 comments on commit 883f839

Please sign in to comment.