Loading...
Searching...
No Matches
valueCommon.h
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
12#include "pxr/base/tf/anyUniquePtr.h"
13#include "pxr/base/tf/preprocessorUtilsLite.h"
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
23PXR_NAMESPACE_OPEN_SCOPE
24
28template <class T>
29struct Vt_DefaultValueFactory;
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.
33struct Vt_DefaultValueHolder
34{
35 // Creates a value-initialized object and stores the type_info for the
36 // static type.
37 template <typename T>
38 static Vt_DefaultValueHolder Create() {
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
60private:
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
68void const *
69Vt_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
75template <class T> struct Vt_ValueStoredType { typedef T Type; };
76VT_VALUE_SET_STORED_TYPE(char const *, std::string);
77VT_VALUE_SET_STORED_TYPE(char *, std::string);
78
79template <size_t N>
80struct Vt_ValueStoredType<char [N]> {
81 using Type = std::string;
82};
83
84#ifdef PXR_PYTHON_SUPPORT_ENABLED
85VT_VALUE_SET_STORED_TYPE(pxr_boost::python::object, TfPyObjWrapper);
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.
91template <class T>
92using Vt_ValueGetStored = Vt_ValueStoredType<std::decay_t<T>>;
93
94#ifndef doxygen
95
99template <class T>
100struct Vt_DefaultValueFactory {
101 static Vt_DefaultValueHolder Invoke();
102};
103
104template <class T>
105inline Vt_DefaultValueHolder
106Vt_DefaultValueFactory<T>::Invoke() {
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) \
119template <> \
120VT_API Vt_DefaultValueHolder Vt_DefaultValueFactory<VT_TYPE(elem)>::Invoke();
121
122TF_PP_SEQ_FOR_EACH(_VT_DECLARE_ZERO_VALUE_FACTORY, ~,
123 VT_VEC_VALUE_TYPES
124 VT_MATRIX_VALUE_TYPES
125 VT_QUATERNION_VALUE_TYPES
126 VT_DUALQUATERNION_VALUE_TYPES)
127
128#undef _VT_DECLARE_ZERO_VALUE_FACTORY
129
130#endif // !doxygen
131
132PXR_NAMESPACE_CLOSE_SCOPE
133
134#endif // PXR_BASE_VT_VALUE_COMMON_H
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
A simple type-erased container that provides only destruction, moves and immutable,...
Definition: anyUniquePtr.h:27
Boost Python object wrapper.
Definition: pyObjWrapper.h:79
STL namespace.
A file containing basic constants and definitions.