Loading...
Searching...
No Matches
typeRegistry.h
1//
2// Copyright 2023 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_TYPE_REGISTRY_H
9#define PXR_BASE_TS_TYPE_REGISTRY_H
10
11#include "pxr/pxr.h"
12#include "pxr/base/tf/api.h"
13#include "pxr/base/tf/hashmap.h"
16#include "pxr/base/ts/data.h"
17
18PXR_NAMESPACE_OPEN_SCOPE
19
32 TsTypeRegistry(const TsTypeRegistry&) = delete;
33 TsTypeRegistry& operator=(const TsTypeRegistry&) = delete;
34
35public:
37 TS_API
40 }
41
44 typedef void(*TypedDataFactory)(
45 Ts_PolymorphicDataHolder *holder,
46 const VtValue &value);
47
49 typedef TfHashMap<TfType,TypedDataFactory,TfHash> DataFactoryMap;
50
52 template <class T>
54 _dataFactoryMap[TfType::Find<T>()] = factory;
55 }
56
59 TS_API
61 Ts_PolymorphicDataHolder *holder,
62 const VtValue &value);
63
66 TS_API
67 bool IsSupportedType(const TfType &type) const;
68
69private:
70 // Private constructor. Only TfSingleton may create one
72 virtual ~TsTypeRegistry();
73
74 friend class TfSingleton<TsTypeRegistry>;
75
76 DataFactoryMap _dataFactoryMap;
77};
78
79#define TS_REGISTER_TYPE(TYPE) \
80TF_REGISTRY_FUNCTION(TsTypeRegistry) { \
81 TsTypeRegistry &reg = TsTypeRegistry::GetInstance(); \
82 reg.RegisterTypedDataFactory<TYPE>( \
83 [](Ts_PolymorphicDataHolder *holder, const VtValue &value) { \
84 holder->New(value.Get<TYPE>()); \
85 }); \
86}
87
88PXR_NAMESPACE_CLOSE_SCOPE
89
90#endif
Manage a single instance of an object (see.
Definition: singleton.h:105
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
TfType represents a dynamic runtime type.
Definition: type.h:48
Type registry which provides a mapping from dynamically typed objects to statically typed internal on...
Definition: typeRegistry.h:31
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:44
static TS_API TsTypeRegistry & GetInstance()
Return the single instance of TsTypeRegistry.
Definition: typeRegistry.h:38
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:49
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:53
Provides a container which may hold any type, and provides introspection and iteration over array typ...
Definition: value.h:147
Manage a single instance of an object.