All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
registry.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_BASE_PLUG_REGISTRY_H
25 #define PXR_BASE_PLUG_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/plug/api.h"
29 
30 #include "pxr/base/js/value.h"
32 #include "pxr/base/tf/hash.h"
33 #include "pxr/base/tf/hashset.h"
34 #include "pxr/base/tf/refPtr.h"
35 #include "pxr/base/tf/singleton.h"
36 #include "pxr/base/tf/type.h"
37 #include "pxr/base/tf/weakBase.h"
38 #include "pxr/base/tf/weakPtr.h"
39 
40 #include <mutex>
41 #include <string>
42 #include <vector>
43 
44 PXR_NAMESPACE_OPEN_SCOPE
45 
47 class Plug_RegistrationMetadata;
48 
335 
336 class PlugRegistry : public TfWeakBase {
337  PlugRegistry(PlugRegistry const &) = delete;
338  PlugRegistry &operator=(PlugRegistry const &) = delete;
339 public:
340  typedef PlugRegistry This;
341  typedef std::vector<TfType> TypeVector;
342 
344  PLUG_API
345  static PlugRegistry & GetInstance();
346 
349  PLUG_API
350  PlugPluginPtrVector RegisterPlugins(const std::string & pathToPlugInfo);
351 
354  PLUG_API
355  PlugPluginPtrVector
356  RegisterPlugins(const std::vector<std::string> & pathsToPlugInfo);
357 
366  PLUG_API
367  static TfType FindTypeByName(std::string const &typeName);
368 
378  PLUG_API
379  static TfType
380  FindDerivedTypeByName(TfType base, std::string const &typeName);
381 
391  template <class Base>
392  static TfType
393  FindDerivedTypeByName(std::string const &typeName) {
394  return FindDerivedTypeByName(TfType::Find<Base>(), typeName);
395  }
396 
401  PLUG_API
402  static std::vector<TfType>
404 
411  PLUG_API
412  static void
413  GetAllDerivedTypes(TfType base, std::set<TfType> *result);
414 
421  template <class Base>
422  static void
423  GetAllDerivedTypes(std::set<TfType> *result) {
424  return GetAllDerivedTypes(TfType::Find<Base>(), result);
425  }
426 
429  PLUG_API
430  PlugPluginPtr GetPluginForType(TfType t) const;
431 
434  PLUG_API
435  PlugPluginPtrVector GetAllPlugins() const;
436 
440  PLUG_API
441  PlugPluginPtr GetPluginWithName(const std::string& name) const;
442 
445  PLUG_API
446  std::string GetStringFromPluginMetaData(TfType type,
447  const std::string &key) const;
448 
451  PLUG_API
453  const std::string &key) const;
454 
455 private:
456  // Private ctor and dtor since this is a constructed as a singleton.
457  PLUG_LOCAL
458  PlugRegistry();
459 
460  // Registers all plug-ins discovered in any of \a pathsToPlugInfo
461  // but does not send a notice. The priority order of paths is honored if
462  // pathsAreOrdered.
463  PLUG_LOCAL
464  PlugPluginPtrVector
465  _RegisterPlugins(const std::vector<std::string>& pathsToPlugInfo,
466  bool pathsAreOrdered);
467 
468  template <class ConcurrentVector>
469  PLUG_LOCAL
470  void _RegisterPlugin(const Plug_RegistrationMetadata&,
471  ConcurrentVector *newPlugins);
472  PLUG_LOCAL
473  bool _InsertRegisteredPluginPath(const std::string &path);
474 
475  friend class TfSingleton< PlugRegistry >;
476  friend class PlugPlugin; // For _RegisterPlugins().
477 
478 private:
479  TfHashSet<std::string, TfHash> _registeredPluginPaths;
480 
481  std::mutex _mutex;
482 };
483 
484 PLUG_API_TEMPLATE_CLASS(TfSingleton<PlugRegistry>);
485 
486 PXR_NAMESPACE_CLOSE_SCOPE
487 
488 #endif // PXR_BASE_PLUG_REGISTRY_H
static PLUG_API TfType FindTypeByName(std::string const &typeName)
Retrieve the TfType corresponding to the given name.
Manage a single instance of an object.
static void GetAllDerivedTypes(std::set< TfType > *result)
Return the set of all types derived (directly or indirectly) from Base.
Definition: registry.h:423
Manage a single instance of an object (see.
Definition: singleton.h:122
#define TF_DECLARE_WEAK_PTRS(type)
Define standard weak pointer types.
Definition: declarePtrs.h:62
Standard pointer typedefs.
Defines an interface for registering plugins.
Definition: registry.h:336
static PLUG_API std::vector< TfType > GetDirectlyDerivedTypes(TfType base)
Return a vector of types derived directly from base.
PLUG_API PlugPluginPtr GetPluginForType(TfType t) const
Returns the plug-in for the given type, or a null pointer if there is no registered plug-in...
PLUG_API JsValue GetDataFromPluginMetaData(TfType type, const std::string &key) const
Looks for a JsValue associated with type and key and returns it, or a null JsValue if type or key are...
Pointer storage with deletion detection.
static TfType FindDerivedTypeByName(std::string const &typeName)
Retrieve the TfType that derives from Base and has the given alias or type name typeName.
Definition: registry.h:393
A discriminated union type for JSON values.
Definition: value.h:61
PLUG_API PlugPluginPtrVector GetAllPlugins() const
Returns all registered plug-ins.
PLUG_API PlugPluginPtrVector RegisterPlugins(const std::string &pathToPlugInfo)
Registers all plug-ins discovered at pathToPlugInfo.
static PLUG_API PlugRegistry & GetInstance()
Returns the singleton PlugRegistry instance.
PLUG_API PlugPluginPtr GetPluginWithName(const std::string &name) const
Returns a plugin with the specified library name.
Defines an interface to registered plugins.
Definition: plugin.h:58
PLUG_API std::string GetStringFromPluginMetaData(TfType type, const std::string &key) const
Looks for a string associated with type and key and returns it, or an empty string if type or key are...
Reference counting.
static PLUG_API void GetAllDerivedTypes(TfType base, std::set< TfType > *result)
Return the set of all types derived (directly or indirectly) from base.
static PLUG_API TfType FindDerivedTypeByName(TfType base, std::string const &typeName)
Retrieve the TfType that derives from base and has the given alias or type name typeName.
TfType represents a dynamic runtime type.
Definition: type.h:64
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:141