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 Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_VT_VISIT_VALUE_H
25 #define PXR_BASE_VT_VISIT_VALUE_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/vt/value.h"
30 
32 
33 namespace Vt_ValueVisitDetail {
34 
35 // These two overloads do SFINAE to detect whether the visitor can be invoked
36 // with the given held type T. If the visitor cannot be invoked with T, it is
37 // instead invoked with the VtValue itself.
38 template <class T, class Visitor,
39  class = decltype(std::declval<Visitor>()(std::declval<T>()))>
40 auto
41 Visit(VtValue const &val, Visitor &&visitor, int) {
42  return std::forward<Visitor>(visitor)(val.UncheckedGet<T>());
43 }
44 
45 template <class T, class Visitor>
46 auto
47 Visit(VtValue const &val, Visitor &&visitor, ...) {
48  return std::forward<Visitor>(visitor)(val);
49 }
50 
51 } // Vt_ValueVisitDetail
52 
53 /// Invoke \p visitor with \p value's held object if \p value holds an object of
54 /// one of the "known" value types (those in VT_VALUE_TYPES, see vt/types.h).
55 /// If \p value does not hold a known type, or if it is empty, or if \p visitor
56 /// cannot be called with an object of the held type, then call \p visitor with
57 /// \p value itself. Note this means that \p visitor must be callable with a
58 /// VtValue argument.
59 ///
60 /// VtVisitValue() can be lower overhead compared to a chained-if of
61 /// VtValue::IsHolding() calls, or a hash-table-lookup dispatch. Additionally,
62 /// visitors can handle related types with a single case, rather than calling
63 /// out all types individually. For example:
64 ///
65 /// \code
66 /// // If the value holds an array return its size, otherwise size_t(-1).
67 /// struct GetArraySize {
68 /// template <class T>
69 /// size_t operator()(VtArray<T> const &array) const {
70 /// return array.size();
71 /// }
72 /// size_t operator()(VtValue const &val) const {
73 /// return size_t(-1);
74 /// }
75 /// };
76 ///
77 /// VtVisitValue(VtValue(VtIntArray(123)), GetArraySize()) -> 123
78 /// VtVisitValue(VtValue(VtDoubleArray(234)), GetArraySize()) -> 234
79 /// VtVisitValue(VtValue(VtVec3fArray(345)), GetArraySize()) -> 345
80 /// VtVisitValue(VtValue("not-a-vt-array"), GetArraySize()) -> size_t(-1)
81 /// \endcode
82 ///
83 /// Note that the visitor is invoked as a normal C++ call expression, so
84 /// implicit conversions and standard overload resolution (including controlling
85 /// overload resolution via techniques like enable_if) can take place. For
86 /// example, consider the following, where the double-specific overload is
87 /// invoked for VtValues holding double, float, and GfHalf.
88 ///
89 /// \code
90 /// struct AsDouble {
91 /// double operator()(double val) const {
92 /// return val;
93 /// }
94 /// double operator()(VtValue const &) const {
95 /// return std::numeric_limits<double>::quiet_NaN();
96 /// }
97 /// };
98 ///
99 /// VtVisitValue(VtValue(1.23), AsDouble()) -> 1.23
100 /// VtVisitValue(VtValue(float(0.5f)), AsDouble()) -> 0.5
101 /// VtVisitValue(VtValue(GfHalf(1.5f)), AsDouble()) -> 1.5
102 /// VtVisitValue(VtValue("not-convertible-to-double"), AsDouble()) -> NaN.
103 /// \endcode
104 template <class Visitor>
105 auto VtVisitValue(VtValue const &value, Visitor &&visitor)
106 {
107  // This generally gets the compiler to emit a jump table to dispatch
108  // directly to the code for each known value type.
109  switch (value.GetKnownValueTypeIndex()) {
110 
111 // Cases for known types.
112 #define VT_CASE_FOR_TYPE_INDEX(unused, elem) \
113  case VtGetKnownValueTypeIndex<VT_TYPE(elem)>(): \
114  return Vt_ValueVisitDetail::Visit<VT_TYPE(elem)>( \
115  value, std::forward<Visitor>(visitor), 0); \
116  break;
118 #undef VT_CASE_FOR_TYPE_INDEX
119 
120  default:
121  // Invoke visitor with value itself.
122  return Vt_ValueVisitDetail::Visit<VtValue>(
123  value, std::forward<Visitor>(visitor), 0);
124  break;
125  };
126 }
127 
129 
130 #endif // PXR_BASE_VT_VISIT_VALUE_H
T const & UncheckedGet() const &
Definition: value.h:1121
#define VT_VALUE_TYPES
Definition: types.h:187
GLsizei const GLfloat * value
Definition: glcorearb.h:824
auto VtVisitValue(VtValue const &value, Visitor &&visitor)
Definition: visitValue.h:105
#define VT_CASE_FOR_TYPE_INDEX(unused, elem)
int GetKnownValueTypeIndex() const
Definition: value.h:1109
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define TF_PP_SEQ_FOR_EACH(_macro, data, seq)
GLuint GLfloat * val
Definition: glcorearb.h:1608
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
auto Visit(VtValue const &val, Visitor &&visitor, int)
Definition: visitValue.h:41
Definition: core.h:1131
Definition: value.h:164