Loading...
Searching...
No Matches
visitValue.h
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
14PXR_NAMESPACE_OPEN_SCOPE
15
16namespace 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.
21template <class T, class Visitor,
22 class = decltype(std::declval<Visitor>()(std::declval<T>()))>
23auto
24Visit(VtValue const &val, Visitor &&visitor, int) {
25 return std::forward<Visitor>(visitor)(val.UncheckedGet<T>());
26}
27
28template <class T, class Visitor>
29auto
30Visit(VtValue const &val, Visitor &&visitor, ...) {
31 return std::forward<Visitor>(visitor)(val);
32}
33
34} // Vt_ValueVisitDetail
35
87template <class Visitor>
88auto 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;
100VT_FOR_EACH_VALUE_TYPE(VT_CASE_FOR_TYPE_INDEX)
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
111PXR_NAMESPACE_CLOSE_SCOPE
112
113#endif // PXR_BASE_VT_VISIT_VALUE_H
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:90
int GetKnownValueTypeIndex() const
Return VtKnownValueTypeIndex<T> for the held type T.
Definition: value.h:1034
T const & UncheckedGet() const &
Returns a const reference to the held object if the held object is of type T.
Definition: value.h:1046