visitValue.h
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 
31 PXR_NAMESPACE_OPEN_SCOPE
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 
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(r, unused, i, elem) \
113  case i: \
114  return Vt_ValueVisitDetail::Visit<VT_TYPE(elem)>( \
115  value, std::forward<Visitor>(visitor), 0); \
116  break;
117 BOOST_PP_SEQ_FOR_EACH_I(VT_CASE_FOR_TYPE_INDEX, ~, VT_VALUE_TYPES)
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 
128 PXR_NAMESPACE_CLOSE_SCOPE
129 
130 #endif // PXR_BASE_VT_VISIT_VALUE_H
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1093
int GetKnownValueTypeIndex() const
Return VtKnownValueTypeIndex<T> for the held type T.
Definition: value.h:1081
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:166