HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
visitValue.h
Go to the documentation of this file.
1 //
2 // Copyright 2022 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_VISIT_VALUE_H
8 #define PXR_BASE_VT_VISIT_VALUE_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/base/vt/value.h"
13 
15 
16 namespace Vt_ValueVisitDetail {
17 
18 // These two overloads do SFINAE to detect whether the visitor can be invoked
19 // with the given held type T. If the visitor cannot be invoked with T, it is
20 // instead invoked with the VtValue itself.
21 template <class T, class Visitor,
22  class = decltype(std::declval<Visitor>()(std::declval<T>()))>
23 auto
24 Visit(VtValue const &val, Visitor &&visitor, int) {
25  return std::forward<Visitor>(visitor)(val.UncheckedGet<T>());
26 }
27 
28 template <class T, class Visitor>
29 auto
30 Visit(VtValue const &val, Visitor &&visitor, ...) {
31  return std::forward<Visitor>(visitor)(val);
32 }
33 
34 } // Vt_ValueVisitDetail
35 
36 /// Invoke \p visitor with \p value's held object if \p value holds an object of
37 /// one of the "known" value types (those in VT_VALUE_TYPES, see vt/types.h).
38 /// If \p value does not hold a known type, or if it is empty, or if \p visitor
39 /// cannot be called with an object of the held type, then call \p visitor with
40 /// \p value itself. Note this means that \p visitor must be callable with a
41 /// VtValue argument.
42 ///
43 /// VtVisitValue() can be lower overhead compared to a chained-if of
44 /// VtValue::IsHolding() calls, or a hash-table-lookup dispatch. Additionally,
45 /// visitors can handle related types with a single case, rather than calling
46 /// out all types individually. For example:
47 ///
48 /// \code
49 /// // If the value holds an array return its size, otherwise size_t(-1).
50 /// struct GetArraySize {
51 /// template <class T>
52 /// size_t operator()(VtArray<T> const &array) const {
53 /// return array.size();
54 /// }
55 /// size_t operator()(VtValue const &val) const {
56 /// return size_t(-1);
57 /// }
58 /// };
59 ///
60 /// VtVisitValue(VtValue(VtIntArray(123)), GetArraySize()) -> 123
61 /// VtVisitValue(VtValue(VtDoubleArray(234)), GetArraySize()) -> 234
62 /// VtVisitValue(VtValue(VtVec3fArray(345)), GetArraySize()) -> 345
63 /// VtVisitValue(VtValue("not-a-vt-array"), GetArraySize()) -> size_t(-1)
64 /// \endcode
65 ///
66 /// Note that the visitor is invoked as a normal C++ call expression, so
67 /// implicit conversions and standard overload resolution (including controlling
68 /// overload resolution via techniques like enable_if) can take place. For
69 /// example, consider the following, where the double-specific overload is
70 /// invoked for VtValues holding double, float, and GfHalf.
71 ///
72 /// \code
73 /// struct AsDouble {
74 /// double operator()(double val) const {
75 /// return val;
76 /// }
77 /// double operator()(VtValue const &) const {
78 /// return std::numeric_limits<double>::quiet_NaN();
79 /// }
80 /// };
81 ///
82 /// VtVisitValue(VtValue(1.23), AsDouble()) -> 1.23
83 /// VtVisitValue(VtValue(float(0.5f)), AsDouble()) -> 0.5
84 /// VtVisitValue(VtValue(GfHalf(1.5f)), AsDouble()) -> 1.5
85 /// VtVisitValue(VtValue("not-convertible-to-double"), AsDouble()) -> NaN.
86 /// \endcode
87 template <class Visitor>
88 auto VtVisitValue(VtValue const &value, Visitor &&visitor)
89 {
90  // This generally gets the compiler to emit a jump table to dispatch
91  // directly to the code for each known value type.
92  switch (value.GetKnownValueTypeIndex()) {
93 
94 // Cases for known types.
95 #define VT_CASE_FOR_TYPE_INDEX(unused, elem) \
96  case VtGetKnownValueTypeIndex<VT_TYPE(elem)>(): \
97  return Vt_ValueVisitDetail::Visit<VT_TYPE(elem)>( \
98  value, std::forward<Visitor>(visitor), 0); \
99  break;
101 #undef VT_CASE_FOR_TYPE_INDEX
102 
103  default:
104  // Invoke visitor with value itself.
105  return Vt_ValueVisitDetail::Visit<VtValue>(
106  value, std::forward<Visitor>(visitor), 0);
107  break;
108  };
109 }
110 
112 
113 #endif // PXR_BASE_VT_VISIT_VALUE_H
T const & UncheckedGet() const &
Definition: value.h:1104
#define VT_VALUE_TYPES
Definition: types.h:170
GLsizei const GLfloat * value
Definition: glcorearb.h:824
auto VtVisitValue(VtValue const &value, Visitor &&visitor)
Definition: visitValue.h:88
#define VT_CASE_FOR_TYPE_INDEX(unused, elem)
int GetKnownValueTypeIndex() const
Definition: value.h:1092
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define TF_PP_SEQ_FOR_EACH(_macro, data, seq)
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
auto Visit(VtValue const &val, Visitor &&visitor, int)
Definition: visitValue.h:24
Definition: value.h:146