HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scriptModuleLoader.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_TF_SCRIPT_MODULE_LOADER_H
8 #define PXR_BASE_TF_SCRIPT_MODULE_LOADER_H
9 
10 #include "pxr/pxr.h"
11 
12 #include "pxr/base/tf/api.h"
13 #include "pxr/base/tf/hash.h"
14 #include "pxr/base/tf/singleton.h"
16 #include "pxr/base/tf/token.h"
17 #include "pxr/base/tf/weakBase.h"
18 
19 // XXX: This include is a hack to avoid build errors due to
20 // incompatible macro definitions in pyport.h on macOS.
21 #include <locale>
22 #include "pxr/external/boost/python/dict.hpp"
23 
24 #include <deque>
25 #include <string>
26 #include <unordered_map>
27 #include <vector>
28 
30 
31 /// \class TfScriptModuleLoader
32 ///
33 /// Provides low-level facilities for shared libraries with script bindings to
34 /// register themselves with their dependences, and provides a mechanism
35 /// whereby those script modules will be loaded when necessary. Currently,
36 /// this is when one of our script modules is loaded, when TfPyInitialize is
37 /// called, and when Plug opens shared libraries.
38 ///
39 /// Generally, user code will not make use of this.
40 ///
42 
43  public:
44 
46 
47  /// Return the singleton instance.
48  TF_API static This &GetInstance() {
50  }
51 
52  /// Register a library named \a name and with script module \a moduleName
53  /// and libraries which must be loaded first \a predecessors. The script
54  /// module will be loaded when necessary. This should generally not be
55  /// called by user code.
56  TF_API
57  void RegisterLibrary(TfToken const &name, TfToken const &moduleName,
58  std::vector<TfToken> const &predecessors);
59 
60  /// Load all the script modules for any libraries registered using \a
61  /// RegisterLibrary if necessary. Loads the modules in dependency order as
62  /// defined in \a RegisterLibrary.
63  TF_API
64  void LoadModules();
65 
66  /// Load all the script modules for any libraries registered using \a
67  /// RegisterLibrary that depend on library \a name.
68  TF_API
69  void LoadModulesForLibrary(TfToken const &name);
70 
71  /// Return a python dict containing all currently known modules under
72  /// their canonical names.
73  TF_API
74  pxr_boost::python::dict GetModulesDict() const;
75 
76  /// Write a graphviz dot-file for the dependency graph of all currently
77  /// registered libraries/modules to \a file.
78  TF_API
79  void WriteDotFile(std::string const &file) const;
80 
81  private:
82  friend class TfSingleton<This>;
83 
84  struct _LibInfo {
85  _LibInfo() = default;
86  _LibInfo(TfToken const &moduleName,
87  std::vector<TfToken> &&predecessors)
88  : moduleName(moduleName)
89  , predecessors(predecessors) { }
90 
91  TfToken moduleName;
92  std::vector<TfToken> predecessors;
93  mutable std::atomic<bool> isLoaded = false;
94  };
95 
96  using _LibInfoMap =
97  std::unordered_map<TfToken, _LibInfo, TfToken::HashFunctor>;
98 
99  using _LibAndInfo = _LibInfoMap::value_type;
100 
102  virtual ~TfScriptModuleLoader();
103 
104  _LibInfo const *_FindInfo(TfToken const &lib) const;
105 
106  void _LoadLibModules(std::vector<_LibAndInfo const *> const &toLoad) const;
107 
108  _LibInfoMap _libInfo;
109  mutable TfSpinRWMutex _mutex;
110 };
111 
113 
115 
116 #endif // PXR_BASE_TF_SCRIPT_MODULE_LOADER_H
static T & GetInstance()
Definition: singleton.h:120
#define TF_API
Definition: api.h:23
TF_API_TEMPLATE_CLASS(TfSingleton< TfScriptModuleLoader >)
static TF_API This & GetInstance()
Return the singleton instance.
TF_API void WriteDotFile(std::string const &file) const
uint64 value_type
Definition: GA_PrimCompat.h:29
Definition: token.h:70
TF_API void RegisterLibrary(TfToken const &name, TfToken const &moduleName, std::vector< TfToken > const &predecessors)
TfScriptModuleLoader This
TF_API void LoadModulesForLibrary(TfToken const &name)
GLuint const GLchar * name
Definition: glcorearb.h:786
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TF_API pxr_boost::python::dict GetModulesDict() const
TF_API void LoadModules()