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"
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 <boost/preprocessor/cat.hpp>
39 #include <boost/preprocessor/seq/for_each.hpp>
40 #include <boost/preprocessor/seq/for_each_i.hpp>
41 #include <boost/preprocessor/tuple/elem.hpp>
42 
43 #include <cstddef>
44 #include <cstring>
45 #include <string>
46 
47 PXR_NAMESPACE_OPEN_SCOPE
48 
49 // Help ensure TfToken is stored in local storage in VtValue by indicating it is
50 // cheap to copy (just refcount operations).
51 VT_TYPE_IS_CHEAP_TO_COPY(TfToken);
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 BOOST_PP_TUPLE_ELEM(2, 0, elem)
146 #define VT_TYPE_NAME(elem) \
147 BOOST_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 BOOST_PP_CAT(Vt, BOOST_PP_CAT(VT_TYPE_NAME(elem), Array)) ;
177 BOOST_PP_SEQ_FOR_EACH(VT_ARRAY_TYPEDEF, ~, VT_SCALAR_VALUE_TYPES)
178 
179 // The following preprocessor code generates the boost pp sequence for
180 // all array value types (VT_ARRAY_VALUE_TYPES)
181 #define VT_ARRAY_TYPE_TUPLE(r, unused, elem) \
182 (( BOOST_PP_CAT(Vt, BOOST_PP_CAT(VT_TYPE_NAME(elem), Array)) , \
183  BOOST_PP_CAT(VT_TYPE_NAME(elem), Array) ))
184 #define VT_ARRAY_VALUE_TYPES \
185 BOOST_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
201 GetIndex() {
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 BOOST_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
218 VtGetNumKnownValueTypes() {
219  return BOOST_PP_SEQ_SIZE(VT_VALUE_TYPES);
220 }
221 
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 
244 template <class T>
245 constexpr bool
247 {
248  return Vt_KnownValueTypeDetail::GetIndex<T>() != -1;
249 }
250 
251 // None of the VT_VALUE_TYPES are value proxies. We want to specialize these
252 // templates here, since otherwise the VtIsTypedValueProxy will require a
253 // complete type to check if it derives VtTypedValueProxyBase.
254 #define VT_SPECIALIZE_IS_VALUE_PROXY(r, unused, elem) \
255  template <> struct \
256  VtIsValueProxy< VT_TYPE(elem) > : std::false_type {}; \
257  template <> struct \
258  VtIsTypedValueProxy< VT_TYPE(elem) > : std::false_type {}; \
259  template <> struct \
260  VtIsErasedValueProxy< VT_TYPE(elem) > : std::false_type {};
261 BOOST_PP_SEQ_FOR_EACH(VT_SPECIALIZE_IS_VALUE_PROXY, ~, VT_VALUE_TYPES)
262 #undef VT_SPECIALIZE_IS_VALUE_PROXY
263 
264 // Free functions to represent "zero" for various base types. See
265 // specializations in Types.cpp
266 template<typename T>
267 T VtZero();
268 
269 // Shape representation used in VtArray for legacy code. This is not supported
270 // at the pxr level or in usd. Shape is represented by a total size, plus sized
271 // dimensions other than the last. The size of the last dimension is computed
272 // as totalSize / (product-of-other-dimensions).
273 struct Vt_ShapeData {
274  unsigned int GetRank() const {
275  return
276  otherDims[0] == 0 ? 1 :
277  otherDims[1] == 0 ? 2 :
278  otherDims[2] == 0 ? 3 : 4;
279  }
280  bool operator==(Vt_ShapeData const &other) const {
281  if (totalSize != other.totalSize)
282  return false;
283  unsigned int thisRank = GetRank(), otherRank = other.GetRank();
284  if (thisRank != otherRank)
285  return false;
286  return std::equal(otherDims, otherDims + GetRank() - 1,
287  other.otherDims);
288  }
289  bool operator!=(Vt_ShapeData const &other) const {
290  return !(*this == other);
291  }
292  void clear() {
293  memset(this, 0, sizeof(*this));
294  }
295  static const int NumOtherDims = 3;
296  size_t totalSize;
297  unsigned int otherDims[NumOtherDims];
298 };
299 
300 PXR_NAMESPACE_CLOSE_SCOPE
301 
302 #endif // PXR_BASE_VT_TYPES_H
This header serves to simply bring in the half float datatype and provide a hash_value function.
Declares Gf types.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
constexpr int VtGetKnownValueTypeIndex()
Provide compile-time value type indexes for types that are "known" to Vt – specifically,...
Definition: types.h:236
Represents an arbitrary dimensional rectangular container class.
Definition: array.h:228
constexpr bool VtIsKnownValueType()
Returns true if T is a type that appears in VT_VALUE_TYPES.
Definition: types.h:246
Define integral types.
VT_API bool operator==(VtDictionary const &, VtDictionary const &)
Equality comparison.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...