All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pluginRegistry.h
1 //
2 // Copyright 2016 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 #ifndef HF_PLUGIN_REGISTRY_H
25 #define HF_PLUGIN_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/imaging/hf/api.h"
29 #include "pxr/imaging/hf/perfLog.h"
30 #include "pxr/imaging/hf/pluginDesc.h"
31 #include "pxr/base/tf/type.h"
32 #include <map>
33 
34 PXR_NAMESPACE_OPEN_SCOPE
35 
36 
37 class HfPluginBase;
38 class Hf_PluginEntry;
39 
40 
70 {
71 
72 public:
77  HF_API
78  void GetPluginDescs(HfPluginDescVector *plugins);
79 
84  HF_API
85  bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc);
86 
90  HF_API
91  void AddPluginReference(HfPluginBase *plugin);
92 
97  HF_API
98  void ReleasePlugin(HfPluginBase *plugin);
99 
104  HF_API
105  bool IsRegisteredPlugin(const TfToken &pluginId);
106 
107 protected:
108  // Must be derived.
109 
115  HF_API
116  HfPluginRegistry(const TfType &pluginBaseType);
117  HF_API
118  virtual ~HfPluginRegistry();
119 
124  HF_API
125  HfPluginBase *GetPlugin(const TfToken &pluginId);
126 
136  template<typename T, typename PluginBaseType, typename... Bases>
137  static void Define();
138 
139 private:
140  typedef std::vector<Hf_PluginEntry> _PluginEntryVector;
141  typedef std::map<TfToken, size_t> _TokenMap;
142 
143  //
144  // The purpose of this group of functions is to provide a factory
145  // to create the registered plugin with the type system
146  // without exposing the internal class _PluginEntry
148  typedef std::function<HfPluginBase *()> _FactoryFn;
149 
150  template<typename T>
151  static HfPluginBase *_CreatePlugin();
152  HF_API
153  static void _SetFactory(TfType &type, _FactoryFn &func);
154 
155  TfType _pluginBaseType;
156 
157  //
158  // Plugins are stored in a ordered list (as a vector). The token
159  // map converts from plugin id into an index in the list.
160  //
161  _PluginEntryVector _pluginEntries;
162  _TokenMap _pluginIndex;
163 
164  // Plugin discovery is deferred until first use.
165  bool _pluginCachePopulated;
166 
167  // Use the Plug system to discover plugins from the meta data.
168  void _DiscoverPlugins();
169 
170  // Find the plugin entry for the given plugin object.
171  Hf_PluginEntry *_GetEntryForPlugin(HfPluginBase *plugin);
172 
176  HfPluginRegistry() = delete;
177  HfPluginRegistry(const HfPluginRegistry &) = delete;
178  HfPluginRegistry &operator=(const HfPluginRegistry &) = delete;
179 };
180 
181 template<typename T>
182 HfPluginBase *
183 HfPluginRegistry::_CreatePlugin()
184 {
185  HF_MALLOC_TAG_FUNCTION();
186  return new T;
187 }
188 
189 
190 template<typename T, typename PluginBaseType, typename... Bases>
191 void
193 {
194  TfType type = TfType::Define<T,
195  TfType::Bases<PluginBaseType, Bases...> >();
196 
197  _FactoryFn func = &_CreatePlugin<T>;
198  _SetFactory(type, func);
199 }
200 
201 
202 
203 PXR_NAMESPACE_CLOSE_SCOPE
204 
205 #endif //HF_PLUGIN_REGISTRY_H
206 
Base class for registering Hydra plugins using the plug mechanism.
HF_API bool IsRegisteredPlugin(const TfToken &pluginId)
Returns true if a plugin has been registered for the given id.
HF_API void ReleasePlugin(HfPluginBase *plugin)
Decrement the reference count on the plugin.
HF_API void GetPluginDescs(HfPluginDescVector *plugins)
Returns an ordered list of all registered plugins.
static void Define()
Entry point for registering a types implementation.
Base class for all hydra plugin classes.
Definition: pluginBase.h:39
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
HF_API HfPluginBase * GetPlugin(const TfToken &pluginId)
Returns the plugin from the given pluginId.
HF_API bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc)
Returns the description for the given plugin id.
A type-list of C++ base types.
Definition: type.h:106
HF_API void AddPluginReference(HfPluginBase *plugin)
Increment the reference count on an existing plugin.
static TfType const & Define()
Define a TfType with the given C++ type T and C++ base types B.
Definition: type_Impl.h:78
TfType represents a dynamic runtime type.
Definition: type.h:70
Common structure used to report registered plugins in one of the plugin registries.
Definition: pluginDesc.h:47