HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
visitValue.h File Reference
#include "pxr/pxr.h"
#include "pxr/base/vt/value.h"
+ Include dependency graph for visitValue.h:

Go to the source code of this file.

Namespaces

 Vt_ValueVisitDetail
 

Macros

#define VT_CASE_FOR_TYPE_INDEX(unused, elem)
 

Functions

template<class T , class Visitor , class = decltype(std::declval<Visitor>()(std::declval<T>()))>
auto Vt_ValueVisitDetail::Visit (VtValue const &val, Visitor &&visitor, int)
 
template<class T , class Visitor >
auto Vt_ValueVisitDetail::Visit (VtValue const &val, Visitor &&visitor,...)
 
template<class Visitor >
auto VtVisitValue (VtValue const &value, Visitor &&visitor)
 

Macro Definition Documentation

#define VT_CASE_FOR_TYPE_INDEX (   unused,
  elem 
)
Value:
case VtGetKnownValueTypeIndex<VT_TYPE(elem)>(): \
value, std::forward<Visitor>(visitor), 0); \
break;
GLsizei const GLfloat * value
Definition: glcorearb.h:824
auto Visit(VtValue const &val, Visitor &&visitor, int)
Definition: visitValue.h:41
#define VT_TYPE(elem)
Definition: types.h:141

Function Documentation

template<class Visitor >
auto VtVisitValue ( VtValue const &  value,
Visitor &&  visitor 
)

Invoke visitor with value's held object if value holds an object of one of the "known" value types (those in VT_VALUE_TYPES, see vt/types.h). If value does not hold a known type, or if it is empty, or if visitor cannot be called with an object of the held type, then call visitor with value itself. Note this means that visitor must be callable with a VtValue argument.

VtVisitValue() can be lower overhead compared to a chained-if of VtValue::IsHolding() calls, or a hash-table-lookup dispatch. Additionally, visitors can handle related types with a single case, rather than calling out all types individually. For example:

// If the value holds an array return its size, otherwise size_t(-1).
struct GetArraySize {
template <class T>
size_t operator()(VtArray<T> const &array) const {
return array.size();
}
size_t operator()(VtValue const &val) const {
return size_t(-1);
}
};
VtVisitValue(VtValue(VtIntArray(123)), GetArraySize()) -> 123
VtVisitValue(VtValue(VtDoubleArray(234)), GetArraySize()) -> 234
VtVisitValue(VtValue(VtVec3fArray(345)), GetArraySize()) -> 345
VtVisitValue(VtValue("not-a-vt-array"), GetArraySize()) -> size_t(-1)

Note that the visitor is invoked as a normal C++ call expression, so implicit conversions and standard overload resolution (including controlling overload resolution via techniques like enable_if) can take place. For example, consider the following, where the double-specific overload is invoked for VtValues holding double, float, and GfHalf.

struct AsDouble {
double operator()(double val) const {
return val;
}
double operator()(VtValue const &) const {
return std::numeric_limits<double>::quiet_NaN();
}
};
VtVisitValue(VtValue(1.23), AsDouble()) -> 1.23
VtVisitValue(VtValue(float(0.5f)), AsDouble()) -> 0.5
VtVisitValue(VtValue(GfHalf(1.5f)), AsDouble()) -> 1.5
VtVisitValue(VtValue("not-convertible-to-double"), AsDouble()) -> NaN.

Definition at line 105 of file visitValue.h.