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