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 PXR_IMAGING_HF_PLUGIN_REGISTRY_H
25 #define PXR_IMAGING_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  HF_API
108  TfToken GetPluginId(const HfPluginBase *plugin) const;
109 
110 protected:
111  // Must be derived.
112 
118  HF_API
119  HfPluginRegistry(const TfType &pluginBaseType);
120  HF_API
121  virtual ~HfPluginRegistry();
122 
127  HF_API
128  HfPluginBase *GetPlugin(const TfToken &pluginId);
129 
139  template<typename T, typename PluginBaseType, typename... Bases>
140  static void Define();
141 
142 private:
143  typedef std::vector<Hf_PluginEntry> _PluginEntryVector;
144  typedef std::map<TfToken, size_t> _TokenMap;
145 
146  //
147  // The purpose of this group of functions is to provide a factory
148  // to create the registered plugin with the type system
149  // without exposing the internal class _PluginEntry
151  typedef std::function<HfPluginBase *()> _FactoryFn;
152 
153  template<typename T>
154  static HfPluginBase *_CreatePlugin();
155  HF_API
156  static void _SetFactory(TfType &type, _FactoryFn &func);
157 
158  TfType _pluginBaseType;
159 
160  //
161  // Plugins are stored in a ordered list (as a vector). The token
162  // map converts from plugin id into an index in the list.
163  //
164  _PluginEntryVector _pluginEntries;
165  _TokenMap _pluginIndex;
166 
167  // Plugin discovery is deferred until first use.
168  bool _pluginCachePopulated;
169 
170  // Use the Plug system to discover plugins from the meta data.
171  void _DiscoverPlugins();
172 
173  // Find the plugin entry for the given plugin object.
174  Hf_PluginEntry *_GetEntryForPlugin(HfPluginBase *plugin);
175 
179  HfPluginRegistry() = delete;
180  HfPluginRegistry(const HfPluginRegistry &) = delete;
181  HfPluginRegistry &operator=(const HfPluginRegistry &) = delete;
182 };
183 
184 template<typename T>
185 HfPluginBase *
186 HfPluginRegistry::_CreatePlugin()
187 {
188  HF_MALLOC_TAG_FUNCTION();
189  return new T;
190 }
191 
192 
193 template<typename T, typename PluginBaseType, typename... Bases>
194 void
196 {
197  TfType type = TfType::Define<T,
198  TfType::Bases<PluginBaseType, Bases...> >();
199 
200  _FactoryFn func = &_CreatePlugin<T>;
201  _SetFactory(type, func);
202 }
203 
204 
205 
206 PXR_NAMESPACE_CLOSE_SCOPE
207 
208 #endif //PXR_IMAGING_HF_PLUGIN_REGISTRY_H
209 
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:87
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:100
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:90
TfType represents a dynamic runtime type.
Definition: type.h:64
Common structure used to report registered plugins in one of the plugin registries.
Definition: pluginDesc.h:47