Skip to content

Commit

Permalink
Implement sdf/types.h internal unit macros with `tf/preprocessorUti…
Browse files Browse the repository at this point in the history
…lsLite.h`
  • Loading branch information
nvmkuruc committed Sep 9, 2023
1 parent 6988a51 commit 756dbb1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
49 changes: 41 additions & 8 deletions pxr/usd/sdf/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "pxr/base/tf/staticData.h"
#include "pxr/base/tf/type.h"

#include <boost/preprocessor/seq/size.hpp>
#include <array>
#include <unordered_map>

using std::map;
Expand Down Expand Up @@ -68,6 +70,35 @@ TF_REGISTRY_FUNCTION(TfType)
TfType::Define<SdfValueBlock>();
}

// Max units is computed by running `BOOST_PP_SEQ_SIZE`
// on every sequence in the varaidic args to populate an
// initializer list for `std::max`.
// A comma is appended to each element.
#define _SDF_UNIT_MAX_UNITS_IMPL(seq) \
BOOST_PP_SEQ_SIZE(seq),
#define _SDF_UNIT_MAX_UNITS_OP(elem) \
_SDF_UNIT_MAX_UNITS_IMPL(BOOST_PP_TUPLE_ELEM(1, elem))

constexpr size_t _Sdf_UnitMaxUnits =
std::max(
{
_SDF_FOR_EACH_UNITS(_SDF_UNIT_MAX_UNITS_OP,
_SDF_UNITS)
// Add 0 to pair with the trailing comma generated by
// the final evaluation of _SDF_UNIT_MAX_UNITS_OP
0
}
);

// Compute the number of unit enums
constexpr size_t _Sdf_UnitNumTypes =
TF_PP_VARIADIC_SIZE(TF_PP_EAT_PARENS(_SDF_UNITS));

static_assert(_Sdf_UnitNumTypes > 0,
"There must be at least one define Sdf unit enum.");
static_assert(_Sdf_UnitMaxUnits > 0,
"There must be at least one defined Sdf unit .");

template <typename T>
static VtValue
_GetTfEnumForEnumValue(const VtValue &value)
Expand Down Expand Up @@ -129,8 +160,10 @@ struct _UnitsInfo {
map<string, TfEnum> _DefaultUnitsMap;
map<string, TfEnum> _UnitCategoryToDefaultUnitMap;
map<string, string> _UnitTypeNameToUnitCategoryMap;
TfEnum _UnitIndicesTable[_SDF_UNIT_NUM_TYPES][_SDF_UNIT_MAX_UNITS];
string _UnitNameTable[_SDF_UNIT_NUM_TYPES][_SDF_UNIT_MAX_UNITS];
std::array<std::array<TfEnum, _Sdf_UnitMaxUnits>,
_Sdf_UnitNumTypes> _UnitIndicesTable;
std::array<std::array<std::string, _Sdf_UnitMaxUnits>,
_Sdf_UnitNumTypes> _UnitNameTable;
map<string, TfEnum> _UnitNameToUnitMap;
map<string, uint32_t> _UnitTypeIndicesTable;
};
Expand Down Expand Up @@ -174,15 +207,15 @@ static void _AddToUnitsMaps(_UnitsInfo &info,
TF_PP_CAT(Sdf ## category ## Unit, _SDF_UNIT_TAG(elem)), \
_SDF_UNIT_NAME(elem));

#define _REGISTRY_FUNCTION(r, unused, elem) \
#define _REGISTRY_FUNCTION(elem) \
TF_REGISTRY_FUNCTION_WITH_TAG(TfEnum, _SDF_UNITSLIST_CATEGORY(elem)) \
{ \
BOOST_PP_SEQ_FOR_EACH(_ADD_UNIT_ENUM, \
_SDF_UNITSLIST_CATEGORY(elem), \
_SDF_UNITSLIST_TUPLES(elem)); \
}

BOOST_PP_LIST_FOR_EACH(_REGISTRY_FUNCTION, ~, _SDF_UNITS)
_SDF_FOR_EACH_UNITS(_REGISTRY_FUNCTION, _SDF_UNITS)

#define _ADD_UNIT_TO_MAPS(r, category, elem) \
_AddToUnitsMaps( \
Expand All @@ -191,14 +224,14 @@ BOOST_PP_LIST_FOR_EACH(_REGISTRY_FUNCTION, ~, _SDF_UNITS)
_SDF_UNIT_NAME(elem), \
_SDF_UNIT_SCALE(elem), #category);

#define _POPULATE_UNIT_MAPS(r, unused, elem) \
#define _POPULATE_UNIT_MAPS(elem) \
BOOST_PP_SEQ_FOR_EACH(_ADD_UNIT_TO_MAPS, \
_SDF_UNITSLIST_CATEGORY(elem), \
_SDF_UNITSLIST_TUPLES(elem)) \

static _UnitsInfo *_MakeUnitsMaps() {
_UnitsInfo *info = new _UnitsInfo;
BOOST_PP_LIST_FOR_EACH(_POPULATE_UNIT_MAPS, ~, _SDF_UNITS);
_SDF_FOR_EACH_UNITS(_POPULATE_UNIT_MAPS, _SDF_UNITS);
return info;
}

Expand All @@ -210,7 +243,7 @@ static _UnitsInfo &_GetUnitsInfo() {
#undef _REGISTRY_FUNCTION
#undef _PROCESS_ENUMERANT

#define _REGISTRY_FUNCTION(r, unused, elem) \
#define _REGISTRY_FUNCTION(elem) \
TF_REGISTRY_FUNCTION_WITH_TAG(TfType, TF_PP_CAT(Type, _SDF_UNITSLIST_CATEGORY(elem))) \
{ \
TfType::Define<_SDF_UNITSLIST_ENUM(elem)>(); \
Expand All @@ -219,7 +252,7 @@ TF_REGISTRY_FUNCTION_WITH_TAG(VtValue, TF_PP_CAT(Value, _SDF_UNITSLIST_CATEGORY(
{ \
_RegisterEnumWithVtValue<_SDF_UNITSLIST_ENUM(elem)>(); \
}
BOOST_PP_LIST_FOR_EACH(_REGISTRY_FUNCTION, ~, _SDF_UNITS)
_SDF_FOR_EACH_UNITS(_REGISTRY_FUNCTION, _SDF_UNITS)
#undef _REGISTRY_FUNCTION

TfEnum
Expand Down
30 changes: 8 additions & 22 deletions pxr/usd/sdf/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@
#include "pxr/base/vt/dictionary.h"
#include "pxr/base/vt/value.h"

#include <boost/preprocessor/list/for_each.hpp>
#include <boost/preprocessor/list/size.hpp>
#include <boost/preprocessor/selection/max.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/seq.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <iosfwd>
#include <list>
#include <map>
Expand Down Expand Up @@ -238,9 +233,8 @@ enum SdfAuthoringError

#define _SDF_UNITS \
((Length, _SDF_LENGTH_UNITS), \
((Angular, _SDF_ANGULAR_UNITS), \
((Dimensionless, _SDF_DIMENSIONLESS_UNITS), \
BOOST_PP_NIL)))
(Angular, _SDF_ANGULAR_UNITS), \
(Dimensionless, _SDF_DIMENSIONLESS_UNITS))

#define _SDF_UNIT_TAG(tup) BOOST_PP_TUPLE_ELEM(3, 0, tup)
#define _SDF_UNIT_NAME(tup) BOOST_PP_TUPLE_ELEM(3, 1, tup)
Expand All @@ -254,27 +248,19 @@ enum SdfAuthoringError
#define _SDF_DECLARE_UNIT_ENUMERANT(r, tag, elem) \
TF_PP_CAT(Sdf ## tag ## Unit, _SDF_UNIT_TAG(elem)),

#define _SDF_DECLARE_UNIT_ENUM(r, unused, elem) \
#define _SDF_DECLARE_UNIT_ENUM(elem) \
enum _SDF_UNITSLIST_ENUM(elem) { \
BOOST_PP_SEQ_FOR_EACH(_SDF_DECLARE_UNIT_ENUMERANT, \
_SDF_UNITSLIST_CATEGORY(elem), \
_SDF_UNITSLIST_TUPLES(elem)) \
};
BOOST_PP_LIST_FOR_EACH(_SDF_DECLARE_UNIT_ENUM, ~, _SDF_UNITS)

// Compute the max number of enumerants over all unit enums
#define _SDF_MAX_UNITS_OP(d, state, list) \
BOOST_PP_MAX_D(d, state, BOOST_PP_SEQ_SIZE(_SDF_UNITSLIST_TUPLES(list)))
#define _SDF_UNIT_MAX_UNITS \
BOOST_PP_LIST_FOLD_LEFT(_SDF_MAX_UNITS_OP, 0, _SDF_UNITS)
#define _SDF_FOR_EACH_UNITS_IMPL(macro, ...) \
TF_PP_FOR_EACH(macro, __VA_ARGS__)
#define _SDF_FOR_EACH_UNITS(macro, args) \
_SDF_FOR_EACH_UNITS_IMPL(macro, TF_PP_EAT_PARENS(args))

// Compute the number of unit enums
#define _SDF_UNIT_NUM_TYPES BOOST_PP_LIST_SIZE(_SDF_UNITS)

// Compute the number of bits needed to hold _SDF_UNIT_MAX_UNITS and
// _SDF_UNIT_NUM_TYPES.
#define _SDF_UNIT_MAX_UNITS_BITS TF_BITS_FOR_VALUES(_SDF_UNIT_MAX_UNITS)
#define _SDF_UNIT_TYPES_BITS TF_BITS_FOR_VALUES(_SDF_UNIT_NUM_TYPES)
_SDF_FOR_EACH_UNITS(_SDF_DECLARE_UNIT_ENUM, _SDF_UNITS)

/// A map of mapper parameter names to parameter values.
typedef std::map<std::string, VtValue> SdfMapperParametersMap;
Expand Down
4 changes: 2 additions & 2 deletions pxr/usd/sdf/wrapTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ void wrapTypes()
VtValueFromPython<SdfAuthoringError>();

// Wrap all units enums.
#define _WRAP_ENUM(r, unused, elem) \
#define _WRAP_ENUM(elem) \
TfPyWrapEnum<_SDF_UNITSLIST_ENUM(elem)>(); \
VtValueFromPython<_SDF_UNITSLIST_ENUM(elem)>();
BOOST_PP_LIST_FOR_EACH(_WRAP_ENUM, ~, _SDF_UNITS)
_SDF_FOR_EACH_UNITS(_WRAP_ENUM, _SDF_UNITS)
#undef _WRAP_ENUM

SdfPyWrapListProxy<SdfNameOrderProxy>();
Expand Down

0 comments on commit 756dbb1

Please sign in to comment.