Loading...
Searching...
No Matches
typeDispatchTable.h
Go to the documentation of this file.
1//
2// Copyright 2025 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_EXEC_VDF_TYPE_DISPATCH_TABLE_H
8#define PXR_EXEC_VDF_TYPE_DISPATCH_TABLE_H
9
11
14
15#include "pxr/pxr.h"
16
17#include "pxr/exec/vdf/api.h"
18
19#include "pxr/base/tf/hash.h"
20#include "pxr/base/tf/pxrTslRobinMap/robin_map.h"
21#include "pxr/base/tf/type.h"
22
23#include <tbb/spin_rw_mutex.h>
24
25#include <typeinfo>
26#include <utility>
27
28PXR_NAMESPACE_OPEN_SCOPE
29
30// Non-template-dependent part of dispatch table implementation.
32{
33public:
35 VDF_API
36 bool IsTypeRegistered(TfType t) const;
37
38protected:
41 VDF_API
42 bool _RegisterType(const std::type_info &ti, void *f);
43
46 VDF_API
47 void *_FindOrFatalError(TfType t) const;
48
49 VDF_API
51
52 VDF_API
54
55private:
56 // Type for the dispatch map, a map from keys to functions.
58
59 // The type dispatch map.
60 _MapType _map;
61
62 // Guards the hash table buckets.
63 mutable tbb::spin_rw_mutex _mutex;
64};
65
66
118template <template <typename> class Fn>
121{
122public:
129 using FnSignature = decltype(Fn<int>::Call);
130
135 template <typename Type>
137 {
138 // Instantiate Fn for the given type, and put a pointer to it in
139 // the type dispatch map.
140 const std::type_info &ti = typeid(Type);
141 FnSignature *f = &Fn<Type>::Call;
142 return _RegisterType(ti, reinterpret_cast<void *>(f));
143 }
144
149 template <typename RetType, typename... Args>
150 RetType Call(TfType key, Args&&... args) const
151 {
152 return reinterpret_cast<FnSignature *>(this->_FindOrFatalError(key))
153 (std::forward<Args>(args)...);
154 }
155};
156
157PXR_NAMESPACE_CLOSE_SCOPE
158
159#endif
TfType represents a dynamic runtime type.
Definition: type.h:48
This file defines VdfTypeDispatchTable, a template that can be used to perform runtime type dispatch.
VDF_API bool _RegisterType(const std::type_info &ti, void *f)
Register function pointer f as the implementation to dispatch to for type ti.
VDF_API void * _FindOrFatalError(TfType t) const
Find a registered function pointer for type t.
VDF_API bool IsTypeRegistered(TfType t) const
Returns true if a function has been registered for type t.
Dispatches calls to template instantiations based on a TfType that is determined at runtime.
decltype(Fn< int >::Call) FnSignature
The signature of the functions dispatched by the table.
bool RegisterType()
Register an additional type with the type dispatch table.
RetType Call(TfType key, Args &&... args) const
Invoke the function registered for key type.