Loading...
Searching...
No Matches
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"
16
17#include <utility>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21#define _MAKE_CLAUSE(unused, tuple) \
22if (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//
50template <
51 template <typename T> class Cls,
52 typename... Args>
53void TsDispatchToValueTypeTemplate(
54 TfType valueType, Args&&... args)
55{
56TF_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.
63template <
64 template <typename T> class Cls,
65 typename... Args>
66void TsDispatchToStorageValueTypeTemplate(
67 TfType valueType, Args&&... args)
68{
69TF_PP_SEQ_FOR_EACH(_MAKE_CLAUSE, ~, TS_SPLINE_STORAGE_VALUE_TYPES)
70
71 if (valueType == Ts_GetType<GfTimeCode>())
72 {
73 Cls<double>()(std::forward<Args>(args)...);
74 return;
75 }
76 TF_CODING_ERROR("Unsupported spline value type for storage");
77}
78#undef _MAKE_CLAUSE
79
80PXR_NAMESPACE_CLOSE_SCOPE
81
82#endif
Low-level utilities for informing users of various internal and external diagnostic conditions.
TfType represents a dynamic runtime type.
Definition type.h:48
#define TF_CODING_ERROR(fmt, args)
Issue an internal programming error, but continue execution.
Definition diagnostic.h:68
This header serves to simply bring in the half float datatype and provide a hash_value function.