HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
types.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_VT_TYPES_H
8 #define PXR_BASE_VT_TYPES_H
9 
10 /// \file vt/types.h
11 /// Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
12 
13 #include "pxr/pxr.h"
14 #include "pxr/base/vt/api.h"
15 #include "pxr/base/vt/traits.h"
16 #include "pxr/base/arch/inttypes.h"
17 #include "pxr/base/gf/declare.h"
18 #include "pxr/base/gf/half.h"
19 #include "pxr/base/tf/meta.h"
21 #include "pxr/base/tf/token.h"
22 
23 #include <cstddef>
24 #include <cstring>
25 #include <string>
26 
28 
29 // Help ensure TfToken is stored in local storage in VtValue by indicating it is
30 // cheap to copy (just refcount operations).
32 
33 // Value types.
34 
35 #define VT_FLOATING_POINT_BUILTIN_VALUE_TYPES \
36 (( double, Double )) \
37 (( float, Float )) \
38 (( GfHalf, Half ))
39 
40 #define VT_INTEGRAL_BUILTIN_VALUE_TYPES \
41 (( bool, Bool )) \
42 (( char, Char )) \
43 (( unsigned char, UChar )) \
44 (( short, Short )) \
45 (( unsigned short, UShort )) \
46 (( int, Int )) \
47 (( unsigned int, UInt )) \
48 (( int64_t, Int64 )) \
49 (( uint64_t, UInt64 ))
50 
51 #define VT_VEC_INT_VALUE_TYPES \
52 (( GfVec4i, Vec4i )) \
53 (( GfVec3i, Vec3i )) \
54 (( GfVec2i, Vec2i ))
55 
56 #define VT_VEC_HALF_VALUE_TYPES \
57 (( GfVec4h, Vec4h )) \
58 (( GfVec3h, Vec3h )) \
59 (( GfVec2h, Vec2h ))
60 
61 #define VT_VEC_FLOAT_VALUE_TYPES \
62 (( GfVec4f, Vec4f )) \
63 (( GfVec3f, Vec3f )) \
64 (( GfVec2f, Vec2f ))
65 
66 #define VT_VEC_DOUBLE_VALUE_TYPES \
67 (( GfVec4d, Vec4d )) \
68 (( GfVec3d, Vec3d )) \
69 (( GfVec2d, Vec2d ))
70 
71 #define VT_VEC_VALUE_TYPES \
72  VT_VEC_INT_VALUE_TYPES \
73  VT_VEC_HALF_VALUE_TYPES \
74  VT_VEC_FLOAT_VALUE_TYPES \
75  VT_VEC_DOUBLE_VALUE_TYPES
76 
77 #define VT_MATRIX_FLOAT_VALUE_TYPES \
78 (( GfMatrix4f, Matrix4f )) \
79 (( GfMatrix3f, Matrix3f )) \
80 (( GfMatrix2f, Matrix2f )) \
81 
82 #define VT_MATRIX_DOUBLE_VALUE_TYPES \
83 (( GfMatrix4d, Matrix4d )) \
84 (( GfMatrix3d, Matrix3d )) \
85 (( GfMatrix2d, Matrix2d ))
86 
87 #define VT_MATRIX_VALUE_TYPES \
88  VT_MATRIX_FLOAT_VALUE_TYPES \
89  VT_MATRIX_DOUBLE_VALUE_TYPES \
90 
91 #define VT_GFRANGE_VALUE_TYPES \
92 (( GfRange3f, Range3f )) \
93 (( GfRange3d, Range3d )) \
94 (( GfRange2f, Range2f )) \
95 (( GfRange2d, Range2d )) \
96 (( GfRange1f, Range1f )) \
97 (( GfRange1d, Range1d ))
98 
99 #define VT_RANGE_VALUE_TYPES \
100  VT_GFRANGE_VALUE_TYPES \
101 (( GfInterval, Interval )) \
102 (( GfRect2i, Rect2i ))
103 
104 #define VT_STRING_VALUE_TYPES \
105 (( std::string, String )) \
106 (( TfToken, Token ))
107 
108 #define VT_QUATERNION_VALUE_TYPES \
109 (( GfQuath, Quath )) \
110 (( GfQuatf, Quatf )) \
111 (( GfQuatd, Quatd )) \
112 (( GfQuaternion, Quaternion ))
113 
114 #define VT_DUALQUATERNION_VALUE_TYPES \
115 (( GfDualQuath, DualQuath )) \
116 (( GfDualQuatf, DualQuatf )) \
117 (( GfDualQuatd, DualQuatd ))
118 
119 #define VT_NONARRAY_VALUE_TYPES \
120 (( GfFrustum, Frustum)) \
121 (( GfMultiInterval, MultiInterval))
122 
123 // Helper macros for extracting bits from a type tuple.
124 #define VT_TYPE(elem) \
125 TF_PP_TUPLE_ELEM(0, elem)
126 #define VT_TYPE_NAME(elem) \
127 TF_PP_TUPLE_ELEM(1, elem)
128 
129 
130 // Composite groups of types.
131 #define VT_BUILTIN_NUMERIC_VALUE_TYPES \
132 VT_INTEGRAL_BUILTIN_VALUE_TYPES VT_FLOATING_POINT_BUILTIN_VALUE_TYPES
133 
134 #define VT_BUILTIN_VALUE_TYPES \
135 VT_BUILTIN_NUMERIC_VALUE_TYPES VT_STRING_VALUE_TYPES
136 
137 #define VT_SCALAR_CLASS_VALUE_TYPES \
138 VT_VEC_VALUE_TYPES \
139 VT_MATRIX_VALUE_TYPES \
140 VT_RANGE_VALUE_TYPES \
141 VT_QUATERNION_VALUE_TYPES \
142 VT_DUALQUATERNION_VALUE_TYPES
143 
144 #define VT_SCALAR_VALUE_TYPES \
145 VT_SCALAR_CLASS_VALUE_TYPES VT_BUILTIN_VALUE_TYPES
146 
147 
148 // The following preprocessor code produces typedefs for VtArray holding
149 // various scalar value types. The produced typedefs are of the form:
150 //
151 // typedef VtArray<int> VtIntArray;
152 // typedef VtArray<double> VtDoubleArray;
153 template<typename T> class VtArray;
154 #define VT_ARRAY_TYPEDEF(unused, elem) \
155 typedef VtArray< VT_TYPE(elem) > \
156 TF_PP_CAT(Vt, TF_PP_CAT(VT_TYPE_NAME(elem), Array)) ;
158 
159 // The following preprocessor code generates the boost pp sequence for
160 // all array value types (VT_ARRAY_VALUE_TYPES)
161 #define VT_ARRAY_TYPE_TUPLE(unused, elem) \
162 (( TF_PP_CAT(Vt, TF_PP_CAT(VT_TYPE_NAME(elem), Array)) , \
163  TF_PP_CAT(VT_TYPE_NAME(elem), Array) ))
164 #define VT_ARRAY_VALUE_TYPES \
165 TF_PP_SEQ_FOR_EACH(VT_ARRAY_TYPE_TUPLE, ~, VT_SCALAR_VALUE_TYPES)
166 
167 #define VT_CLASS_VALUE_TYPES \
168 VT_ARRAY_VALUE_TYPES VT_SCALAR_CLASS_VALUE_TYPES VT_NONARRAY_VALUE_TYPES
169 
170 #define VT_VALUE_TYPES \
171  VT_BUILTIN_VALUE_TYPES VT_CLASS_VALUE_TYPES
172 
173 #define _VT_MAP_TYPE_LIST(unused, elem) , VT_TYPE(elem)
174 
175 // Populate a type list from the preprocessor sequence.
176 // void is prepended to match the comma for the first type
177 // and then dropped by TfMetaTail.
178 using Vt_ValueTypeList =
181 
182 namespace Vt_KnownValueTypeDetail
183 {
184 
185 // Implement compile-time value type indexes.
186 // Base case -- unknown types get index -1.
187 template <typename T>
188 constexpr int
190  return -1;
191 }
192 
193 template <typename T, typename Typelist>
194 constexpr int
195 GetIndexImpl(Typelist) {
196  if (std::is_same_v<T, TfMetaApply<TfMetaHead, Typelist>>) {
197  return 0;
198  }
199  else if (const int indexOfTail =
200  GetIndexImpl<T>(TfMetaApply<TfMetaTail, Typelist>{});
201  indexOfTail >= 0) {
202  return 1 + indexOfTail;
203  }
204  else {
205  return -1;
206  }
207 }
208 
209 template <typename T>
210 constexpr int
212  return GetIndexImpl<T>(Vt_ValueTypeList{});
213 }
214 
215 } // Vt_KnownValueTypeDetail
216 
217 // Total number of 'known' value types.
218 constexpr int
221 }
222 
223 /// Provide compile-time value type indexes for types that are "known" to Vt --
224 /// specifically, those types that appear in VT_VALUE_TYPES. Note that VtArray
225 /// and VtValue can work with other types that are not these "known" types.
226 ///
227 /// VtGetKnownValueTypeIndex can only be used with known types. Querying a
228 /// type that is not known to Vt results in a compilation error. The set of
229 /// known types and their indexes are not guaranteed to be stable across
230 /// releases of the library.
231 ///
232 /// Most clients should prefer VtVisitValue over direct use of the type index
233 /// as VtVisitValue provides convenient and efficient access to the held
234 /// value.
235 template <class T>
236 constexpr int
238 {
239  constexpr int index = Vt_KnownValueTypeDetail::GetIndex<T>();
240  static_assert(index != -1, "T is not one of the known VT_VALUE_TYPES.");
241  return index;
242 }
243 
244 /// Returns true if `T` is a type that appears in VT_VALUE_TYPES.
245 template <class T>
246 constexpr bool
248 {
249  return Vt_KnownValueTypeDetail::GetIndex<T>() != -1;
250 }
251 
252 // XXX: Works around an MSVC bug where constexpr functions cannot be used as the
253 // condition in enable_if, fixed in MSVC 2022 version 14.33 1933 (version 17.3).
254 // https://developercommunity.visualstudio.com/t/function-template-has-already-been-defined-using-s/833543
255 template <class T>
257 {
258  static const bool value = VtIsKnownValueType<T>();
259 };
260 
261 // None of the VT_VALUE_TYPES are value proxies. We want to specialize these
262 // templates here, since otherwise the VtIsTypedValueProxy will require a
263 // complete type to check if it derives VtTypedValueProxyBase.
264 #define VT_SPECIALIZE_IS_VALUE_PROXY(unused, elem) \
265  template <> struct \
266  VtIsValueProxy< VT_TYPE(elem) > : std::false_type {}; \
267  template <> struct \
268  VtIsTypedValueProxy< VT_TYPE(elem) > : std::false_type {}; \
269  template <> struct \
270  VtIsErasedValueProxy< VT_TYPE(elem) > : std::false_type {};
272 #undef VT_SPECIALIZE_IS_VALUE_PROXY
273 
274 // Free functions to represent "zero" for various base types. See
275 // specializations in Types.cpp
276 template<typename T>
277 T VtZero();
278 
279 // Shape representation used in VtArray for legacy code. This is not supported
280 // at the pxr level or in usd. Shape is represented by a total size, plus sized
281 // dimensions other than the last. The size of the last dimension is computed
282 // as totalSize / (product-of-other-dimensions).
283 struct Vt_ShapeData {
284  unsigned int GetRank() const {
285  return
286  otherDims[0] == 0 ? 1 :
287  otherDims[1] == 0 ? 2 :
288  otherDims[2] == 0 ? 3 : 4;
289  }
290  bool operator==(Vt_ShapeData const &other) const {
291  if (totalSize != other.totalSize)
292  return false;
293  unsigned int thisRank = GetRank(), otherRank = other.GetRank();
294  if (thisRank != otherRank)
295  return false;
296  return std::equal(otherDims, otherDims + GetRank() - 1,
297  other.otherDims);
298  }
299  bool operator!=(Vt_ShapeData const &other) const {
300  return !(*this == other);
301  }
302  void clear() {
303  memset(this, 0, sizeof(*this));
304  }
305  static const int NumOtherDims = 3;
306  size_t totalSize;
307  unsigned int otherDims[NumOtherDims];
308 };
309 
311 
312 #endif // PXR_BASE_VT_TYPES_H
void clear()
Definition: types.h:302
#define VT_VALUE_TYPES
Definition: types.h:170
bool operator!=(Vt_ShapeData const &other) const
Definition: types.h:299
GLsizei const GLfloat * value
Definition: glcorearb.h:824
PXR_NAMESPACE_OPEN_SCOPE VT_TYPE_IS_CHEAP_TO_COPY(TfToken)
unsigned int GetRank() const
Definition: types.h:284
IMATH_HOSTDEVICE constexpr bool equal(T1 a, T2 b, T3 t) IMATH_NOEXCEPT
Definition: ImathFun.h:105
#define VT_SPECIALIZE_IS_VALUE_PROXY(unused, elem)
Definition: types.h:264
TF_PP_SEQ_FOR_EACH(_TS_SUPPORT_DATA_TYPE,~, TS_SPLINE_SUPPORTED_VALUE_TYPES) template< class T > inline const expr bool TsSplineIsValidSampleType
True if template parameter T is a supported spline sampling vertex type.
size_t totalSize
Definition: types.h:306
Definition: token.h:70
#define VT_SCALAR_VALUE_TYPES
Definition: types.h:144
constexpr int VtGetKnownValueTypeIndex()
Definition: types.h:237
Definition: types.h:153
constexpr bool VtIsKnownValueType()
Returns true if T is a type that appears in VT_VALUE_TYPES.
Definition: types.h:247
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define _VT_MAP_TYPE_LIST(unused, elem)
Definition: types.h:173
unsigned int otherDims[NumOtherDims]
Definition: types.h:307
GLuint index
Definition: glcorearb.h:786
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
static const int NumOtherDims
Definition: types.h:305
constexpr int VtGetNumKnownValueTypes()
Definition: types.h:219
constexpr int GetIndexImpl(TfMetaList<>)
Definition: types.h:189
TfMetaList< Tail...> TfMetaTail
Definition: meta.h:44
T VtZero()
constexpr int GetIndex()
Definition: types.h:211
bool operator==(Vt_ShapeData const &other) const
Definition: types.h:290
TfMetaApply< TfMetaTail, TfMetaList< void TF_PP_SEQ_FOR_EACH(_VT_MAP_TYPE_LIST,~, VT_VALUE_TYPES)>> Vt_ValueTypeList
Definition: types.h:180
#define VT_ARRAY_TYPEDEF(unused, elem)
Definition: types.h:154
typename Tf_MetaApplyImpl< Cls, TypeList >::Type TfMetaApply
Definition: meta.h:36