Loading...
Searching...
No Matches
VtValueRef Class Reference

A non-owning type-erased view of a value, interoperating with VtValue. More...

#include <valueRef.h>

+ Inheritance diagram for VtValueRef:

Public Member Functions

 VtValueRef ()
 Default ctor gives empty VtValueRef.
 
 VtValueRef (VtValueRef const &)=default
 
 VtValueRef (VtValueRef &&)=default
 
template<class T >
 VtValueRef (T &&obj, std::enable_if_t< !std::is_same_v< std::decay_t< T >, VtValueRef > &&!std::is_same_v< std::decay_t< T >, VtValue > > *=0)
 Implicitly convert or construct a VtValueRef referring to obj.
 
VtValueRefoperator= (VtValueRef const &ref)=default
 Rebind to view the same object as another VtValueRef.
 
VtValueRefoperator= (VtValueRef &&ref)=default
 
template<class T >
VtValueRefoperator= (T &&)=delete
 
template<class T >
bool IsHolding () const
 Return true if this value is viewing an object of type T, false otherwise.
 
bool IsRValue () const
 Return true if this refers to an rvalue, and thus can be Remove()d by a move construction operation instead of a copy.
 
VT_API bool IsArrayValued () const
 Return true if this views a VtArray instance, false otherwise.
 
VT_API bool IsArrayEditValued () const
 Return true if this views a VtArrayEdit instance, false otherwise.
 
size_t GetArraySize () const
 Return the number of elements in the viewed value if IsArrayValued(), return 0 otherwise.
 
std::type_info const & GetTypeid () const
 Return the typeid of the type viewed by this value.
 
std::type_info const & GetElementTypeid () const
 If this value views a VtArray or VtArrayEdit instance, return the typeid of its element type.
 
VT_API TfType GetType () const
 Returns the TfType of the type viewed by this value.
 
VT_API std::string GetTypeName () const
 Return the type name of the viewed typeid.
 
int GetKnownValueTypeIndex () const
 Return VtKnownValueTypeIndex<T> for the viewed type T.
 
template<class T >
_TypeInfoFor< T >::GetObjResultType UncheckedGet () const
 Return a const reference to the viewed object if the viewed object is of type T.
 
template<class T >
_TypeInfoFor< T >::GetObjResultType Get () const
 Return a const reference to the viewed object if the viewed object is of type T.
 
template<class T >
GetWithDefault (T const &def=T()) const
 Return a copy of the viewed object if the viewed object is of type T.
 
template<class T >
Remove ()
 Return a move-constructed (if the viewed object is an rvalue) or copy-constructed T instance from the viewed object.
 
template<class T >
UncheckedRemove ()
 Return a move-constructed (if the viewed object is an rvalue) or copy-constructed T instance from the viewed object.
 
bool IsEmpty () const
 Returns true iff this value is empty.
 
VT_API operator VtValue () const
 Implicitly convert to a VtValue by copying the viewed object.
 
VT_API bool CanHash () const
 Return true if the viewed object provides a hash implementation.
 
VT_API size_t GetHash () const
 Return a hash code for the viewed object by calling VtHashValue() on it.
 
bool CanComposeOver () const
 Return true if this value holds a type that has been declared at compile time to support composing over other types.
 
bool CanTransform () const
 Return true if this value holds a type that has been declared to support value transforms at compile time.
 
bool operator== (const VtValueRef &rhs) const
 Test two values for equality.
 
bool operator!= (const VtValueRef &rhs) const
 

Protected Types

template<class T >
using _ArrayHelper = TfConditionalType< VtIsArray< T >::value, _IsArrayHelper< T >, TfConditionalType< VtIsArrayEdit< T >::value, _IsArrayEditHelper< T >, _NonArrayHelper< T > > >
 
template<class T >
using _TypeInfoFor = _TypeInfoImpl< std::decay_t< T >, std::is_reference_v< T > &&std::is_const_v< T > >
 

Protected Member Functions

VT_API size_t _GetNumElements () const
 
template<class T >
bool _TypeIs () const
 
template<class T >
_TypeInfoFor< T >::GetMutableObjResultType _GetMutable ()
 
template<class T >
_TypeInfoFor< T >::GetObjResultType _Get () const
 
VT_API void const * _FailGet (Vt_DefaultValueHolder(*factory)(), std::type_info const &queryType) const
 
VT_API void _FailRemove (std::type_info const &)
 
VT_API TfPyObjWrapper _GetPythonObject () const
 

Protected Attributes

_RefdObjPtr _ptr
 
_TypeInfo const * _info
 

Friends

void swap (VtValueRef &lhs, VtValueRef &rhs)
 Overloaded swap() for generic code/stl/etc. Swaps the viewed objects.
 
size_t hash_value (VtValueRef const &val)
 
VT_API friend std::ostream & operator<< (std::ostream &out, const VtValueRef &self)
 Calls through to operator << on the viewed object.
 

Detailed Description

A non-owning type-erased view of a value, interoperating with VtValue.

Since VtValueRef is non-owning it must not persist beyond the lifetime of the value it views, and so it is typically best used as a function argument or an automatic variable.

Both ordinary typed values and VtValues are implicitly convertible to VtValueRef. This makes it convenient to use when writing a function that can take an object of any type. Since it's non-owning, it is relatively light-weight, incurring no heap allocations or reference counting operations. In addition, VtValueRef remembers if it is viewing an rvalue-reference, in which case the underlying object can be removed by a move() operation rather than a copy. See Remove().

VtValueRef supports much of the same API as VtValue, including implicitly converting back to VtValue if needed.

Definition at line 64 of file valueRef.h.

Member Typedef Documentation

◆ _ArrayHelper

using _ArrayHelper = TfConditionalType< VtIsArray<T>::value, _IsArrayHelper<T>, TfConditionalType<VtIsArrayEdit<T>::value, _IsArrayEditHelper<T>, _NonArrayHelper<T> > >
protected

Definition at line 211 of file valueRef.h.

◆ _TypeInfoFor

using _TypeInfoFor = _TypeInfoImpl< std::decay_t<T>, std::is_reference_v<T> && std::is_const_v<T> >
protected

Definition at line 359 of file valueRef.h.

Constructor & Destructor Documentation

◆ VtValueRef() [1/2]

VtValueRef ( )
inline

Default ctor gives empty VtValueRef.

Definition at line 366 of file valueRef.h.

◆ VtValueRef() [2/2]

VtValueRef ( T &&  obj,
std::enable_if_t< !std::is_same_v< std::decay_t< T >, VtValueRef > &&!std::is_same_v< std::decay_t< T >, VtValue > > *  = 0 
)
inline

Implicitly convert or construct a VtValueRef referring to obj.

The passed obj must outlive this VtValueRef instance.

Definition at line 374 of file valueRef.h.

Member Function Documentation

◆ _Get()

_TypeInfoFor< T >::GetObjResultType _Get ( ) const
inlineprotected

Definition at line 591 of file valueRef.h.

◆ _GetMutable()

_TypeInfoFor< T >::GetMutableObjResultType _GetMutable ( )
inlineprotected

Definition at line 584 of file valueRef.h.

◆ _TypeIs()

bool _TypeIs ( ) const
inlineprotected

Definition at line 572 of file valueRef.h.

◆ CanComposeOver()

bool CanComposeOver ( ) const
inline

Return true if this value holds a type that has been declared at compile time to support composing over other types.

This is a fast check that can be used to avoid calling VtValueComposeOver(strong, weak) if strong does not support composing over. Empty VtValue always can compose over.

Definition at line 535 of file valueRef.h.

◆ CanHash()

VT_API bool CanHash ( ) const

Return true if the viewed object provides a hash implementation.

◆ CanTransform()

bool CanTransform ( ) const
inline

Return true if this value holds a type that has been declared to support value transforms at compile time.

This is a fast check that can be used to avoid calling the relatively slower VtValueTryTransform(obj, xform) if obj does not support transforms at all. Empty VtValue does not support transforms.

Definition at line 544 of file valueRef.h.

◆ Get()

_TypeInfoFor< T >::GetObjResultType Get ( ) const
inline

Return a const reference to the viewed object if the viewed object is of type T.

Issues an error and return a const reference to a default value if the viewed object is not of type T. Use IsHolding to verify correct type before calling this function. The default value returned in case of type mismatch is constructed using Vt_DefaultValueFactory<T>. That may be specialized for client types. The default implementation of the default value factory produces a value-initialized T.

Definition at line 464 of file valueRef.h.

◆ GetArraySize()

size_t GetArraySize ( ) const
inline

Return the number of elements in the viewed value if IsArrayValued(), return 0 otherwise.

Definition at line 421 of file valueRef.h.

◆ GetElementTypeid()

std::type_info const & GetElementTypeid ( ) const
inline

If this value views a VtArray or VtArrayEdit instance, return the typeid of its element type.

For example, if this value views a VtIntArray or a VtIntArrayEdit, return typeid(int). Otherwise return typeid(void).

Definition at line 431 of file valueRef.h.

◆ GetHash()

VT_API size_t GetHash ( ) const

Return a hash code for the viewed object by calling VtHashValue() on it.

◆ GetKnownValueTypeIndex()

int GetKnownValueTypeIndex ( ) const
inline

Return VtKnownValueTypeIndex<T> for the viewed type T.

If this value is empty or views a type that is not 'known', return -1.

Definition at line 443 of file valueRef.h.

◆ GetType()

VT_API TfType GetType ( ) const

Returns the TfType of the type viewed by this value.

◆ GetTypeid()

std::type_info const & GetTypeid ( ) const
inline

Return the typeid of the type viewed by this value.

Definition at line 424 of file valueRef.h.

◆ GetTypeName()

VT_API std::string GetTypeName ( ) const

Return the type name of the viewed typeid.

◆ GetWithDefault()

T GetWithDefault ( T const &  def = T()) const
inline

Return a copy of the viewed object if the viewed object is of type T.

Return a copy of the default value def otherwise. Note that this always returns a copy, as opposed to Get() which always returns a reference.

Definition at line 482 of file valueRef.h.

◆ IsArrayEditValued()

VT_API bool IsArrayEditValued ( ) const

Return true if this views a VtArrayEdit instance, false otherwise.

◆ IsArrayValued()

VT_API bool IsArrayValued ( ) const

Return true if this views a VtArray instance, false otherwise.

◆ IsEmpty()

bool IsEmpty ( ) const
inline

Returns true iff this value is empty.

Definition at line 515 of file valueRef.h.

◆ IsHolding()

bool IsHolding ( ) const
inline

Return true if this value is viewing an object of type T, false otherwise.

Definition at line 403 of file valueRef.h.

◆ IsRValue()

bool IsRValue ( ) const
inline

Return true if this refers to an rvalue, and thus can be Remove()d by a move construction operation instead of a copy.

Definition at line 409 of file valueRef.h.

◆ operator VtValue()

VT_API operator VtValue ( ) const

Implicitly convert to a VtValue by copying the viewed object.

◆ operator!=()

bool operator!= ( const VtValueRef rhs) const
inline

Definition at line 561 of file valueRef.h.

◆ operator=()

VtValueRef & operator= ( VtValueRef const &  ref)
default

Rebind to view the same object as another VtValueRef.

◆ operator==()

bool operator== ( const VtValueRef rhs) const
inline

Test two values for equality.

Definition at line 549 of file valueRef.h.

◆ Remove()

T Remove ( )
inline

Return a move-constructed (if the viewed object is an rvalue) or copy-constructed T instance from the viewed object.

If the viewed object is an rvalue then it is left in its moved-from state. If this value does not view a T instance, issue an error and return a default-constructed T.

Definition at line 492 of file valueRef.h.

◆ UncheckedGet()

_TypeInfoFor< T >::GetObjResultType UncheckedGet ( ) const
inline

Return a const reference to the viewed object if the viewed object is of type T.

Invokes undefined behavior otherwise. This is the fastest Get() method to use after a successful IsHolding() check.

Definition at line 452 of file valueRef.h.

◆ UncheckedRemove()

T UncheckedRemove ( )
inline

Return a move-constructed (if the viewed object is an rvalue) or copy-constructed T instance from the viewed object.

If the viewed object is an rvalue then it is left in its moved-from state. If this value does not view a T instance, invoke undefined behavior.

Definition at line 507 of file valueRef.h.

Friends And Related Function Documentation

◆ hash_value

size_t hash_value ( VtValueRef const &  val)
friend

Definition at line 526 of file valueRef.h.

◆ operator<<

VT_API friend std::ostream & operator<< ( std::ostream &  out,
const VtValueRef self 
)
friend

Calls through to operator << on the viewed object.

◆ swap

void swap ( VtValueRef lhs,
VtValueRef rhs 
)
friend

Overloaded swap() for generic code/stl/etc. Swaps the viewed objects.

Definition at line 395 of file valueRef.h.

Member Data Documentation

◆ _info

_TypeInfo const* _info
protected

Definition at line 614 of file valueRef.h.

◆ _ptr

_RefdObjPtr _ptr
protected

Definition at line 613 of file valueRef.h.


The documentation for this class was generated from the following file: