valueTypeDispatch.h
1 //
2 // Copyright 2024 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TS_VALUE_TYPE_DISPATCH_H
9 #define PXR_BASE_TS_VALUE_TYPE_DISPATCH_H
10 
11 #include "pxr/pxr.h"
12 #include "pxr/base/ts/typeHelpers.h"
13 #include "pxr/base/gf/half.h"
14 #include "pxr/base/tf/type.h"
15 #include "pxr/base/tf/diagnostic.h"
16 
17 #include <utility>
18 
19 PXR_NAMESPACE_OPEN_SCOPE
20 
21 #define _MAKE_CLAUSE(unused, tuple) \
22 if (valueType == Ts_GetType<TS_SPLINE_VALUE_CPP_TYPE(tuple)>()) \
23 { \
24  Cls<TS_SPLINE_VALUE_CPP_TYPE(tuple)>()(std::forward<Args>(args)...); \
25  return; \
26 }
27 
28 // Makes a call to a template functor based on a dynamic type. No return value;
29 // obtain outputs with out-params. Supports all valid spline value types.
30 //
31 // Example:
32 //
33 // template <typename T>
34 // struct _HasNonzeroValue
35 // {
36 // void operator()(const TsKnot &knot, bool *resultOut)
37 // {
38 // T value = 0;
39 // if (knot.GetValue(&value))
40 // *resultOut = (value != 0);
41 // else
42 // *resultOut = false;
43 // }
44 // };
45 //
46 // bool nonzero = false;
47 // TsDispatchToValueTypeTemplate<_HasNonzeroValue>(
48 // myKnot.GetValueType(), myKnot, &nonzero);
49 //
50 template <
51  template <typename T> class Cls,
52  typename... Args>
53 void TsDispatchToValueTypeTemplate(
54  TfType valueType, Args&&... args)
55 {
56 TF_PP_SEQ_FOR_EACH(_MAKE_CLAUSE, ~, TS_SPLINE_SUPPORTED_VALUE_TYPES)
57  TF_CODING_ERROR("Unsupported spline value type");
58 }
59 
60 // Makes a call to a template functor based on dynamic type, dispatching
61 // templated calls using the storage type of T. For example, GfTimeCode
62 // dispatches to double.
63 template <
64  template <typename T> class Cls,
65  typename... Args>
66 void TsDispatchToStorageValueTypeTemplate(
67  TfType valueType, Args&&... args)
68 {
69 TF_PP_SEQ_FOR_EACH(_MAKE_CLAUSE, ~, TS_SPLINE_STORAGE_VALUE_TYPES)
70 
71  if (valueType == Ts_GetType<GfTimeCode>() ||
72  valueType == Ts_GetType<GfDuration>())
73  {
74  Cls<double>()(std::forward<Args>(args)...);
75  return;
76  }
77  TF_CODING_ERROR("Unsupported spline value type for storage");
78 }
79 #undef _MAKE_CLAUSE
80 
81 PXR_NAMESPACE_CLOSE_SCOPE
82 
83 #endif
This header serves to simply bring in the half float datatype and provide a hash_value function.
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition: diagnostic.h:69
Low-level utilities for informing users of various internal and external diagnostic conditions.
TfType represents a dynamic runtime type.
Definition: type.h:47