24 #ifndef PXR_BASE_VT_VALUE_H
25 #define PXR_BASE_VT_VALUE_H
29 #ifdef PXR_PYTHON_SUPPORT_ENABLED
32 #include "pxr/base/tf/pyObjWrapper.h"
33 #endif // PXR_PYTHON_SUPPORT_ENABLED
35 #include "pxr/base/tf/pyLock.h"
39 #include "pxr/base/tf/anyUniquePtr.h"
40 #include "pxr/base/tf/pointerAndBits.h"
44 #include "pxr/base/tf/type.h"
46 #include "pxr/base/vt/api.h"
47 #include "pxr/base/vt/hash.h"
48 #include "pxr/base/vt/streamOut.h"
52 #include <boost/intrusive_ptr.hpp>
53 #include <boost/type_traits/has_trivial_assign.hpp>
54 #include <boost/type_traits/has_trivial_constructor.hpp>
55 #include <boost/type_traits/has_trivial_copy.hpp>
56 #include <boost/type_traits/has_trivial_destructor.hpp>
60 #include <type_traits>
62 PXR_NAMESPACE_OPEN_SCOPE
68 struct Vt_DefaultValueFactory;
72 struct Vt_DefaultValueHolder
77 static Vt_DefaultValueHolder Create() {
78 return Vt_DefaultValueHolder(TfAnyUniquePtr::New<T>(),
typeid(T));
84 static Vt_DefaultValueHolder Create(T
const &val) {
85 return Vt_DefaultValueHolder(TfAnyUniquePtr::New(val),
typeid(T));
89 std::type_info
const &GetType()
const {
95 void const *GetPointer()
const {
100 Vt_DefaultValueHolder(
TfAnyUniquePtr &&ptr, std::type_info
const &type)
101 : _ptr(std::move(ptr)), _type(&type) {}
104 std::type_info
const *_type;
111 VT_API std::ostream &VtStreamOut(std::vector<VtValue>
const &val, std::ostream &);
113 #define VT_VALUE_SET_STORED_TYPE(SRC, DST) \
114 template <> struct Vt_ValueStoredType<SRC> { typedef DST Type; }
116 template <
class T>
struct Vt_ValueStoredType {
typedef T Type; };
117 VT_VALUE_SET_STORED_TYPE(
char const *, std::string);
118 VT_VALUE_SET_STORED_TYPE(
char *, std::string);
120 #ifdef PXR_PYTHON_SUPPORT_ENABLED
122 #endif // PXR_PYTHON_SUPPORT_ENABLED
124 #undef VT_VALUE_SET_STORED_TYPE
128 struct Vt_ValueGetStored
129 : Vt_ValueStoredType<std::decay_t<T>> {};
170 static const unsigned int _LocalFlag = 1 << 0;
171 static const unsigned int _TrivialCopyFlag = 1 << 1;
172 static const unsigned int _ProxyFlag = 1 << 2;
176 explicit _Counted(T
const &obj) : _obj(obj) {
179 bool IsUnique()
const {
return _refCount == 1; }
180 T
const &
Get()
const {
return _obj; }
181 T &GetMutable() {
return _obj; }
185 mutable std::atomic<int> _refCount;
187 friend inline void intrusive_ptr_add_ref(_Counted
const *d) {
188 d->_refCount.fetch_add(1, std::memory_order_relaxed);
190 friend inline void intrusive_ptr_release(_Counted
const *d) {
191 if (d->_refCount.fetch_sub(1, std::memory_order_release) == 1) {
192 std::atomic_thread_fence(std::memory_order_acquire);
201 static const size_t _MaxLocalSize =
sizeof(
void*);
202 typedef std::aligned_storage<
203 _MaxLocalSize, _MaxLocalSize>::type _Storage;
206 using _IsTriviallyCopyable = std::integral_constant<bool,
207 boost::has_trivial_constructor<T>::value &&
208 boost::has_trivial_copy<T>::value &&
209 boost::has_trivial_assign<T>::value &&
210 boost::has_trivial_destructor<T>::value>;
215 using _UsesLocalStore = std::integral_constant<bool,
216 (
sizeof(T) <=
sizeof(_Storage)) &&
217 VtValueTypeHasCheapCopy<T>::value &&
218 std::is_nothrow_move_constructible<T>::value &&
219 std::is_nothrow_move_assignable<T>::value>;
224 using _CopyInitFunc = void (*)(_Storage
const &, _Storage &);
225 using _DestroyFunc = void (*)(_Storage &);
226 using _MoveFunc = void (*)(_Storage &, _Storage &);
227 using _CanHashFunc = bool (*)(_Storage
const &);
228 using _HashFunc = size_t (*)(_Storage
const &);
229 using _EqualFunc = bool (*)(_Storage
const &, _Storage
const &);
230 using _EqualPtrFunc = bool (*)(_Storage
const &,
void const *);
231 using _MakeMutableFunc = void (*)(_Storage &);
232 #ifdef PXR_PYTHON_SUPPORT_ENABLED
234 #endif // PXR_PYTHON_SUPPORT_ENABLED
235 using _StreamOutFunc =
236 std::ostream & (*)(_Storage
const &, std::ostream &);
237 using _GetTypeidFunc = std::type_info
const & (*)(_Storage
const &);
238 using _IsArrayValuedFunc = bool (*)(_Storage
const &);
239 using _GetElementTypeidFunc =
240 std::type_info
const & (*)(_Storage
const &);
241 using _GetShapeDataFunc =
const Vt_ShapeData* (*)(_Storage
const &);
242 using _GetNumElementsFunc = size_t (*)(_Storage
const &);
243 using _ProxyHoldsTypeFunc = bool (*)(_Storage
const &, std::type_info
const &);
244 using _GetProxiedTypeFunc =
TfType (*)(_Storage
const &);
245 using _GetProxiedTypeidFunc =
246 std::type_info
const & (*)(_Storage
const &);
247 using _GetProxiedObjPtrFunc =
void const *(*)(_Storage
const &);
248 using _GetProxiedAsVtValueFunc =
VtValue (*)(_Storage
const &);
251 constexpr _TypeInfo(
const std::type_info &ti,
252 const std::type_info &elementTi,
256 _CopyInitFunc copyInit,
257 _DestroyFunc destroy,
259 _CanHashFunc canHash,
262 _EqualPtrFunc equalPtr,
263 _MakeMutableFunc makeMutable,
264 #ifdef PXR_PYTHON_SUPPORT_ENABLED
265 _GetPyObjFunc getPyObj,
267 _StreamOutFunc streamOut,
268 _GetTypeidFunc getTypeid,
269 _IsArrayValuedFunc isArrayValued,
270 _GetElementTypeidFunc getElementTypeid,
271 _GetShapeDataFunc getShapeData,
272 _GetNumElementsFunc getNumElements,
273 _ProxyHoldsTypeFunc proxyHoldsType,
274 _GetProxiedTypeFunc getProxiedType,
275 _GetProxiedTypeidFunc getProxiedTypeid,
276 _GetProxiedObjPtrFunc getProxiedObjPtr,
277 _GetProxiedAsVtValueFunc getProxiedAsVtValue)
279 , elementTypeInfo(elementTi)
282 , isHashable(isHashable)
284 , _copyInit(copyInit)
290 , _equalPtr(equalPtr)
291 , _makeMutable(makeMutable)
292 #ifdef PXR_PYTHON_SUPPORT_ENABLED
293 , _getPyObj(getPyObj)
294 #endif // PXR_PYTHON_SUPPORT_ENABLED
295 , _streamOut(streamOut)
296 , _getTypeid(getTypeid)
297 , _isArrayValued(isArrayValued)
298 , _getElementTypeid(getElementTypeid)
299 , _getShapeData(getShapeData)
300 , _getNumElements(getNumElements)
301 , _proxyHoldsType(proxyHoldsType)
302 , _getProxiedType(getProxiedType)
303 , _getProxiedTypeid(getProxiedTypeid)
304 , _getProxiedObjPtr(getProxiedObjPtr)
305 , _getProxiedAsVtValue(getProxiedAsVtValue)
309 void CopyInit(_Storage
const &src, _Storage &dst)
const {
312 void Destroy(_Storage &storage)
const {
315 void Move(_Storage &src, _Storage &dst)
const noexcept {
318 bool CanHash(_Storage
const &storage)
const {
319 return _canHash(storage);
321 size_t Hash(_Storage
const &storage)
const {
322 return _hash(storage);
324 bool Equal(_Storage
const &lhs, _Storage
const &rhs)
const {
325 return _equal(lhs, rhs);
327 bool EqualPtr(_Storage
const &lhs,
void const *rhs)
const {
328 return _equalPtr(lhs, rhs);
330 void MakeMutable(_Storage &storage)
const {
331 _makeMutable(storage);
333 #ifdef PXR_PYTHON_SUPPORT_ENABLED
335 return _getPyObj(storage);
337 #endif // PXR_PYTHON_SUPPORT_ENABLED
338 std::ostream &StreamOut(_Storage
const &storage,
339 std::ostream &out)
const {
340 return _streamOut(storage, out);
343 return _isArrayValued(storage);
346 return _getElementTypeid(storage);
348 std::type_info
const &
GetTypeid(_Storage
const &storage)
const {
349 return _getTypeid(storage);
351 const Vt_ShapeData* GetShapeData(_Storage
const &storage)
const {
352 return _getShapeData(storage);
354 size_t GetNumElements(_Storage
const &storage)
const {
355 return _getNumElements(storage);
357 bool ProxyHoldsType(_Storage
const &storage,
358 std::type_info
const &t)
const {
359 return _proxyHoldsType(storage, t);
361 TfType GetProxiedType(_Storage
const &storage)
const {
362 return _getProxiedType(storage);
364 std::type_info
const &GetProxiedTypeid(_Storage
const &storage)
const {
365 return _getProxiedTypeid(storage);
367 VtValue GetProxiedAsVtValue(_Storage
const &storage)
const {
368 return _getProxiedAsVtValue(storage);
370 void const *GetProxiedObjPtr(_Storage
const &storage)
const {
371 return _getProxiedObjPtr(storage);
374 const std::type_info &typeInfo;
375 const std::type_info &elementTypeInfo;
381 _CopyInitFunc _copyInit;
382 _DestroyFunc _destroy;
384 _CanHashFunc _canHash;
387 _EqualPtrFunc _equalPtr;
388 _MakeMutableFunc _makeMutable;
389 #ifdef PXR_PYTHON_SUPPORT_ENABLED
390 _GetPyObjFunc _getPyObj;
391 #endif // PXR_PYTHON_SUPPORT_ENABLED
392 _StreamOutFunc _streamOut;
393 _GetTypeidFunc _getTypeid;
394 _IsArrayValuedFunc _isArrayValued;
395 _GetElementTypeidFunc _getElementTypeid;
396 _GetShapeDataFunc _getShapeData;
397 _GetNumElementsFunc _getNumElements;
398 _ProxyHoldsTypeFunc _proxyHoldsType;
399 _GetProxiedTypeFunc _getProxiedType;
400 _GetProxiedTypeidFunc _getProxiedTypeid;
401 _GetProxiedObjPtrFunc _getProxiedObjPtr;
402 _GetProxiedAsVtValueFunc _getProxiedAsVtValue;
408 template <
class T,
class Enable=
void>
411 static const Vt_ShapeData* GetShapeData(T
const &) {
return NULL; }
412 static size_t GetNumElements(T
const &) {
return 0; }
417 template <
class Array>
419 Array, typename std::enable_if<VtIsArray<Array>::value>::type>
421 static const Vt_ShapeData* GetShapeData(Array
const &obj) {
422 return obj._GetShapeData();
424 static size_t GetNumElements(Array
const &obj) {
428 return typeid(
typename Array::ElementType);
435 _TypedProxyEqualityImpl(T
const &a, T
const &b,
int) -> decltype(a == b) {
439 template <
class NoEqual>
441 _TypedProxyEqualityImpl(NoEqual
const &a, NoEqual
const &b,
long) {
442 return VtGetProxiedObject(a) == VtGetProxiedObject(b);
447 _ErasedProxyEqualityImpl(T
const &a, T
const &b,
int) -> decltype(a == b) {
451 template <
class NoEqual>
453 _ErasedProxyEqualityImpl(NoEqual
const &a, NoEqual
const &b,
long) {
454 return *VtGetErasedProxiedVtValue(a) == *VtGetErasedProxiedVtValue(b);
458 template <
class T,
class Enable =
void>
461 using ProxiedType =
typename VtGetProxiedType<T>::type;
463 static bool CanHash(T
const &) {
return VtIsHashable<ProxiedType>(); }
464 static size_t Hash(T
const &obj) {
465 return VtHashValue(VtGetProxiedObject(obj));
467 static bool Equal(T
const &a, T
const &b) {
472 return _TypedProxyEqualityImpl(a, b, 0);
474 #ifdef PXR_PYTHON_SUPPORT_ENABLED
476 ProxiedType
const &p = VtGetProxiedObject(obj);
478 return boost::python::api::object(p);
480 #endif //PXR_PYTHON_SUPPORT_ENABLED
481 static std::ostream &StreamOut(T
const &obj, std::ostream &out) {
482 return VtStreamOut(VtGetProxiedObject(obj), out);
484 static Vt_ShapeData
const *GetShapeData(T
const &obj) {
485 return _ArrayHelper<ProxiedType>::GetShapeData(
486 VtGetProxiedObject(obj));
488 static size_t GetNumElements(T
const &obj) {
489 return _ArrayHelper<ProxiedType>::GetNumElements(
490 VtGetProxiedObject(obj));
495 static std::type_info
const &
GetTypeid(T
const &) {
496 return typeid(ProxiedType);
499 return _ArrayHelper<ProxiedType>::GetElementTypeid();
501 static VtValue GetProxiedAsVtValue(T
const &obj) {
502 return VtValue(VtGetProxiedObject(obj));
504 static bool HoldsType(T
const &tp, std::type_info
const &query) {
507 static TfType GetTfType(T
const &tp) {
508 return TfType::Find<ProxiedType>();
510 static void const *GetObjPtr(T
const &tp) {
511 return static_cast<void const *
>(&VtGetProxiedObject(tp));
515 template <
class ErasedProxy>
517 ErasedProxy, typename std::enable_if<
518 VtIsErasedValueProxy<ErasedProxy>::value>::type>
520 static bool CanHash(ErasedProxy
const &proxy) {
521 return VtGetErasedProxiedVtValue(proxy)->CanHash();
523 static size_t Hash(ErasedProxy
const &proxy) {
524 return VtGetErasedProxiedVtValue(proxy)->GetHash();
526 static bool Equal(ErasedProxy
const &a, ErasedProxy
const &b) {
531 return _ErasedProxyEqualityImpl(a, b, 0);
533 #ifdef PXR_PYTHON_SUPPORT_ENABLED
535 VtValue const *val = VtGetErasedProxiedVtValue(obj);
537 return boost::python::api::object(*val);
539 #endif //PXR_PYTHON_SUPPORT_ENABLED
541 static std::ostream &
542 StreamOut(ErasedProxy
const &obj, std::ostream &out) {
543 return VtStreamOut(obj, out);
545 static Vt_ShapeData
const *GetShapeData(ErasedProxy
const &obj) {
546 return VtGetErasedProxiedVtValue(obj)->_GetShapeData();
548 static size_t GetNumElements(ErasedProxy
const &obj) {
549 return VtGetErasedProxiedVtValue(obj)->_GetNumElements();
552 return VtGetErasedProxiedVtValue(obj)->IsArrayValued();
554 static std::type_info
const &
GetTypeid(ErasedProxy
const &obj) {
555 return VtGetErasedProxiedVtValue(obj)->GetTypeid();
558 return VtGetErasedProxiedVtValue(obj)->GetElementTypeid();
560 static VtValue GetProxiedAsVtValue(ErasedProxy
const &ep) {
561 return *VtGetErasedProxiedVtValue(ep);
564 HoldsType(ErasedProxy
const &ep, std::type_info
const &query) {
565 return VtErasedProxyHoldsType(ep, query);
567 static TfType GetTfType(ErasedProxy
const &ep) {
568 return VtGetErasedProxiedTfType(ep);
570 static void const *GetObjPtr(ErasedProxy
const &ep) {
571 VtValue const *val = VtGetErasedProxiedVtValue(ep);
572 return val ? val->_GetProxiedObjPtr() :
nullptr;
579 template <
class T,
class Container,
class Derived>
580 struct _TypeInfoImpl :
public _TypeInfo
582 static const bool IsLocal = _UsesLocalStore<T>::value;
583 static const bool HasTrivialCopy = _IsTriviallyCopyable<T>::value;
584 static const bool IsProxy = VtIsValueProxy<T>::value;
586 using ProxyHelper = _ProxyHelper<T>;
588 using This = _TypeInfoImpl;
590 constexpr _TypeInfoImpl()
591 : _TypeInfo(
typeid(T),
592 _ArrayHelper<T>::GetElementTypeid(),
604 #ifdef PXR_PYTHON_SUPPORT_ENABLED
612 &This::_IsArrayValued,
613 &This::_GetElementTypeid,
614 &This::_GetShapeData,
615 &This::_GetNumElements,
618 &This::_ProxyHoldsType,
619 &This::_GetProxiedType,
620 &This::_GetProxiedTypeid,
621 &This::_GetProxiedObjPtr,
622 &This::_GetProxiedAsVtValue)
627 static T
const &GetObj(_Storage
const &storage) {
628 return Derived::_GetObj(_Container(storage));
631 static T &GetMutableObj(_Storage &storage) {
632 return Derived::_GetMutableObj(_Container(storage));
635 static void CopyInitObj(T
const &objSrc, _Storage &dst) {
636 Derived::_PlaceCopy(&_Container(dst), objSrc);
640 static_assert(
sizeof(Container) <=
sizeof(_Storage),
641 "Container size cannot exceed storage size.");
645 static void _CopyInit(_Storage
const &src, _Storage &dst) {
646 new (&_Container(dst)) Container(_Container(src));
649 static void _Destroy(_Storage &storage) {
650 _Container(storage).~Container();
653 static bool _CanHash(_Storage
const &storage) {
654 return ProxyHelper::CanHash(GetObj(storage));
657 static size_t _Hash(_Storage
const &storage) {
658 return ProxyHelper::Hash(GetObj(storage));
661 static bool _Equal(_Storage
const &lhs, _Storage
const &rhs) {
665 return ProxyHelper::Equal(GetObj(lhs), GetObj(rhs));
668 static bool _EqualPtr(_Storage
const &lhs,
void const *rhs) {
672 return ProxyHelper::Equal(
673 GetObj(lhs), *static_cast<T const *>(rhs));
676 static void _Move(_Storage &src, _Storage &dst) noexcept {
677 new (&_Container(dst)) Container(std::move(_Container(src)));
681 static void _MakeMutable(_Storage &storage) {
682 GetMutableObj(storage);
685 #ifdef PXR_PYTHON_SUPPORT_ENABLED
687 return ProxyHelper::GetPyObj(GetObj(storage));
689 #endif // PXR_PYTHON_SUPPORT_ENABLED
691 static std::ostream &_StreamOut(
692 _Storage
const &storage, std::ostream &out) {
693 return ProxyHelper::StreamOut(GetObj(storage), out);
696 static std::type_info
const &_GetTypeid(_Storage
const &storage) {
697 return ProxyHelper::GetTypeid(GetObj(storage));
700 static bool _IsArrayValued(_Storage
const &storage) {
701 return ProxyHelper::IsArrayValued(GetObj(storage));
704 static std::type_info
const &
705 _GetElementTypeid(_Storage
const &storage) {
706 return ProxyHelper::GetElementTypeid(GetObj(storage));
709 static const Vt_ShapeData* _GetShapeData(_Storage
const &storage) {
710 return ProxyHelper::GetShapeData(GetObj(storage));
713 static size_t _GetNumElements(_Storage
const &storage) {
714 return ProxyHelper::GetNumElements(GetObj(storage));
718 _ProxyHoldsType(_Storage
const &storage, std::type_info
const &t) {
719 return ProxyHelper::HoldsType(GetObj(storage), t);
723 _GetProxiedType(_Storage
const &storage) {
724 return ProxyHelper::GetTfType(GetObj(storage));
727 static std::type_info
const &
728 _GetProxiedTypeid(_Storage
const &storage) {
729 return ProxyHelper::GetTypeid(GetObj(storage));
733 _GetProxiedObjPtr(_Storage
const &storage) {
734 return ProxyHelper::GetObjPtr(GetObj(storage));
738 _GetProxiedAsVtValue(_Storage
const &storage) {
739 return ProxyHelper::GetProxiedAsVtValue(GetObj(storage));
745 static Container &_Container(_Storage &storage) {
747 return *
reinterpret_cast<Container *
>(&storage);
749 static Container
const &_Container(_Storage
const &storage) {
751 return *
reinterpret_cast<Container
const *
>(&storage);
759 struct _LocalTypeInfo : _TypeInfoImpl<
765 constexpr _LocalTypeInfo()
766 : _TypeInfoImpl<T, T, _LocalTypeInfo<T>>()
770 static T &_GetMutableObj(T &obj) {
return obj; }
771 static T
const &_GetObj(T
const &obj) {
return obj; }
773 static void _PlaceCopy(T *dst, T
const &src) {
new (dst) T(src); }
780 struct _RemoteTypeInfo : _TypeInfoImpl<
782 boost::intrusive_ptr<_Counted<T> >,
786 constexpr _RemoteTypeInfo()
788 T, boost::intrusive_ptr<_Counted<T>>, _RemoteTypeInfo<T>>()
791 typedef boost::intrusive_ptr<_Counted<T> > Ptr;
793 static T &_GetMutableObj(Ptr &ptr) {
794 if (!ptr->IsUnique())
795 ptr.reset(
new _Counted<T>(ptr->Get()));
796 return ptr->GetMutable();
798 static T
const &_GetObj(Ptr
const &ptr) {
return ptr->Get(); }
800 static void _PlaceCopy(Ptr *dst, T
const &src) {
801 new (dst) Ptr(
new _Counted<T>(src));
807 struct _TypeInfoFor {
809 typedef std::conditional_t<_UsesLocalStore<T>::value,
811 _RemoteTypeInfo<T>> Type;
816 struct _TypeInfoFor<char[N]> : _TypeInfoFor<std::string> {};
822 typedef typename _TypeInfoFor<T>::Type TI;
824 static constexpr
unsigned int flags =
825 (TI::IsLocal ? _LocalFlag : 0) |
826 (TI::HasTrivialCopy ? _TrivialCopyFlag : 0) |
827 (TI::IsProxy ? _ProxyFlag : 0);
837 friend struct _HoldAside;
839 explicit _HoldAside(
VtValue *val)
840 : info((val->
IsEmpty() || val->_IsLocalAndTriviallyCopyable())
841 ? static_cast<_TypeInfo const *>(NULL) : val->_info.
Get()) {
843 info->Move(val->_storage, storage);
847 info->Destroy(storage);
850 _TypeInfo
const *info;
855 std::is_same<T, typename Vt_ValueGetStored<T>::Type>::value>
856 _Init(T
const &obj) {
857 _info = GetTypeInfo<T>();
858 typedef typename _TypeInfoFor<T>::Type TypeInfo;
859 TypeInfo::CopyInitObj(obj, _storage);
864 !std::is_same<T, typename Vt_ValueGetStored<T>::Type>::value>
865 _Init(T
const &obj) {
866 _Init(
typename Vt_ValueGetStored<T>::Type(obj));
925 if (ARCH_LIKELY(
this != &other))
932 if (ARCH_LIKELY(
this != &other))
941 _TypeInfoFor<T>::Type::IsLocal &&
942 _TypeInfoFor<T>::Type::HasTrivialCopy,
959 !_TypeInfoFor<T>::Type::IsLocal ||
960 !_TypeInfoFor<T>::Type::HasTrivialCopy,
963 _HoldAside tmp(
this);
971 std::string tmp(cstr);
979 return *
this =
const_cast<char const *
>(cstr);
985 if (!
IsEmpty() || !rhs.IsEmpty()) {
1008 std::is_same<T, typename Vt_ValueGetStored<T>::Type>::value>
1010 if (!IsHolding<T>())
1027 std::is_same<T, typename Vt_ValueGetStored<T>::Type>::value>
1030 swap(_GetMutable<T>(), rhs);
1074 VT_API std::type_info
const &
GetTypeid()
const;
1102 typedef Vt_DefaultValueFactory<T> Factory;
1106 if (ARCH_UNLIKELY(!IsHolding<T>())) {
1107 return *(
static_cast<T
const *
>(
1108 _FailGet(Factory::Invoke,
typeid(T))));
1120 return IsHolding<T>() ? UncheckedGet<T>() : def;
1124 template <
typename From,
typename To>
1126 _RegisterCast(
typeid(From),
typeid(To), castFn);
1131 template <
typename From,
typename To>
1133 _RegisterCast(
typeid(From),
typeid(To), _SimpleCast<From, To>);
1138 template <
typename From,
typename To>
1140 RegisterSimpleCast<From, To>();
1141 RegisterSimpleCast<To, From>();
1151 template <
typename T>
1154 return ret.
Cast<T>();
1181 std::type_info
const &to) {
1182 return _CanCast(from, to);
1192 template <
typename T>
1196 return *
this = _PerformCast(
typeid(T), *
this);
1207 return *
this = _PerformCast(other.
GetTypeid(), *
this);
1218 return *
this = _PerformCast(type, *
this);
1224 template <
typename T>
1226 return _CanCast(
GetTypeid(),
typeid(T));
1250 VT_API
size_t GetHash()
const;
1252 friend inline size_t hash_value(
VtValue const &val) {
1257 template <
typename T>
1259 typedef typename Vt_ValueGetStored<T>::Type Stored;
1262 template <
typename T>
1268 template <
typename T>
1270 return !(lhs == rhs);
1272 template <
typename T>
1274 return !(lhs == rhs);
1280 if (empty || rhsEmpty) {
1282 return empty == rhsEmpty;
1286 return _info.
Get()->Equal(_storage, rhs._storage);
1288 return _EqualityImpl(rhs);
1293 VT_API
friend std::ostream &
1297 VT_API
const Vt_ShapeData* _GetShapeData()
const;
1298 VT_API
size_t _GetNumElements()
const;
1299 friend struct Vt_ValueShapeDataAccess;
1307 _HoldAside tmp(&dst);
1308 dst._info = src._info;
1309 if (src._IsLocalAndTriviallyCopyable()) {
1310 dst._storage = src._storage;
1312 dst._info->CopyInit(src._storage, dst._storage);
1322 _HoldAside tmp(&dst);
1323 dst._info = src._info;
1324 if (src._IsLocalAndTriviallyCopyable()) {
1325 dst._storage = src._storage;
1327 dst._info->Move(src._storage, dst._storage);
1330 src._info.
Set(
nullptr, 0);
1334 inline bool _TypeIs()
const {
1335 std::type_info
const &t =
typeid(T);
1337 return ARCH_UNLIKELY(_IsProxy() && !cmp) ? _TypeIsImpl(t) : cmp;
1340 VT_API
bool _TypeIsImpl(std::type_info
const &queriedType)
const;
1342 VT_API
bool _EqualityImpl(
VtValue const &rhs)
const;
1344 template <
class Proxy>
1345 std::enable_if_t<VtIsValueProxy<Proxy>::value, Proxy &>
1347 typedef typename _TypeInfoFor<Proxy>::Type TypeInfo;
1348 return TypeInfo::GetMutableObj(_storage);
1352 std::enable_if_t<!VtIsValueProxy<T>::value, T &>
1355 if (ARCH_UNLIKELY(_IsProxy())) {
1356 *
this = _info->GetProxiedAsVtValue(_storage);
1358 typedef typename _TypeInfoFor<T>::Type TypeInfo;
1359 return TypeInfo::GetMutableObj(_storage);
1362 template <
class Proxy>
1363 std::enable_if_t<VtIsValueProxy<Proxy>::value, Proxy
const &>
1365 typedef typename _TypeInfoFor<Proxy>::Type TypeInfo;
1366 return TypeInfo::GetObj(_storage);
1370 std::enable_if_t<!VtIsValueProxy<T>::value, T
const &>
1372 typedef typename _TypeInfoFor<T>::Type TypeInfo;
1373 if (ARCH_UNLIKELY(_IsProxy())) {
1374 return *
static_cast<T
const *
>(_GetProxiedObjPtr());
1376 return TypeInfo::GetObj(_storage);
1379 void const *_GetProxiedObjPtr()
const {
1380 return _info->GetProxiedObjPtr(_storage);
1386 _FailGet(Vt_DefaultValueHolder (*factory)(),
1387 std::type_info
const &queryType)
const;
1389 inline void _Clear() {
1391 if (_info.
GetLiteral() && !_IsLocalAndTriviallyCopyable())
1392 _info.
Get()->Destroy(_storage);
1393 _info.
Set(
nullptr, 0);
1396 inline bool _IsLocalAndTriviallyCopyable()
const {
1397 unsigned int bits = _info.
BitsAs<
unsigned int>();
1398 return (bits & (_LocalFlag | _TrivialCopyFlag)) ==
1399 (_LocalFlag | _TrivialCopyFlag);
1402 inline bool _IsProxy()
const {
1403 return _info.
BitsAs<
unsigned int>() & _ProxyFlag;
1406 VT_API
static void _RegisterCast(std::type_info
const &from,
1407 std::type_info
const &to,
1411 _PerformCast(std::type_info
const &to,
VtValue const &val);
1414 _CanCast(std::type_info
const &from, std::type_info
const &to);
1417 template <
typename From,
typename To>
1422 #ifdef PXR_PYTHON_SUPPORT_ENABLED
1428 Vt_GetPythonObjectFromHeldValue(
VtValue const &
self);
1431 #endif // PXR_PYTHON_SUPPORT_ENABLED
1443 struct Vt_DefaultValueFactory {
1445 static Vt_DefaultValueHolder Invoke() {
1446 return Vt_DefaultValueHolder::Create<T>();
1450 struct Vt_ValueShapeDataAccess {
1451 static const Vt_ShapeData* _GetShapeData(
const VtValue& value) {
1452 return value._GetShapeData();
1455 static size_t _GetNumElements(
const VtValue& value) {
1456 return value._GetNumElements();
1470 #define _VT_DECLARE_ZERO_VALUE_FACTORY(r, unused, elem) \
1472 VT_API Vt_DefaultValueHolder Vt_DefaultValueFactory<VT_TYPE(elem)>::Invoke();
1474 BOOST_PP_SEQ_FOR_EACH(_VT_DECLARE_ZERO_VALUE_FACTORY,
1477 VT_MATRIX_VALUE_TYPES
1478 VT_QUATERNION_VALUE_TYPES)
1480 #undef _VT_DECLARE_ZERO_VALUE_FACTORY
1489 VtValue::Get<VtValue>()
const {
1495 VtValue::UncheckedGet<VtValue>()
const {
1501 VtValue::IsHolding<VtValue>()
const {
1508 VtValue::IsHolding<void>()
const {
1516 PXR_NAMESPACE_CLOSE_SCOPE
1518 #endif // PXR_BASE_VT_VALUE_H
static void RegisterSimpleBidirectionalCast()
Register a two-way cast from VtValue holding From to VtValue holding To.
constexpr T * Get() const noexcept
Retrieve the pointer.
VT_API bool CanHash() const
Return true if the held object provides a hash implementation.
T const & UncheckedGet() const
Returns a const reference to the held object if the held object is of type T.
Safely compare C++ RTTI type structures.
VtValue(VtValue &&other) noexcept
Move construct with other.
VtValue & operator=(char const *cstr)
Assigning a char const * gives a VtValue holding a std::string.
bool CanCastToTypeOf(VtValue const &other) const
Return if this can be cast to type.
VtValue & Cast()
Return this holding value type cast to T.
VT_API bool IsArrayValued() const
Returns true iff this is holding an array type (see VtIsArray<>).
bool CanCast() const
Return if this can be cast to T.
size_t GetArraySize() const
Return the number of elements in the held value if IsArrayValued(), return 0 otherwise.
bool IsHolding() const
Return true if this value is holding an object of type T, false otherwise.
Definitions of basic string utilities in tf.
VT_API TfType GetType() const
Returns the TfType of the type held by this value.
void UncheckedSwap(T &rhs)
Swap the held value with rhs.
constexpr uintptr_t GetLiteral() const noexcept
Retrieve the raw underlying value.
VtValue(VtValue const &other)
Copy construct with other.
T const & Get() const
Returns a const reference to the held object if the held object is of type T.
T UncheckedRemove()
Make this value empty and return the held T instance.
Demangle C++ typenames generated by the typeid() facility.
static void RegisterSimpleCast()
Register a simple cast from VtValue holding From to VtValue.
static VT_API VtValue CastToTypeOf(VtValue const &val, VtValue const &other)
Return a VtValue holding val cast to same type that other is holding.
static bool CanCastFromTypeidToTypeid(std::type_info const &from, std::type_info const &to)
Return if a value of type from can be cast to type to.
VtValue & CastToTypeOf(VtValue const &other)
Return this holding value type cast to same type that other is holding.
static VtValue Take(T &obj)
Create a new VtValue, taking its contents from obj.
void swap(UsdStageLoadRules &l, UsdStageLoadRules &r)
Swap the contents of rules l and r.
friend bool operator!=(VtValue const &lhs, T const &rhs)
Tests for inequality.
bool CanCastToTypeid(std::type_info const &type) const
Return if this can be cast to type.
VT_API std::type_info const & GetTypeid() const
Returns the typeid of the type held by this value.
friend bool operator==(VtValue const &lhs, T const &rhs)
Tests for equality.
static VtValue Cast(VtValue const &val)
Return a VtValue holding val cast to hold T.
VtValue & operator=(VtValue &&other) noexcept
Move assignment from another VtValue.
static VT_API VtValue CastToTypeid(VtValue const &val, std::type_info const &type)
Return a VtValue holding val cast to type.
T GetWithDefault(T const &def=T()) const
Return a copy of the held object if the held object is of type T.
VT_API std::string GetTypeName() const
Return the type name of the held typeid.
Defines all the types "TYPED" for which Vt creates a VtTYPEDArray typedef.
A simple type-erased container that provides only destruction, moves and immutable, untyped access to the held value.
VT_API std::type_info const & GetElementTypeid() const
Return the typeid of elements in a array valued type.
Array concept. By default, types are not arrays.
static void RegisterCast(VtValue(*castFn)(VtValue const &))
Register a cast from VtValue holding From to VtValue holding To.
VtValue & operator=(char *cstr)
Assigning a char * gives a VtValue holding a std::string.
Boost Python object wrapper.
VtValue(T const &obj)
Construct a VtValue holding a copy of obj.
VT_API size_t GetHash() const
Return a hash code for the held object by calling VtHashValue() on it.
VtValue & Swap(VtValue &rhs) noexcept
Swap this with rhs.
void UncheckedSwap(VtValue &rhs)
friend void swap(VtValue &lhs, VtValue &rhs)
Overloaded swap() for generic code/stl/etc.
TfType represents a dynamic runtime type.
constexpr Integral BitsAs() const noexcept
Retrieve the stored bits as the integral type Integral.
void Set(T *ptr) noexcept
Set the pointer value to ptr.
VtValue & CastToTypeid(std::type_info const &type)
Return this holding value type cast to type.
VT_API friend std::ostream & operator<<(std::ostream &out, const VtValue &self)
Calls through to operator << on the held object.
VtValue()
Default ctor gives empty VtValue.
T Remove()
Make this value empty and return the held T instance.
bool IsEmpty() const
Returns true iff this value is empty.
Provides a container which may hold any type, and provides introspection and iteration over array typ...
A file containing basic constants and definitions.
VtValue & operator=(VtValue const &other)
Copy assignment from another VtValue.
bool TfSafeTypeCompare(const std::type_info &t1, const std::type_info &t2)
Safely compare std::type_info structures.