Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace BOOST_PP_SEQ_FOR_EACH_I with constexpr expressions in vt #2831

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading