HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
typeRegistry.h
Go to the documentation of this file.
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"
32 #include "pxr/base/tf/singleton.h"
33 #include "pxr/base/ts/data.h"
34 
36 
37 /// \class TsTypeRegistry
38 /// \brief Type registry which provides a mapping from dynamically typed
39 /// objects to statically typed internal ones.
40 ///
41 /// A new type may be registered by using the TS_REGISTER_TYPE macro. ie:
42 ///
43 /// TS_REGISTER_TYPE(double);
44 ///
45 /// The type will also need to have a traits class defined for it. See Types.h
46 /// for example traits classes.
47 ///
49  TsTypeRegistry(const TsTypeRegistry&) = delete;
50  TsTypeRegistry& operator=(const TsTypeRegistry&) = delete;
51 
52 public:
53  /// Return the single instance of TsTypeRegistry
54  TS_API
57  }
58 
59  /// A TypedDataFactory is a function which initializes an
60  /// Ts_PolymorphicDataHolder instance for a given VtValue.
63  const VtValue &value);
64 
65  /// Map from TfTypes to TypedDataFactories
67 
68  /// Registers a TypedDataFactory for a particular type
69  template <class T>
71  _dataFactoryMap[TfType::Find<T>()] = factory;
72  }
73 
74  /// Initialize an Ts_PolymorphicDataHolder so that it holds an
75  /// Ts_TypedData of the appropriate type with the provided values.
76  TS_API
79  const VtValue &value);
80 
81  /// Returns true if the type of \e value is a type we can make keyframes
82  /// for.
83  TS_API
84  bool IsSupportedType(const TfType &type) const;
85 
86 private:
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) \
97 TF_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 
106 
107 #endif
static T & GetInstance()
Definition: singleton.h:137
void
Definition: png.h:1083
TS_API void InitializeDataHolder(Ts_PolymorphicDataHolder *holder, const VtValue &value)
GLsizei const GLfloat * value
Definition: glcorearb.h:824
TS_API bool IsSupportedType(const TfType &type) const
Type registry which provides a mapping from dynamically typed objects to statically typed internal on...
Definition: typeRegistry.h:48
#define TS_API
Definition: api.h:41
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
TfHashMap< TfType, TypedDataFactory, TfHash > DataFactoryMap
Map from TfTypes to TypedDataFactories.
Definition: typeRegistry.h:66
static TS_API TsTypeRegistry & GetInstance()
Return the single instance of TsTypeRegistry.
Definition: typeRegistry.h:55
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: type.h:64
Definition: core.h:1131
type
Definition: core.h:1059
Definition: value.h:164
void(* TypedDataFactory)(Ts_PolymorphicDataHolder *holder, const VtValue &value)
Definition: typeRegistry.h:61
void RegisterTypedDataFactory(TypedDataFactory factory)
Registers a TypedDataFactory for a particular type.
Definition: typeRegistry.h:70