Loading...
Searching...
No Matches
typeRegistry.h
1//
2// Copyright 2023 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
25#ifndef PXR_BASE_TS_TYPE_REGISTRY_H
26#define PXR_BASE_TS_TYPE_REGISTRY_H
27
28#include "pxr/pxr.h"
29#include "pxr/base/tf/api.h"
30#include "pxr/base/tf/hashmap.h"
33#include "pxr/base/ts/data.h"
34
35PXR_NAMESPACE_OPEN_SCOPE
36
49 TsTypeRegistry(const TsTypeRegistry&) = delete;
50 TsTypeRegistry& operator=(const TsTypeRegistry&) = delete;
51
52public:
54 TS_API
57 }
58
61 typedef void(*TypedDataFactory)(
62 Ts_PolymorphicDataHolder *holder,
63 const VtValue &value);
64
66 typedef TfHashMap<TfType,TypedDataFactory,TfHash> DataFactoryMap;
67
69 template <class T>
71 _dataFactoryMap[TfType::Find<T>()] = factory;
72 }
73
76 TS_API
78 Ts_PolymorphicDataHolder *holder,
79 const VtValue &value);
80
83 TS_API
84 bool IsSupportedType(const TfType &type) const;
85
86private:
87 // Private constructor. Only TfSingleton may create one
89 virtual ~TsTypeRegistry();
90
91 friend class TfSingleton<TsTypeRegistry>;
92
93 DataFactoryMap _dataFactoryMap;
94};
95
96#define TS_REGISTER_TYPE(TYPE) \
97TF_REGISTRY_FUNCTION(TsTypeRegistry) { \
98 TsTypeRegistry &reg = TsTypeRegistry::GetInstance(); \
99 reg.RegisterTypedDataFactory<TYPE>( \
100 [](Ts_PolymorphicDataHolder *holder, const VtValue &value) { \
101 holder->New(value.Get<TYPE>()); \
102 }); \
103}
104
105PXR_NAMESPACE_CLOSE_SCOPE
106
107#endif
Manage a single instance of an object (see.
Definition: singleton.h:122
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
TfType represents a dynamic runtime type.
Definition: type.h:65
Type registry which provides a mapping from dynamically typed objects to statically typed internal on...
Definition: typeRegistry.h:48
void(* TypedDataFactory)(Ts_PolymorphicDataHolder *holder, const VtValue &value)
A TypedDataFactory is a function which initializes an Ts_PolymorphicDataHolder instance for a given V...
Definition: typeRegistry.h:61
static TS_API TsTypeRegistry & GetInstance()
Return the single instance of TsTypeRegistry.
Definition: typeRegistry.h:55
TS_API void InitializeDataHolder(Ts_PolymorphicDataHolder *holder, const VtValue &value)
Initialize an Ts_PolymorphicDataHolder so that it holds an Ts_TypedData of the appropriate type with ...
TfHashMap< TfType, TypedDataFactory, TfHash > DataFactoryMap
Map from TfTypes to TypedDataFactories.
Definition: typeRegistry.h:66
TS_API bool IsSupportedType(const TfType &type) const
Returns true if the type of value is a type we can make keyframes for.
void RegisterTypedDataFactory(TypedDataFactory factory)
Registers a TypedDataFactory for a particular type.
Definition: typeRegistry.h:70
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:164
Manage a single instance of an object.