Skip to content

Commit

Permalink
Merge pull request #2831 from nvmkuruc/vttypeindex
Browse files Browse the repository at this point in the history
Replace `BOOST_PP_SEQ_FOR_EACH_I` with `constexpr` expressions in `vt`

(Internal change: 2307122)
  • Loading branch information
pixar-oss committed Dec 4, 2023
2 parents 94ba3e2 + ce07eb1 commit b0d709b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
45 changes: 33 additions & 12 deletions pxr/base/vt/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#include "pxr/base/arch/inttypes.h"
#include "pxr/base/gf/declare.h"
#include "pxr/base/gf/half.h"
#include "pxr/base/tf/meta.h"
#include "pxr/base/tf/preprocessorUtilsLite.h"
#include "pxr/base/tf/token.h"

#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>

#include <cstddef>
#include <cstring>
Expand Down Expand Up @@ -189,33 +189,54 @@ VT_ARRAY_VALUE_TYPES VT_SCALAR_CLASS_VALUE_TYPES VT_NONARRAY_VALUE_TYPES
#define VT_VALUE_TYPES \
VT_BUILTIN_VALUE_TYPES VT_CLASS_VALUE_TYPES

#define _VT_MAP_TYPE_LIST(r, unused, elem) , VT_TYPE(elem)

// Populate a type list from the preprocessor sequence.
// void is prepended to match the comma for the first type
// and then dropped by TfMetaTail.
using Vt_ValueTypeList =
TfMetaApply<TfMetaTail, TfMetaList<
void BOOST_PP_SEQ_FOR_EACH(_VT_MAP_TYPE_LIST, ~, VT_VALUE_TYPES)>>;

namespace Vt_KnownValueTypeDetail
{

// Implement compile-time value type indexes.
//
// Base case -- unknown types get index -1.
template <class T>
template <typename T>
constexpr int
GetIndex() {
GetIndexImpl(TfMetaList<>) {
return -1;
}

// Set indexes for known types.
#define VT_SET_VALUE_TYPE_INDEX(r, unused, i, elem) \
template <> constexpr int \
GetIndex< VT_TYPE(elem) >() { \
return i; \
template <typename T, typename Typelist>
constexpr int
GetIndexImpl(Typelist) {
if (std::is_same_v<T, TfMetaApply<TfMetaHead, Typelist>>) {
return 0;
}
else if (const int indexOfTail =
GetIndexImpl<T>(TfMetaApply<TfMetaTail, Typelist>{});
indexOfTail >= 0) {
return 1 + indexOfTail;
}
BOOST_PP_SEQ_FOR_EACH_I(VT_SET_VALUE_TYPE_INDEX, ~, VT_VALUE_TYPES)
#undef VT_SET_VALUE_TYPE_INDEX
else {
return -1;
}
}

template <typename T>
constexpr int
GetIndex() {
return GetIndexImpl<T>(Vt_ValueTypeList{});
}

} // Vt_KnownValueTypeDetail

// Total number of 'known' value types.
constexpr int
VtGetNumKnownValueTypes() {
return BOOST_PP_SEQ_SIZE(VT_VALUE_TYPES);
return TfMetaApply<TfMetaLength, Vt_ValueTypeList>::value;
}

/// Provide compile-time value type indexes for types that are "known" to Vt --
Expand Down
6 changes: 3 additions & 3 deletions pxr/base/vt/visitValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ auto VtVisitValue(VtValue const &value, Visitor &&visitor)
switch (value.GetKnownValueTypeIndex()) {

// Cases for known types.
#define VT_CASE_FOR_TYPE_INDEX(r, unused, i, elem) \
case i: \
#define VT_CASE_FOR_TYPE_INDEX(r, unused, elem) \
case VtGetKnownValueTypeIndex<VT_TYPE(elem)>(): \
return Vt_ValueVisitDetail::Visit<VT_TYPE(elem)>( \
value, std::forward<Visitor>(visitor), 0); \
break;
BOOST_PP_SEQ_FOR_EACH_I(VT_CASE_FOR_TYPE_INDEX, ~, VT_VALUE_TYPES)
BOOST_PP_SEQ_FOR_EACH(VT_CASE_FOR_TYPE_INDEX, ~, VT_VALUE_TYPES)
#undef VT_CASE_FOR_TYPE_INDEX

default:
Expand Down

0 comments on commit b0d709b

Please sign in to comment.