HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
valueCommon.h
Go to the documentation of this file.
1 //
2 // Copyright 2025 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_VALUE_COMMON_H
8 #define PXR_BASE_VT_VALUE_COMMON_H
9 
10 #include "pxr/pxr.h"
11 
14 #include "pxr/base/tf/tf.h"
15 
16 #include "pxr/base/vt/api.h"
17 #include "pxr/base/vt/traits.h"
18 #include "pxr/base/vt/types.h"
19 
20 #include <typeinfo>
21 #include <type_traits>
22 
24 
25 /// Make a default value.
26 /// VtValue uses this to create values to be returned from failed calls to \a
27 /// Get. Clients may specialize this for their own types.
28 template <class T>
30 
31 // This is a helper class used by Vt_DefaultValueFactory to return a value with
32 // its type erased and only known at runtime via a std::type_info.
34 {
35  // Creates a value-initialized object and stores the type_info for the
36  // static type.
37  template <typename T>
39  return Vt_DefaultValueHolder(TfAnyUniquePtr::New<T>(), typeid(T));
40  }
41 
42  // Creates a copy of the object and stores the type_info for the static
43  // type.
44  template <typename T>
45  static Vt_DefaultValueHolder Create(T const &val) {
46  return Vt_DefaultValueHolder(TfAnyUniquePtr::New(val), typeid(T));
47  }
48 
49  // Return the runtime type of the held object.
50  std::type_info const &GetType() const {
51  return *_type;
52  }
53 
54  // Return a pointer to the held object. This may be safely cast to the
55  // static type corresponding to the type_info returned by GetType.
56  void const *GetPointer() const {
57  return _ptr.Get();
58  }
59 
60 private:
61  Vt_DefaultValueHolder(TfAnyUniquePtr &&ptr, std::type_info const &type)
62  : _ptr(std::move(ptr)), _type(&type) {}
63 
64  TfAnyUniquePtr _ptr;
65  std::type_info const *_type;
66 };
67 
68 void const *
69 Vt_FindOrCreateDefaultValue(std::type_info const &type,
70  Vt_DefaultValueHolder (*factory)());
71 
72 #define VT_VALUE_SET_STORED_TYPE(SRC, DST) \
73  template <> struct Vt_ValueStoredType<SRC> { typedef DST Type; }
74 
75 template <class T> struct Vt_ValueStoredType { typedef T Type; };
76 VT_VALUE_SET_STORED_TYPE(char const *, std::string);
77 VT_VALUE_SET_STORED_TYPE(char *, std::string);
78 
79 template <size_t N>
80 struct Vt_ValueStoredType<char [N]> {
81  using Type = std::string;
82 };
83 
84 #ifdef PXR_PYTHON_SUPPORT_ENABLED
86 #endif // PXR_PYTHON_SUPPORT_ENABLED
87 
88 #undef VT_VALUE_SET_STORED_TYPE
89 
90 // A metafunction that gives the type VtValue should store for a given type T.
91 template <class T>
93 
94 #ifndef doxygen
95 
96 /// Make a default value. VtValue uses this to create values to be returned
97 /// from failed calls to \a Get. Clients may specialize this for their own
98 /// types.
99 template <class T>
100 struct Vt_DefaultValueFactory {
101  static Vt_DefaultValueHolder Invoke();
102 };
103 
104 template <class T>
107  return Vt_DefaultValueHolder::Create<T>();
108 }
109 
110 // For performance reasons, the default constructors for vectors,
111 // matrices, and quaternions do *not* initialize the data of the
112 // object. This greatly improves the performance of creating large
113 // arrays of objects. However, for consistency and to avoid
114 // errors complaining about uninitialized values, we use VtZero
115 // to construct zeroed out vectors, matrices, and quaternions by
116 // explicitly instantiating the factory for these types.
117 //
118 #define _VT_DECLARE_ZERO_VALUE_FACTORY(unused, elem) \
119 template <> \
120 VT_API Vt_DefaultValueHolder Vt_DefaultValueFactory<VT_TYPE(elem)>::Invoke();
121 
127 
128 #undef _VT_DECLARE_ZERO_VALUE_FACTORY
129 
130 #endif // !doxygen
131 
133 
134 #endif // PXR_BASE_VT_VALUE_COMMON_H
static Vt_DefaultValueHolder Create(T const &val)
Definition: valueCommon.h:45
void const * Vt_FindOrCreateDefaultValue(std::type_info const &type, Vt_DefaultValueHolder(*factory)())
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
#define VT_DUALQUATERNION_VALUE_TYPES
Definition: types.h:114
#define VT_VALUE_SET_STORED_TYPE(SRC, DST)
Definition: valueCommon.h:72
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
#define VT_VEC_VALUE_TYPES
Definition: types.h:71
void const * GetPointer() const
Definition: valueCommon.h:56
#define TF_PP_SEQ_FOR_EACH(_macro, data, seq)
std::type_info const & GetType() const
Definition: valueCommon.h:50
static Vt_DefaultValueHolder Create()
Definition: valueCommon.h:38
auto ptr(T p) -> const void *
Definition: format.h:4331
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
GA_API const UT_StringHolder N
#define _VT_DECLARE_ZERO_VALUE_FACTORY(unused, elem)
Definition: valueCommon.h:118
static TfAnyUniquePtr New()
Definition: anyUniquePtr.h:30
#define VT_QUATERNION_VALUE_TYPES
Definition: types.h:108
static Vt_DefaultValueHolder Invoke()
Definition: valueCommon.h:106
#define VT_MATRIX_VALUE_TYPES
Definition: types.h:87
void const * Get() const
Return a pointer to the owned object.
Definition: anyUniquePtr.h:69