Loading...
Searching...
No Matches
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
12
13#include "pxr/pxr.h"
14#include "pxr/base/vt/api.h"
15#include "pxr/base/vt/traits.h"
17#include "pxr/base/gf/declare.h"
18#include "pxr/base/gf/half.h"
19#include "pxr/base/tf/meta.h"
20#include "pxr/base/tf/preprocessorUtilsLite.h"
21#include "pxr/base/tf/token.h"
22
23#include <cstddef>
24#include <cstring>
25#include <string>
26
27PXR_NAMESPACE_OPEN_SCOPE
28
29// Help ensure TfToken is stored in local storage in VtValue by indicating it is
30// cheap to copy (just refcount operations).
31VT_TYPE_IS_CHEAP_TO_COPY(TfToken);
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) \
125TF_PP_TUPLE_ELEM(0, elem)
126#define VT_TYPE_NAME(elem) \
127TF_PP_TUPLE_ELEM(1, elem)
128
129
130// Composite groups of types.
131#define VT_BUILTIN_NUMERIC_VALUE_TYPES \
132VT_INTEGRAL_BUILTIN_VALUE_TYPES VT_FLOATING_POINT_BUILTIN_VALUE_TYPES
133
134#define VT_BUILTIN_VALUE_TYPES \
135VT_BUILTIN_NUMERIC_VALUE_TYPES VT_STRING_VALUE_TYPES
136
137#define VT_SCALAR_CLASS_VALUE_TYPES \
138VT_VEC_VALUE_TYPES \
139VT_MATRIX_VALUE_TYPES \
140VT_RANGE_VALUE_TYPES \
141VT_QUATERNION_VALUE_TYPES \
142VT_DUALQUATERNION_VALUE_TYPES
143
144#define VT_SCALAR_VALUE_TYPES \
145VT_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;
153template<typename T> class VtArray;
154#define VT_ARRAY_TYPEDEF(unused, elem) \
155typedef VtArray< VT_TYPE(elem) > \
156TF_PP_CAT(Vt, TF_PP_CAT(VT_TYPE_NAME(elem), Array)) ;
157TF_PP_SEQ_FOR_EACH(VT_ARRAY_TYPEDEF, ~, VT_SCALAR_VALUE_TYPES)
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 \
165TF_PP_SEQ_FOR_EACH(VT_ARRAY_TYPE_TUPLE, ~, VT_SCALAR_VALUE_TYPES)
166
167#define VT_CLASS_VALUE_TYPES \
168VT_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.
178using Vt_ValueTypeList =
179 TfMetaApply<TfMetaTail, TfMetaList<
180 void TF_PP_SEQ_FOR_EACH(_VT_MAP_TYPE_LIST, ~, VT_VALUE_TYPES)>>;
181
182namespace Vt_KnownValueTypeDetail
183{
184
185// Implement compile-time value type indexes.
186// Base case -- unknown types get index -1.
187template <typename T>
188constexpr int
189GetIndexImpl(TfMetaList<>) {
190 return -1;
191}
192
193template <typename T, typename Typelist>
194constexpr int
195GetIndexImpl(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
209template <typename T>
210constexpr int
211GetIndex() {
212 return GetIndexImpl<T>(Vt_ValueTypeList{});
213}
214
215} // Vt_KnownValueTypeDetail
216
217// Total number of 'known' value types.
218constexpr int
219VtGetNumKnownValueTypes() {
220 return TfMetaApply<TfMetaLength, Vt_ValueTypeList>::value;
221}
222
235template <class T>
236constexpr 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
245template <class T>
246constexpr 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
255template <class T>
256struct VtIsKnownValueType_Workaround
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 {};
271TF_PP_SEQ_FOR_EACH(VT_SPECIALIZE_IS_VALUE_PROXY, ~, VT_VALUE_TYPES)
272#undef VT_SPECIALIZE_IS_VALUE_PROXY
273
274// Free functions to represent "zero" for various base types. See
275// specializations in Types.cpp
276template<typename T>
277T 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).
283struct 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
310PXR_NAMESPACE_CLOSE_SCOPE
311
312#endif // PXR_BASE_VT_TYPES_H
Declares Gf types.
constexpr int VtGetKnownValueTypeIndex()
Provide compile-time value type indexes for types that are "known" to Vt – specifically,...
Definition: types.h:237
constexpr bool VtIsKnownValueType()
Returns true if T is a type that appears in VT_VALUE_TYPES.
Definition: types.h:247
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:211
This header serves to simply bring in the half float datatype and provide a hash_value function.
Define integral types.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...