HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pluginRegistry.h
Go to the documentation of this file.
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"
31 #include "pxr/base/tf/type.h"
32 #include <map>
33 
35 
36 
37 class HfPluginBase;
38 class Hf_PluginEntry;
39 
40 
41 ///
42 /// \class HfPluginRegistry
43 ///
44 /// Base class for registering Hydra plugins using the plug mechanism.
45 /// It is expected that each plugin has a pluginfo.json file that contains
46 /// a list of types, where each type provides a list of base classes,
47 /// displayName and priority.
48 ///
49 /// The priority is used to order plugins, with the plugin with the highest
50 /// priority being at the front of the order. priority is a signed integer.
51 /// In the event of two plugins having the same priority, the plugins are sorted
52 /// alphabetically on the type name.
53 ///
54 /// The plugin sorted to the front is used as the default plugin, when not
55 /// specified.
56 ///
57 /// Example:
58 ///
59 ///{
60 /// "Types": {
61 /// "CPPTypeName": {
62 /// "bases": ["BaseTypeName"],
63 /// "displayName": "Human Readable Name",
64 /// "priority" : 0
65 /// }
66 /// }
67 ///}
68 ///
70 {
71 
72 public:
73  ///
74  /// Returns an ordered list of all registered plugins.
75  /// The plugins are ordered by priority then alphabetically
76  ///
77  HF_API
78  void GetPluginDescs(HfPluginDescVector *plugins);
79 
80  ///
81  /// Returns the description for the given plugin id.
82  /// The plugin may not be loaded or been actually created yet.
83  ///
84  HF_API
85  bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc);
86 
87  ///
88  /// Increment the reference count on an existing plugin.
89  ///
90  HF_API
91  void AddPluginReference(HfPluginBase *plugin);
92 
93  ///
94  /// Decrement the reference count on the plugin. If the
95  /// reference count get to 0, the plugin is freed.
96  ///
97  HF_API
98  void ReleasePlugin(HfPluginBase *plugin);
99 
100  ///
101  /// Returns true if a plugin has been registered for the given id.
102  /// The plugin may not be loaded or been actually created yet.
103  ///
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 
113  ///
114  /// Constructs a Plugin Registry.
115  /// pluginBaseType is the TfType of the class derived from
116  /// HfPluginBase that provides the plugin API.
117  ///
118  HF_API
119  HfPluginRegistry(const TfType &pluginBaseType);
120  HF_API
121  virtual ~HfPluginRegistry();
122 
123  ///
124  /// Returns the plugin from the given pluginId.
125  /// The reference count on the plugin is automatically increased.
126  ///
127  HF_API
128  HfPluginBase *GetPlugin(const TfToken &pluginId);
129 
130  ///
131  /// Entry point for registering a types implementation.
132  /// T is the plugin being registered.
133  /// PluginBaseType is the HfPluginBase derived class
134  /// that specifies the API (the same one the TfType is for in
135  /// the constructor).
136  ///
137  /// Bases optionally specifies other classes that T is derived from.
138  ///
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
150  ///
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 
176  ///
177  /// This class is not intended to be copied.
178  ///
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 {
189  return new T;
190 }
191 
192 
193 template<typename T, typename PluginBaseType, typename... Bases>
194 void
196 {
198  TfType::Bases<PluginBaseType, Bases...> >();
199 
200  _FactoryFn func = &_CreatePlugin<T>;
201  _SetFactory(type, func);
202 }
203 
204 
205 
207 
208 #endif //PXR_IMAGING_HF_PLUGIN_REGISTRY_H
209 
#define HF_API
Definition: api.h:40
HF_API TfToken GetPluginId(const HfPluginBase *plugin) const
#define HF_MALLOC_TAG_FUNCTION()
Definition: perfLog.h:37
HF_API void ReleasePlugin(HfPluginBase *plugin)
HF_API bool IsRegisteredPlugin(const TfToken &pluginId)
Definition: token.h:87
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
static TfType const & Define()
Definition: type_Impl.h:90
HF_API bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc)
HF_API void GetPluginDescs(HfPluginDescVector *plugins)
GLenum func
Definition: glcorearb.h:782
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1375
static void Define()
HF_API HfPluginBase * GetPlugin(const TfToken &pluginId)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
Definition: type.h:64
std::vector< HfPluginDesc > HfPluginDescVector
Definition: pluginDesc.h:53
HF_API void AddPluginReference(HfPluginBase *plugin)
virtual HF_API ~HfPluginRegistry()