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