Loading...
Searching...
No Matches
VtValue Class Reference

Provides a container which may hold any type, and provides introspection and iteration over array types. More...

#include <value.h>

Public Member Functions

 VtValue ()
 Default ctor gives empty VtValue.
 
 VtValue (VtValue const &other)
 Copy construct with other.
 
 VtValue (VtValue &&other) noexcept
 Move construct with other.
 
template<class T >
 VtValue (T const &obj)
 Construct a VtValue holding a copy of obj.
 
VT_API VtValue (VtValueRef ref)
 Construct with VtValueRef.
 
 ~VtValue ()
 Destructor.
 
VtValueoperator= (VtValue const &other)
 Copy assignment from another VtValue.
 
VtValueoperator= (VtValue &&other) noexcept
 Move assignment from another VtValue.
 
VT_API VtValueoperator= (VtValueRef ref)
 Assignment from VtValueRef.
 
template<class T >
VtValueoperator= (T const &obj)
 Assignment operator from any type.
 
VtValueSwap (VtValue &rhs) noexcept
 Swap this with rhs.
 
template<class T >
void Swap (T &rhs)
 Swap the held value with rhs.
 
template<class T >
void UncheckedSwap (T &rhs)
 Swap the held value with rhs.
 
void UncheckedSwap (VtValue &rhs)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<class T >
Remove ()
 Make this value empty and return the held T instance.
 
template<class T >
UncheckedRemove ()
 Make this value empty and return the held T instance.
 
template<class T , class Fn >
bool Mutate (Fn &&mutateFn)
 If this value holds an object of type T, invoke mutateFn, passing it a non-const reference to the held object and return true.
 
template<class T , class Fn >
void UncheckedMutate (Fn &&mutateFn)
 Invoke mutateFn, passing it a non-const reference to the held object which must be of type T.
 
template<class T >
bool IsHolding () const
 Return true if this value is holding an object of type T, false otherwise.
 
VT_API bool IsArrayValued () const
 Return true if this holds a VtArray instance, false otherwise.
 
VT_API bool IsArrayEditValued () const
 Return true if this holds a VtArrayEdit instance, false otherwise.
 
size_t GetArraySize () const
 Return the number of elements in the held value if IsArrayValued(), return 0 otherwise.
 
VT_API std::type_info const & GetTypeid () const
 Return the typeid of the type held by this value.
 
VT_API std::type_info const & GetElementTypeid () const
 If this value holds a VtArray or VtArrayEdit instance, return the typeid of its element type.
 
VT_API TfType GetType () const
 Returns the TfType of the type held by this value.
 
VT_API std::string GetTypeName () const
 Return the type name of the held typeid.
 
int GetKnownValueTypeIndex () const
 Return VtKnownValueTypeIndex<T> for the held type T.
 
template<class T >
T const & UncheckedGet () const &
 Returns a const reference to the held object if the held object is of type T.
 
template<class T >
UncheckedGet () &&
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. In case *this is an rvalue, move the held value out and return by value.
 
template<class T >
T const & Get () const &
 Returns a const reference to the held object if the held object is of type T.
 
template<class T >
Get () &&
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. In case *this is an rvalue, move the held value out and return by value.
 
template<class T >
GetWithDefault (T const &def=T()) const
 Return a copy of the held object if the held object is of type T.
 
template<typename T >
VtValueCast ()
 Return this holding value type cast to T.
 
VtValueCastToTypeOf (VtValue const &other)
 Return this holding value type cast to same type that other is holding.
 
VtValueCastToTypeid (std::type_info const &type)
 Return this holding value type cast to type.
 
template<typename T >
bool CanCast () const
 Return if this can be cast to T.
 
bool CanCastToTypeOf (VtValue const &other) const
 Return if this can be cast to type.
 
bool CanCastToTypeid (std::type_info const &type) const
 Return if this can be cast to type.
 
bool IsEmpty () const
 Returns true iff this value is empty.
 
VT_API VtValueRef Ref () const &
 Return a VtValueRef that refers to the current object held by this VtValue.
 
VT_API VtValueRef Ref () &&
 Return a VtValueRef that refers to the current object held by this rvalue VtValue.
 
VT_API operator VtValueRef () const &
 Implicitly convert to VtValueRef.
 
VT_API operator VtValueRef () &&
 Implicitly convert to an rvalue VtValueRef.
 
VT_API bool CanHash () const
 Return true if the held object provides a hash implementation.
 
VT_API size_t GetHash () const
 Return a hash code for the held object by calling VtHashValue() on it.
 
VT_API bool CanComposeOver () const
 Return true if this value holds a type that has been declared at compile time to support composing over other types.
 
VT_API 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 VtValue &rhs) const
 Test two values for equality.
 
bool operator!= (const VtValue &rhs) const
 
template<>
VtValueoperator= (VtValueRef const &obj)=delete
 

Static Public Member Functions

template<class T >
static VtValue Take (T &obj)
 Create a new VtValue, taking its contents from obj.
 
template<typename From , typename To >
static void RegisterCast (VtValue(*castFn)(VtValue const &))
 Register a cast from VtValue holding From to VtValue holding To.
 
template<typename From , typename To >
static void RegisterSimpleCast ()
 Register a simple cast from VtValue holding From to VtValue.
 
template<typename From , typename To >
static void RegisterSimpleBidirectionalCast ()
 Register a two-way cast from VtValue holding From to VtValue holding To.
 
template<typename T >
static VtValue Cast (VtValue const &val)
 Return a VtValue holding val cast to hold T.
 
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 VT_API VtValue CastToTypeid (VtValue const &val, std::type_info const &type)
 Return a VtValue holding val cast to type.
 
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.
 

Friends

struct _HoldAside
 
void swap (VtValue &lhs, VtValue &rhs)
 Overloaded swap() for generic code/stl/etc.
 
size_t hash_value (VtValue const &val)
 
template<typename T >
bool operator== (VtValue const &lhs, T const &rhs)
 Tests for equality.
 
template<typename T >
bool operator== (T const &lhs, VtValue const &rhs)
 
template<typename T >
bool operator!= (VtValue const &lhs, T const &rhs)
 Tests for inequality.
 
template<typename T >
bool operator!= (T const &lhs, VtValue const &rhs)
 
VT_API friend std::ostream & operator<< (std::ostream &out, const VtValue &self)
 Calls through to operator << on the held object.
 

Detailed Description

Provides a container which may hold any type, and provides introspection and iteration over array types.

See VtIsArray for more info.

Held-type Conversion with VtValue::Cast

VtValue provides a suite of "Cast" methods that convert or create a VtValue holding a requested type (via template parameter, typeid, or type-matching to another VtValue) from the type of the currently-held value. Clients can add conversions between their own types using the RegisterCast(), RegisterSimpleCast(), and RegisterSimpleBidirectionalCast() methods. Conversions from plugins can be guaranteed to be registered before they are needed by registering them from within a

}
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
#define TF_REGISTRY_FUNCTION(KEY_TYPE)
Define a function that is called on demand by TfRegistryManager.

block.

Builtin Type Conversion

Conversions between most of the basic "value types" that are intrinsically convertible are builtin, including all numeric types (including Gf's half), std::string/TfToken, GfVec* (for vecs of the same dimension), and VtArray<T> for floating-point POD and GfVec of the preceding.

Numeric Conversion Safety

The conversions between all scalar numeric types are performed with range checks such as provided by boost::numeric_cast(), and will fail, returning an empty VtValue if the source value is out of range of the destination type.

Conversions between GfVec and other compound-numeric types provide no more or less safety or checking than the conversion constructors of the types themselves. This includes VtArray, even VtArray<T> for T in scalar types that are range-checked when held singly.

Definition at line 89 of file value.h.

Constructor & Destructor Documentation

◆ VtValue() [1/5]

VtValue ( )
inline

Default ctor gives empty VtValue.

Definition at line 823 of file value.h.

◆ VtValue() [2/5]

VtValue ( VtValue const &  other)
inline

Copy construct with other.

Definition at line 826 of file value.h.

◆ VtValue() [3/5]

VtValue ( VtValue &&  other)
inlinenoexcept

Move construct with other.

Definition at line 831 of file value.h.

◆ VtValue() [4/5]

VtValue ( T const &  obj)
inlineexplicit

Construct a VtValue holding a copy of obj.

If T is a char pointer or array, produce a VtValue holding a std::string. If T is pxr_boost::python::object, produce a VtValue holding a TfPyObjWrapper.

Definition at line 841 of file value.h.

◆ VtValue() [5/5]

VT_API VtValue ( VtValueRef  ref)
explicit

Construct with VtValueRef.

◆ ~VtValue()

~VtValue ( )
inline

Destructor.

Definition at line 876 of file value.h.

Member Function Documentation

◆ CanCast()

bool CanCast ( ) const
inline

Return if this can be cast to T.

See also
Held-type Conversion with VtValue::Cast

Definition at line 1208 of file value.h.

◆ CanCastFromTypeidToTypeid()

static bool CanCastFromTypeidToTypeid ( std::type_info const &  from,
std::type_info const &  to 
)
inlinestatic

Return if a value of type from can be cast to type to.

See also
Held-type Conversion with VtValue::Cast

Definition at line 1160 of file value.h.

◆ CanCastToTypeid()

bool CanCastToTypeid ( std::type_info const &  type) const
inline

Return if this can be cast to type.

See also
Held-type Conversion with VtValue::Cast

Definition at line 1222 of file value.h.

◆ CanCastToTypeOf()

bool CanCastToTypeOf ( VtValue const &  other) const
inline

Return if this can be cast to type.

See also
Held-type Conversion with VtValue::Cast

Definition at line 1215 of file value.h.

◆ CanComposeOver()

VT_API bool CanComposeOver ( ) const

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.

◆ CanHash()

VT_API bool CanHash ( ) const

Return true if the held object provides a hash implementation.

◆ CanTransform()

VT_API bool CanTransform ( ) const

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.

◆ Cast() [1/2]

VtValue & Cast ( )
inline

Return this holding value type cast to T.

This value is left empty if the cast fails.

Note
Since this method mutates this value, it is not safe to invoke on the same VtValue in multiple threads simultaneously.
See also
Held-type Conversion with VtValue::Cast

Definition at line 1173 of file value.h.

◆ Cast() [2/2]

static VtValue Cast ( VtValue const &  val)
inlinestatic

Return a VtValue holding val cast to hold T.

Return empty VtValue if cast fails.

This Cast() function is safe to call in multiple threads as it does not mutate the operant val.

See also
Held-type Conversion with VtValue::Cast

Definition at line 1131 of file value.h.

◆ CastToTypeid() [1/2]

VtValue & CastToTypeid ( std::type_info const &  type)
inline

Return this holding value type cast to type.

This value is left empty if the cast fails.

Note
Since this method mutates this value, it is not safe to invoke on the same VtValue in multiple threads simultaneously.
See also
Held-type Conversion with VtValue::Cast

Definition at line 1197 of file value.h.

◆ CastToTypeid() [2/2]

static VT_API VtValue CastToTypeid ( VtValue const &  val,
std::type_info const &  type 
)
static

Return a VtValue holding val cast to type.

Return empty VtValue if cast fails.

This Cast() function is safe to call in multiple threads as it does not mutate the operant val.

See also
Held-type Conversion with VtValue::Cast

◆ CastToTypeOf() [1/2]

VtValue & CastToTypeOf ( VtValue const &  other)
inline

Return this holding value type cast to same type that other is holding.

This value is left empty if the cast fails.

Note
Since this method mutates this value, it is not safe to invoke on the same VtValue in multiple threads simultaneously.
See also
Held-type Conversion with VtValue::Cast

Definition at line 1186 of file value.h.

◆ CastToTypeOf() [2/2]

static VT_API VtValue CastToTypeOf ( VtValue const &  val,
VtValue const &  other 
)
static

Return a VtValue holding val cast to same type that other is holding.

Return empty VtValue if cast fails.

This Cast() function is safe to call in multiple threads as it does not mutate the operant val.

See also
Held-type Conversion with VtValue::Cast

◆ Get() [1/2]

T Get ( ) &&
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. In case *this is an rvalue, move the held value out and return by value.

Definition at line 1080 of file value.h.

◆ Get() [2/2]

T const & Get ( ) const &
inline

Returns a const reference to the held object if the held object is of type T.

Issues an error and returns a const reference to a default value if the held 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 1063 of file value.h.

◆ GetArraySize()

size_t GetArraySize ( ) const
inline

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

Definition at line 1014 of file value.h.

◆ GetElementTypeid()

VT_API std::type_info const & GetElementTypeid ( ) const

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

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

◆ GetHash()

VT_API size_t GetHash ( ) const

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

◆ GetKnownValueTypeIndex()

int GetKnownValueTypeIndex ( ) const
inline

Return VtKnownValueTypeIndex<T> for the held type T.

If this value holds a proxy type, resolve the proxy and return the proxied type's index. If this value is empty or holds a type that is not 'known', return -1.

Definition at line 1034 of file value.h.

◆ GetType()

VT_API TfType GetType ( ) const

Returns the TfType of the type held by this value.

◆ GetTypeid()

VT_API std::type_info const & GetTypeid ( ) const

Return the typeid of the type held by this value.

◆ GetTypeName()

VT_API std::string GetTypeName ( ) const

Return the type name of the held typeid.

◆ GetWithDefault()

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

Return a copy of the held object if the held 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 1098 of file value.h.

◆ IsArrayEditValued()

VT_API bool IsArrayEditValued ( ) const

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

◆ IsArrayValued()

VT_API bool IsArrayValued ( ) const

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

◆ IsEmpty()

bool IsEmpty ( ) const
inline

Returns true iff this value is empty.

Definition at line 1227 of file value.h.

◆ IsHolding()

bool IsHolding ( ) const
inline

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

Definition at line 1002 of file value.h.

◆ Mutate()

bool Mutate ( Fn &&  mutateFn)
inline

If this value holds an object of type T, invoke mutateFn, passing it a non-const reference to the held object and return true.

Otherwise do nothing and return false.

Definition at line 972 of file value.h.

◆ operator VtValueRef() [1/2]

VT_API operator VtValueRef ( ) &&

Implicitly convert to an rvalue VtValueRef.

◆ operator VtValueRef() [2/2]

VT_API operator VtValueRef ( ) const &

Implicitly convert to VtValueRef.

◆ operator!=()

bool operator!= ( const VtValue rhs) const
inline

Definition at line 1303 of file value.h.

◆ operator=() [1/4]

VtValue & operator= ( T const &  obj)

Assignment operator from any type.

Definition at line 1528 of file value.h.

◆ operator=() [2/4]

VtValue & operator= ( VtValue &&  other)
inlinenoexcept

Move assignment from another VtValue.

Definition at line 886 of file value.h.

◆ operator=() [3/4]

VtValue & operator= ( VtValue const &  other)
inline

Copy assignment from another VtValue.

Definition at line 879 of file value.h.

◆ operator=() [4/4]

VT_API VtValue & operator= ( VtValueRef  ref)

Assignment from VtValueRef.

◆ operator==()

bool operator== ( const VtValue rhs) const
inline

Test two values for equality.

Definition at line 1291 of file value.h.

◆ Ref() [1/2]

VT_API VtValueRef Ref ( ) &&

Return a VtValueRef that refers to the current object held by this rvalue VtValue.

The returned VtValueRef is invalidated and must not be used after this VtValue is modified or destroyed.

◆ Ref() [2/2]

VT_API VtValueRef Ref ( ) const &

Return a VtValueRef that refers to the current object held by this VtValue.

The returned VtValueRef is invalidated and must not be used after this VtValue is modified or destroyed.

◆ RegisterCast()

static void RegisterCast ( VtValue(*)(VtValue const &)  castFn)
inlinestatic

Register a cast from VtValue holding From to VtValue holding To.

Definition at line 1104 of file value.h.

◆ RegisterSimpleBidirectionalCast()

static void RegisterSimpleBidirectionalCast ( )
inlinestatic

Register a two-way cast from VtValue holding From to VtValue holding To.

Definition at line 1118 of file value.h.

◆ RegisterSimpleCast()

static void RegisterSimpleCast ( )
inlinestatic

Register a simple cast from VtValue holding From to VtValue.

Definition at line 1111 of file value.h.

◆ Remove()

T Remove ( )
inline

Make this value empty and return the held T instance.

If this value does not hold a T instance, make this value empty and return a default-constructed T.

Definition at line 949 of file value.h.

◆ Swap() [1/2]

void Swap ( T &  rhs)
inline

Swap the held value with rhs.

If this value is holding a T, make an unqualified call to swap(<held-value>, rhs). If this value is not holding a T, replace the held value with a value-initialized T instance first, then swap.

Definition at line 921 of file value.h.

◆ Swap() [2/2]

VtValue & Swap ( VtValue rhs)
inlinenoexcept

Swap this with rhs.

Definition at line 901 of file value.h.

◆ Take()

static VtValue Take ( T &  obj)
inlinestatic

Create a new VtValue, taking its contents from obj.

This is equivalent to creating a VtValue holding a value-initialized T instance, then invoking swap(<held-value>, obj), leaving obj in a default-constructed (value-initialized) state. In the case that obj is expensive to copy, it may be significantly faster to use this idiom when obj need not retain its contents:

MyExpensiveObject obj = CreateObject();
return VtValue::Take(obj);
static VtValue Take(T &obj)
Create a new VtValue, taking its contents from obj.
Definition: value.h:869

Rather than:

MyExpensiveObject obj = CreateObject();
return VtValue(obj);
VtValue()
Default ctor gives empty VtValue.
Definition: value.h:823

Definition at line 869 of file value.h.

◆ UncheckedGet() [1/2]

T UncheckedGet ( ) &&
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. In case *this is an rvalue, move the held value out and return by value.

Definition at line 1052 of file value.h.

◆ UncheckedGet() [2/2]

T const & UncheckedGet ( ) const &
inline

Returns a const reference to the held object if the held 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 1046 of file value.h.

◆ UncheckedMutate()

void UncheckedMutate ( Fn &&  mutateFn)
inline

Invoke mutateFn, passing it a non-const reference to the held object which must be of type T.

If the held object is not of type T, this function invokes undefined behavior.

Definition at line 987 of file value.h.

◆ UncheckedRemove()

T UncheckedRemove ( )
inline

Make this value empty and return the held T instance.

If this value does not hold a T instance, this method invokes undefined behavior.

Definition at line 960 of file value.h.

◆ UncheckedSwap() [1/2]

void UncheckedSwap ( T &  rhs)
inline

Swap the held value with rhs.

This VtValue must be holding an object of type T. If it does not, this invokes undefined behavior. Use Swap() if this VtValue is not known to contain an object of type T.

Definition at line 935 of file value.h.

◆ UncheckedSwap() [2/2]

void UncheckedSwap ( VtValue rhs)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 943 of file value.h.

Friends And Related Function Documentation

◆ _HoldAside

friend struct _HoldAside
friend

Definition at line 776 of file value.h.

◆ hash_value

size_t hash_value ( VtValue const &  val)
friend

Definition at line 1251 of file value.h.

◆ operator!= [1/2]

bool operator!= ( T const &  lhs,
VtValue const &  rhs 
)
friend

Definition at line 1286 of file value.h.

◆ operator!= [2/2]

bool operator!= ( VtValue const &  lhs,
T const &  rhs 
)
friend

Tests for inequality.

Definition at line 1282 of file value.h.

◆ operator<<

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

Calls through to operator << on the held object.

◆ operator== [1/2]

bool operator== ( T const &  lhs,
VtValue const &  rhs 
)
friend

Definition at line 1276 of file value.h.

◆ operator== [2/2]

bool operator== ( VtValue const &  lhs,
T const &  rhs 
)
friend

Tests for equality.

Definition at line 1271 of file value.h.

◆ swap

void swap ( VtValue lhs,
VtValue rhs 
)
friend

Overloaded swap() for generic code/stl/etc.

Definition at line 913 of file value.h.


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