All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plugin.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 PLUG_PLUGIN_H
25 #define PLUG_PLUGIN_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/plug/api.h"
29 
30 #include "pxr/base/js/types.h"
31 #include "pxr/base/tf/declarePtrs.h"
32 #include "pxr/base/tf/refPtr.h"
33 #include "pxr/base/tf/weakPtr.h"
34 
35 #include <boost/noncopyable.hpp>
36 #include <atomic>
37 #include <string>
38 #include <utility>
39 #include <vector>
40 
41 PXR_NAMESPACE_OPEN_SCOPE
42 
44 
45 class Plug_RegistrationMetadata;
46 class TfType;
47 
59 class PlugPlugin : public TfRefBase, public TfWeakBase {
60 public:
61  PLUG_API virtual ~PlugPlugin();
62 
65  PLUG_API bool Load();
66 
69  PLUG_API bool IsLoaded() const;
70 
71 #ifdef PXR_PYTHON_SUPPORT_ENABLED
72  PLUG_API bool IsPythonModule() const;
74 #endif // PXR_PYTHON_SUPPORT_ENABLED
75 
77  PLUG_API bool IsResource() const;
78 
80  PLUG_API JsObject GetMetadata();
81 
83  PLUG_API JsObject GetMetadataForType(const TfType &type);
84 
86  PLUG_API JsObject GetDependencies();
87 
91  PLUG_API bool DeclaresType(const TfType& type, bool includeSubclasses = false) const;
92 
94  std::string const &GetName() const {
95  return _name;
96  }
97 
99  std::string const &GetPath() const {
100  return _path;
101  }
102 
104  std::string const &GetResourcePath() const {
105  return _resourcePath;
106  }
107 
110  PLUG_API std::string MakeResourcePath(const std::string& path) const;
111 
116  PLUG_API std::string FindPluginResource(const std::string& path, bool verify = true) const;
117 
118 private:
119  enum _Type {
120  LibraryType,
121 #ifdef PXR_PYTHON_SUPPORT_ENABLED
122  PythonType,
123 #endif // PXR_PYTHON_SUPPORT_ENABLED
124  ResourceType
125  };
126 
127  // Private ctor, plugins are constructed only by PlugRegistry.
128  PLUG_LOCAL
129  PlugPlugin(const std::string & path,
130  const std::string & name,
131  const std::string & resourcePath,
132  const JsObject & plugInfo,
133  _Type type);
134 
135  PLUG_LOCAL
136  static PlugPluginPtr _GetPluginForType(const TfType & type);
137 
138  PLUG_LOCAL
139  static void _RegisterAllPlugins();
140  PLUG_LOCAL
141  static PlugPluginPtr _GetPluginWithName(const std::string& name);
142  PLUG_LOCAL
143  static PlugPluginPtrVector _GetAllPlugins();
144 
145  template <class PluginMap>
146  PLUG_LOCAL
147  static std::pair<PlugPluginPtr, bool>
148  _NewPlugin(const Plug_RegistrationMetadata &metadata,
149  _Type pluginType,
150  const std::string& pluginCreationPath,
151  PluginMap *allPluginsByNamePtr);
152 
153  PLUG_LOCAL
154  static std::pair<PlugPluginPtr, bool>
155  _NewDynamicLibraryPlugin(const Plug_RegistrationMetadata& metadata);
156 
157 #ifdef PXR_PYTHON_SUPPORT_ENABLED
158  PLUG_LOCAL
159  static std::pair<PlugPluginPtr, bool>
160  _NewPythonModulePlugin(const Plug_RegistrationMetadata& metadata);
161 #endif // PXR_PYTHON_SUPPORT_ENABLED
162 
163  PLUG_LOCAL
164  static std::pair<PlugPluginPtr, bool>
165  _NewResourcePlugin(const Plug_RegistrationMetadata& metadata);
166 
167  PLUG_LOCAL
168  bool _Load();
169 
170  PLUG_LOCAL
171  void _DeclareAliases( TfType t, const JsObject & metadata );
172  PLUG_LOCAL
173  void _DeclareTypes();
174  PLUG_LOCAL
175  void _DeclareType(const std::string &name, const JsObject &dict);
176  PLUG_LOCAL
177  static void _DefineType( TfType t );
178 
179  struct _SeenPlugins;
180  PLUG_LOCAL
181  bool _LoadWithDependents(_SeenPlugins * seenPlugins);
182 
183  PLUG_LOCAL
184  static void _UpdatePluginMaps( const TfType & baseType );
185 
186  PLUG_LOCAL
187  static constexpr char const *_GetPluginTypeDisplayName(_Type type);
188 
189 private:
190  std::string _name;
191  std::string _path;
192  std::string _resourcePath;
193  JsObject _dict;
194  void *_handle; // the handle returned by ArchLibraryOpen() is a void*
195  std::atomic<bool> _isLoaded;
196  _Type _type;
197 
198  friend class PlugRegistry;
199 };
200 
205 PLUG_API
206 std::string
207 PlugFindPluginResource(const PlugPluginPtr& plugin,
208  const std::string& path, bool verify = true);
209 
210 PXR_NAMESPACE_CLOSE_SCOPE
211 
212 #endif // PLUG_PLUGIN_H
PLUG_API std::string MakeResourcePath(const std::string &path) const
Build a plugin resource path by returning a given absolute path or combining the plugin&#39;s resource pa...
std::string const & GetResourcePath() const
Returns the plugin&#39;s resources filesystem path.
Definition: plugin.h:104
PLUG_API JsObject GetMetadata()
Returns the dictionary containing meta-data for the plugin.
Defines an interface for registering plugins.
Definition: registry.h:336
PLUG_API JsObject GetMetadataForType(const TfType &type)
Returns the metadata sub-dictionary for a particular type.
std::string const & GetPath() const
Returns the plugin&#39;s filesystem path.
Definition: plugin.h:99
PLUG_API std::string FindPluginResource(const std::string &path, bool verify=true) const
Find a plugin resource by absolute or relative path optionally verifying that file exists...
#define TF_DECLARE_WEAK_AND_REF_PTRS(type)
Define standard weak, ref, and vector pointer types.
Definition: declarePtrs.h:89
PLUG_API JsObject GetDependencies()
Returns the dictionary containing the dependencies for the plugin.
PLUG_API bool IsResource() const
Returns true if the plugin is resource-only.
Enable a concrete base class for use with TfRefPtr.
Definition: refBase.h:72
PLUG_API bool DeclaresType(const TfType &type, bool includeSubclasses=false) const
Returns true if type is declared by this plugin.
Defines an interface to registered plugins.
Definition: plugin.h:59
std::string const & GetName() const
Returns the plugin&#39;s name.
Definition: plugin.h:94
TfType represents a dynamic runtime type.
Definition: type.h:70
PLUG_API bool Load()
Loads the plugin.
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142
PLUG_API bool IsLoaded() const
Returns true if the plugin is currently loaded.