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 PLUG_REGISTRY_H
25 #define PLUG_REGISTRY_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/base/plug/api.h"
29 
30 #include "pxr/base/js/value.h"
31 #include "pxr/base/tf/declarePtrs.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.
462  PLUG_LOCAL
463  PlugPluginPtrVector
464  _RegisterPlugins(const std::vector<std::string>& pathsToPlugInfo);
465 
466  template <class ConcurrentVector>
467  PLUG_LOCAL
468  void _RegisterPlugin(const Plug_RegistrationMetadata&,
469  ConcurrentVector *newPlugins);
470  PLUG_LOCAL
471  bool _InsertRegisteredPluginPath(const std::string &path);
472 
473  friend class TfSingleton< PlugRegistry >;
474  friend class PlugPlugin; // For _RegisterPlugins().
475 
476 private:
477  TfHashSet<std::string, TfHash> _registeredPluginPaths;
478 
479  std::mutex _mutex;
480 };
481 
482 PLUG_API_TEMPLATE_CLASS(TfSingleton<PlugRegistry>);
483 
484 PXR_NAMESPACE_CLOSE_SCOPE
485 
486 #endif // PLUG_REGISTRY_H
static PLUG_API TfType FindTypeByName(std::string const &typeName)
Retrieve the TfType corresponding to the given name.
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
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...
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:59
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...
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:70
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142