HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plugin.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_BASE_PLUG_PLUGIN_H
8 #define PXR_BASE_PLUG_PLUGIN_H
9 
10 #include "pxr/pxr.h"
11 #include "pxr/base/plug/api.h"
12 
13 #include "pxr/base/js/types.h"
15 #include "pxr/base/tf/weakBase.h"
16 
17 #include <atomic>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
23 
25 
26 class Plug_RegistrationMetadata;
27 class TfType;
28 
29 /// \class PlugPlugin
30 ///
31 /// Defines an interface to registered plugins.
32 ///
33 /// Plugins are registered using the interfaces in \c PlugRegistry.
34 ///
35 /// For each registered plugin, there is an instance of
36 /// \c PlugPlugin which can be used to load and unload
37 /// the plugin and to retrieve information about the
38 /// classes implemented by the plugin.
39 ///
40 class PlugPlugin : public TfWeakBase {
41 public:
43 
44  /// Loads the plugin.
45  /// This is a noop if the plugin is already loaded.
46  PLUG_API bool Load();
47 
48  /// Returns \c true if the plugin is currently loaded. Resource
49  /// plugins always report as loaded.
50  PLUG_API bool IsLoaded() const;
51 
52 #ifdef PXR_PYTHON_SUPPORT_ENABLED
53  /// Returns \c true if the plugin is a python module.
54  PLUG_API bool IsPythonModule() const;
55 #endif // PXR_PYTHON_SUPPORT_ENABLED
56 
57  /// Returns \c true if the plugin is resource-only.
58  PLUG_API bool IsResource() const;
59 
60  /// Returns the dictionary containing meta-data for the plugin.
62 
63  /// Returns the metadata sub-dictionary for a particular type.
65 
66  /// Returns the dictionary containing the dependencies for the plugin.
68 
69  /// Returns true if \p type is declared by this plugin.
70  /// If \p includeSubclasses is specified, also returns true if any
71  /// subclasses of \p type have been declared.
72  PLUG_API bool DeclaresType(const TfType& type, bool includeSubclasses = false) const;
73 
74  /// Returns the plugin's name.
75  std::string const &GetName() const {
76  return _name;
77  }
78 
79  /// Returns the plugin's filesystem path.
80  std::string const &GetPath() const {
81  return _path;
82  }
83 
84  /// Returns the plugin's resources filesystem path.
85  std::string const &GetResourcePath() const {
86  return _resourcePath;
87  }
88 
89  /// Build a plugin resource path by returning a given absolute path or
90  /// combining the plugin's resource path with a given relative path.
91  PLUG_API std::string MakeResourcePath(const std::string& path) const;
92 
93  /// Find a plugin resource by absolute or relative path optionally
94  /// verifying that file exists. If verification fails an empty path
95  /// is returned. Relative paths are relative to the plugin's resource
96  /// path.
97  PLUG_API std::string FindPluginResource(const std::string& path, bool verify = true) const;
98 
99 private:
100  enum _Type {
101  LibraryType,
102  PythonType,
103  ResourceType
104  };
105 
106  // Private ctor, plugins are constructed only by PlugRegistry.
107  PLUG_LOCAL
108  PlugPlugin(const std::string & path,
109  const std::string & name,
110  const std::string & resourcePath,
111  const JsObject & plugInfo,
112  _Type type);
113 
114  PLUG_LOCAL
115  static PlugPluginPtr _GetPluginForType(const TfType & type);
116 
117  PLUG_LOCAL
118  static void _RegisterAllPlugins();
119  PLUG_LOCAL
120  static PlugPluginPtr _GetPluginWithName(const std::string& name);
121  PLUG_LOCAL
122  static PlugPluginPtrVector _GetAllPlugins();
123 
124  template <class PluginMap>
125  PLUG_LOCAL
126  static std::pair<PlugPluginPtr, bool>
127  _NewPlugin(const Plug_RegistrationMetadata &metadata,
128  _Type pluginType,
129  const std::string& pluginCreationPath,
130  PluginMap *allPluginsByNamePtr);
131 
132  PLUG_LOCAL
133  static std::pair<PlugPluginPtr, bool>
134  _NewDynamicLibraryPlugin(const Plug_RegistrationMetadata& metadata);
135 
136 #ifdef PXR_PYTHON_SUPPORT_ENABLED
137  PLUG_LOCAL
138  static std::pair<PlugPluginPtr, bool>
139  _NewPythonModulePlugin(const Plug_RegistrationMetadata& metadata);
140 #endif // PXR_PYTHON_SUPPORT_ENABLED
141 
142  PLUG_LOCAL
143  static std::pair<PlugPluginPtr, bool>
144  _NewResourcePlugin(const Plug_RegistrationMetadata& metadata);
145 
146  PLUG_LOCAL
147  bool _Load();
148 
149  PLUG_LOCAL
150  void _DeclareAliases( TfType t, const JsObject & metadata );
151  PLUG_LOCAL
152  void _DeclareTypes();
153  PLUG_LOCAL
154  void _DeclareType(const std::string &name, const JsObject &dict);
155  PLUG_LOCAL
156  static void _DefineType( TfType t );
157 
158  struct _SeenPlugins;
159  PLUG_LOCAL
160  bool _LoadWithDependents(_SeenPlugins * seenPlugins);
161 
162  PLUG_LOCAL
163  static void _UpdatePluginMaps( const TfType & baseType );
164 
165  PLUG_LOCAL
166  static constexpr char const *_GetPluginTypeDisplayName(_Type type);
167 
168 private:
169  std::string _name;
170  std::string _path;
171  std::string _resourcePath;
172  JsObject _dict;
173  void *_handle; // the handle returned by ArchLibraryOpen() is a void*
174  std::atomic<bool> _isLoaded;
175  _Type _type;
176 
177  friend class PlugRegistry;
178 };
179 
180 /// Find a plugin's resource by absolute or relative path optionally
181 /// verifying that file exists. If \c plugin is \c NULL or verification
182 /// fails an empty path is returned. Relative paths are relative to the
183 /// plugin's resource path.
184 PLUG_API
185 std::string
186 PlugFindPluginResource(const PlugPluginPtr& plugin,
187  const std::string& path, bool verify = true);
188 
190 
191 #endif // PXR_BASE_PLUG_PLUGIN_H
PLUG_API JsObject GetMetadataForType(const TfType &type)
Returns the metadata sub-dictionary for a particular type.
std::string const & GetPath() const
Returns the plugin's filesystem path.
Definition: plugin.h:80
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
std::string const & GetName() const
Returns the plugin's name.
Definition: plugin.h:75
PXR_NAMESPACE_OPEN_SCOPE TF_DECLARE_WEAK_PTRS(PlugPlugin)
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:108
#define PLUG_LOCAL
Definition: api.h:27
PLUG_API bool IsResource() const
Returns true if the plugin is resource-only.
GLuint const GLchar * name
Definition: glcorearb.h:786
std::map< std::string, JsValue > JsObject
Definition: types.h:20
GLdouble t
Definition: glad.h:2397
PLUG_API JsObject GetDependencies()
Returns the dictionary containing the dependencies for the plugin.
PLUG_API std::string FindPluginResource(const std::string &path, bool verify=true) const
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
PLUG_API std::string PlugFindPluginResource(const PlugPluginPtr &plugin, const std::string &path, bool verify=true)
#define PLUG_API
Definition: api.h:23
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
Definition: type.h:47
PLUG_API JsObject GetMetadata()
Returns the dictionary containing meta-data for the plugin.
PLUG_API bool IsLoaded() const
PLUG_API bool Load()
PLUG_API std::string MakeResourcePath(const std::string &path) const
PLUG_API bool DeclaresType(const TfType &type, bool includeSubclasses=false) const
PLUG_API ~PlugPlugin()
std::string const & GetResourcePath() const
Returns the plugin's resources filesystem path.
Definition: plugin.h:85