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 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"
14 #include "pxr/base/plug/registry.h"
15 #include "pxr/base/tf/type.h"
16 
17 #include <map>
18 
20 
21 
22 class HfPluginBase;
23 class Hf_PluginEntry;
24 
25 
26 ///
27 /// \class HfPluginRegistry
28 ///
29 /// Base class for registering Hydra plugins using the plug mechanism.
30 /// It is expected that each plugin has a pluginfo.json file that contains
31 /// a list of types, where each type provides a list of base classes,
32 /// displayName and priority.
33 ///
34 /// The priority is used to order plugins, with the plugin with the highest
35 /// priority being at the front of the order. priority is a signed integer.
36 /// In the event of two plugins having the same priority, the plugins are sorted
37 /// alphabetically on the type name.
38 ///
39 /// The plugin sorted to the front is used as the default plugin, when not
40 /// specified.
41 ///
42 /// Example:
43 ///
44 ///{
45 /// "Types": {
46 /// "CPPTypeName": {
47 /// "bases": ["BaseTypeName"],
48 /// "displayName": "Human Readable Name",
49 /// "priority" : 0
50 /// }
51 /// }
52 ///}
53 ///
55 {
56 
57 public:
58  ///
59  /// Returns an ordered list of all registered plugins.
60  /// The plugins are ordered by priority then alphabetically
61  ///
62  HF_API
63  void GetPluginDescs(HfPluginDescVector *plugins);
64 
65  ///
66  /// Returns the description for the given plugin id.
67  /// The plugin may not be loaded or been actually created yet.
68  ///
69  HF_API
70  bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc);
71 
72  ///
73  /// Increment the reference count on an existing plugin.
74  ///
75  HF_API
76  void AddPluginReference(HfPluginBase *plugin);
77 
78  ///
79  /// Decrement the reference count on the plugin. If the
80  /// reference count get to 0, the plugin is freed.
81  ///
82  HF_API
83  void ReleasePlugin(HfPluginBase *plugin);
84 
85  ///
86  /// Returns true if a plugin has been registered for the given id.
87  /// The plugin may not be loaded or been actually created yet.
88  ///
89  HF_API
90  bool IsRegisteredPlugin(const TfToken &pluginId);
91 
92  HF_API
93  TfToken GetPluginId(const HfPluginBase *plugin) const;
94 
95 protected:
96  // Must be derived.
97 
98  ///
99  /// Constructs a Plugin Registry.
100  /// pluginBaseType is the TfType of the class derived from
101  /// HfPluginBase that provides the plugin API.
102  ///
103  HF_API
104  HfPluginRegistry(const TfType &pluginBaseType);
105  HF_API
106  virtual ~HfPluginRegistry();
107 
108  ///
109  /// Returns the plugin from the given pluginId.
110  /// The reference count on the plugin is automatically increased.
111  ///
112  HF_API
113  HfPluginBase *GetPlugin(const TfToken &pluginId);
114 
115  ///
116  /// Entry point for registering a types implementation.
117  /// T is the plugin being registered.
118  /// PluginBaseType is the HfPluginBase derived class
119  /// that specifies the API (the same one the TfType is for in
120  /// the constructor).
121  ///
122  /// Bases optionally specifies other classes that T is derived from.
123  ///
124  template<typename T, typename PluginBaseType, typename... Bases>
125  static void Define();
126 
127  /// Gives subclasses an opportunity to inspect plugInfo-based metadata
128  /// at the time of discovery.
129  HF_API
130  virtual void _CollectAdditionalMetadata(
131  const PlugRegistry &plugRegistry, const TfType &pluginType);
132 
133 private:
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
141  ///
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 
168  ///
169  /// This class is not intended to be copied.
170  ///
171  HfPluginRegistry() = delete;
172  HfPluginRegistry(const HfPluginRegistry &) = delete;
173  HfPluginRegistry &operator=(const HfPluginRegistry &) = delete;
174 };
175 
176 template<typename T>
177 HfPluginBase *
178 HfPluginRegistry::_CreatePlugin()
179 {
181  return new T;
182 }
183 
184 
185 template<typename T, typename PluginBaseType, typename... Bases>
186 void
188 {
190  TfType::Bases<PluginBaseType, Bases...> >();
191 
192  _FactoryFn func = &_CreatePlugin<T>;
193  _SetFactory(type, func);
194 }
195 
196 
197 
199 
200 #endif //PXR_IMAGING_HF_PLUGIN_REGISTRY_H
201 
#define HF_API
Definition: api.h:23
HF_API TfToken GetPluginId(const HfPluginBase *plugin) const
#define HF_MALLOC_TAG_FUNCTION()
Definition: perfLog.h:20
virtual HF_API void _CollectAdditionalMetadata(const PlugRegistry &plugRegistry, const TfType &pluginType)
HF_API void ReleasePlugin(HfPluginBase *plugin)
HF_API bool IsRegisteredPlugin(const TfToken &pluginId)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
Definition: token.h:70
static TfType const & Define()
Definition: type_Impl.h:61
HF_API bool GetPluginDesc(const TfToken &pluginId, HfPluginDesc *desc)
HF_API void GetPluginDescs(HfPluginDescVector *plugins)
GLenum func
Definition: glcorearb.h:783
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
static void Define()
HF_API HfPluginBase * GetPlugin(const TfToken &pluginId)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
std::vector< HfPluginDesc > HfPluginDescVector
Definition: pluginDesc.h:36
HF_API void AddPluginReference(HfPluginBase *plugin)
virtual HF_API ~HfPluginRegistry()