All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
pluginRegistry.h
1//
2// Copyright 2016 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_IMAGING_HF_PLUGIN_REGISTRY_H
8#define PXR_IMAGING_HF_PLUGIN_REGISTRY_H
9
10#include "pxr/pxr.h"
11#include "pxr/imaging/hf/api.h"
12#include "pxr/imaging/hf/perfLog.h"
13#include "pxr/imaging/hf/pluginDesc.h"
14#include "pxr/base/plug/registry.h"
15#include "pxr/base/tf/type.h"
16
17#include <map>
18
19PXR_NAMESPACE_OPEN_SCOPE
20
21
22class HfPluginBase;
23class Hf_PluginEntry;
24
25
55{
56
57public:
62 HF_API
63 void GetPluginDescs(HfPluginDescVector *plugins);
64
69 HF_API
70 bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc);
71
75 HF_API
77
82 HF_API
84
89 HF_API
90 bool IsRegisteredPlugin(const TfToken &pluginId);
91
92 HF_API
93 TfToken GetPluginId(const HfPluginBase *plugin) const;
94
95protected:
96 // Must be derived.
97
103 HF_API
104 HfPluginRegistry(const TfType &pluginBaseType);
105 HF_API
106 virtual ~HfPluginRegistry();
107
112 HF_API
113 HfPluginBase *GetPlugin(const TfToken &pluginId);
114
124 template<typename T, typename PluginBaseType, typename... Bases>
125 static void Define();
126
129 HF_API
131 const PlugRegistry &plugRegistry, const TfType &pluginType);
132
133private:
134 typedef std::vector<Hf_PluginEntry> _PluginEntryVector;
135 typedef std::map<TfToken, size_t> _TokenMap;
136
137 //
138 // The purpose of this group of functions is to provide a factory
139 // to create the registered plugin with the type system
140 // without exposing the internal class _PluginEntry
142 typedef std::function<HfPluginBase *()> _FactoryFn;
143
144 template<typename T>
145 static HfPluginBase *_CreatePlugin();
146
147 HF_API
148 static void _SetFactory(TfType &type, _FactoryFn &func);
149
150 TfType _pluginBaseType;
151
152 //
153 // Plugins are stored in a ordered list (as a vector). The token
154 // map converts from plugin id into an index in the list.
155 //
156 _PluginEntryVector _pluginEntries;
157 _TokenMap _pluginIndex;
158
159 // Plugin discovery is deferred until first use.
160 bool _pluginCachePopulated;
161
162 // Use the Plug system to discover plugins from the meta data.
163 void _DiscoverPlugins();
164
165 // Find the plugin entry for the given plugin object.
166 Hf_PluginEntry *_GetEntryForPlugin(HfPluginBase *plugin);
167
171 HfPluginRegistry() = delete;
172 HfPluginRegistry(const HfPluginRegistry &) = delete;
173 HfPluginRegistry &operator=(const HfPluginRegistry &) = delete;
174};
175
176template<typename T>
178HfPluginRegistry::_CreatePlugin()
179{
180 HF_MALLOC_TAG_FUNCTION();
181 return new T;
182}
183
184
185template<typename T, typename PluginBaseType, typename... Bases>
186void
188{
189 TfType type = TfType::Define<T,
190 TfType::Bases<PluginBaseType, Bases...> >();
191
192 _FactoryFn func = &_CreatePlugin<T>;
193 _SetFactory(type, func);
194}
195
196
197
198PXR_NAMESPACE_CLOSE_SCOPE
199
200#endif //PXR_IMAGING_HF_PLUGIN_REGISTRY_H
201
Base class for all hydra plugin classes.
Definition: pluginBase.h:23
Base class for registering Hydra plugins using the plug mechanism.
HF_API HfPluginRegistry(const TfType &pluginBaseType)
Constructs a Plugin Registry.
HF_API void ReleasePlugin(HfPluginBase *plugin)
Decrement the reference count on the plugin.
static void Define()
Entry point for registering a types implementation.
HF_API void AddPluginReference(HfPluginBase *plugin)
Increment the reference count on an existing plugin.
HF_API bool IsRegisteredPlugin(const TfToken &pluginId)
Returns true if a plugin has been registered for the given id.
HF_API void GetPluginDescs(HfPluginDescVector *plugins)
Returns an ordered list of all registered plugins.
HF_API bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc)
Returns the description for the given plugin id.
virtual HF_API void _CollectAdditionalMetadata(const PlugRegistry &plugRegistry, const TfType &pluginType)
Gives subclasses an opportunity to inspect plugInfo-based metadata at the time of discovery.
HF_API HfPluginBase * GetPlugin(const TfToken &pluginId)
Returns the plugin from the given pluginId.
Defines an interface for registering plugins.
Definition: registry.h:319
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
TfType represents a dynamic runtime type.
Definition: type.h:48
static TfType const & Define()
Define a TfType with the given C++ type T and C++ base types B.
A type-list of C++ base types.
Definition: type.h:83
Common structure used to report registered plugins in one of the plugin registries.
Definition: pluginDesc.h:30