Loading...
Searching...
No Matches
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
29
30#include "pxr/pxr.h"
31#include "pxr/base/vt/api.h"
32#include "pxr/base/vt/traits.h"
34#include "pxr/base/gf/declare.h"
35#include "pxr/base/gf/half.h"
36#include "pxr/base/tf/meta.h"
37#include "pxr/base/tf/preprocessorUtilsLite.h"
38#include "pxr/base/tf/token.h"
39
40#include <cstddef>
41#include <cstring>
42#include <string>
43
44PXR_NAMESPACE_OPEN_SCOPE
45
46// Help ensure TfToken is stored in local storage in VtValue by indicating it is
47// cheap to copy (just refcount operations).
48VT_TYPE_IS_CHEAP_TO_COPY(TfToken);
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) \
142TF_PP_TUPLE_ELEM(0, elem)
143#define VT_TYPE_NAME(elem) \
144TF_PP_TUPLE_ELEM(1, elem)
145
146
147// Composite groups of types.
148#define VT_BUILTIN_NUMERIC_VALUE_TYPES \
149VT_INTEGRAL_BUILTIN_VALUE_TYPES VT_FLOATING_POINT_BUILTIN_VALUE_TYPES
150
151#define VT_BUILTIN_VALUE_TYPES \
152VT_BUILTIN_NUMERIC_VALUE_TYPES VT_STRING_VALUE_TYPES
153
154#define VT_SCALAR_CLASS_VALUE_TYPES \
155VT_VEC_VALUE_TYPES \
156VT_MATRIX_VALUE_TYPES \
157VT_RANGE_VALUE_TYPES \
158VT_QUATERNION_VALUE_TYPES \
159VT_DUALQUATERNION_VALUE_TYPES
160
161#define VT_SCALAR_VALUE_TYPES \
162VT_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;
170template<typename T> class VtArray;
171#define VT_ARRAY_TYPEDEF(unused, elem) \
172typedef VtArray< VT_TYPE(elem) > \
173TF_PP_CAT(Vt, TF_PP_CAT(VT_TYPE_NAME(elem), Array)) ;
174TF_PP_SEQ_FOR_EACH(VT_ARRAY_TYPEDEF, ~, VT_SCALAR_VALUE_TYPES)
175
176// The following preprocessor code generates the boost 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 \
182TF_PP_SEQ_FOR_EACH(VT_ARRAY_TYPE_TUPLE, ~, VT_SCALAR_VALUE_TYPES)
183
184#define VT_CLASS_VALUE_TYPES \
185VT_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.
195using Vt_ValueTypeList =
196 TfMetaApply<TfMetaTail, TfMetaList<
197 void TF_PP_SEQ_FOR_EACH(_VT_MAP_TYPE_LIST, ~, VT_VALUE_TYPES)>>;
198
199namespace Vt_KnownValueTypeDetail
200{
201
202// Implement compile-time value type indexes.
203// Base case -- unknown types get index -1.
204template <typename T>
205constexpr int
206GetIndexImpl(TfMetaList<>) {
207 return -1;
208}
209
210template <typename T, typename Typelist>
211constexpr int
212GetIndexImpl(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
226template <typename T>
227constexpr int
228GetIndex() {
229 return GetIndexImpl<T>(Vt_ValueTypeList{});
230}
231
232} // Vt_KnownValueTypeDetail
233
234// Total number of 'known' value types.
235constexpr int
236VtGetNumKnownValueTypes() {
237 return TfMetaApply<TfMetaLength, Vt_ValueTypeList>::value;
238}
239
252template <class T>
253constexpr 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
262template <class T>
263constexpr 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
272template <class T>
273struct VtIsKnownValueType_Workaround
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 {};
288TF_PP_SEQ_FOR_EACH(VT_SPECIALIZE_IS_VALUE_PROXY, ~, VT_VALUE_TYPES)
289#undef VT_SPECIALIZE_IS_VALUE_PROXY
290
291// Free functions to represent "zero" for various base types. See
292// specializations in Types.cpp
293template<typename T>
294T 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).
300struct 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
327PXR_NAMESPACE_CLOSE_SCOPE
328
329#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:254
constexpr bool VtIsKnownValueType()
Returns true if T is a type that appears in VT_VALUE_TYPES.
Definition: types.h:264
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:88
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:228
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...