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 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_TF_SCRIPT_MODULE_LOADER_H
25 #define PXR_BASE_TF_SCRIPT_MODULE_LOADER_H
26 
27 #include "pxr/pxr.h"
28 
29 #include "pxr/base/tf/api.h"
30 #include "pxr/base/tf/hash.h"
31 #include "pxr/base/tf/singleton.h"
32 #include "pxr/base/tf/token.h"
33 #include "pxr/base/tf/weakBase.h"
34 
35 // XXX: This include is a hack to avoid build errors due to
36 // incompatible macro definitions in pyport.h on macOS.
37 #include <locale>
38 #include <hboost/python/dict.hpp>
39 
40 #include <deque>
41 #include "pxr/base/tf/hashmap.h"
42 #include "pxr/base/tf/hashset.h"
43 #include <string>
44 #include <vector>
45 
47 
48 /// \class TfScriptModuleLoader
49 ///
50 /// Provides low-level facilities for shared libraries with script bindings to
51 /// register themselves with their dependences, and provides a mechanism
52 /// whereby those script modules will be loaded when necessary. Currently,
53 /// this is when one of our script modules is loaded, when TfPyInitialize is
54 /// called, and when Plug opens shared libraries.
55 ///
56 /// Generally, user code will not make use of this.
57 ///
59 
60  public:
61 
63 
64  /// Return the singleton instance.
65  TF_API static This &GetInstance() {
67  }
68 
69  /// Register a library named \a name and with script module \a moduleName
70  /// and libraries which must be loaded first \a predecessors. The script
71  /// module will be loaded when necessary. This should generally not be
72  /// called by user code.
73  TF_API
74  void RegisterLibrary(TfToken const &name, TfToken const &moduleName,
75  std::vector<TfToken> const &predecessors);
76 
77  /// Load all the script modules for any libraries registered using \a
78  /// RegisterLibrary if necessary. Loads the modules in dependency order as
79  /// defined in \a RegisterLibrary.
80  TF_API
81  void LoadModules();
82 
83  /// Load all the script modules for any libraries registered using \a
84  /// RegisterLibrary that depend on library \a name.
85  TF_API
86  void LoadModulesForLibrary(TfToken const &name);
87 
88  /// Return a list of all currently known modules in a valid dependency
89  /// order.
90  TF_API
91  std::vector<std::string> GetModuleNames() const;
92 
93  /// Return a python dict containing all currently known modules under
94  /// their canonical names.
95  TF_API
96  hboost::python::dict GetModulesDict() const;
97 
98  /// Write a graphviz dot-file for the dependency graph of all. currently
99  /// known libraries/modules to \a file.
100  TF_API
101  void WriteDotFile(std::string const &file) const;
102 
103  private:
104 
105  struct _LibInfo {
106  _LibInfo() {}
107  std::vector<TfToken> predecessors, successors;
108  };
109 
111  _TokenToInfoMap;
112 
114  _TokenToTokenMap;
115 
117  _TokenSet;
118 
120  virtual ~TfScriptModuleLoader();
121  friend class TfSingleton<This>;
122 
123  void _AddSuccessor(TfToken const &lib, TfToken const &successor);
124  void _LoadModulesFor(TfToken const &name);
125  void _LoadUpTo(TfToken const &name);
126  void _GetOrderedDependenciesRecursive(TfToken const &lib,
127  TfToken::HashSet *seenLibs,
128  std::vector<TfToken> *result) const;
129  void _GetOrderedDependencies(std::vector<TfToken> const &input,
130  std::vector<TfToken> *result) const;
131  void _TopologicalSort(std::vector<TfToken> *result) const;
132 
133  bool _HasTransitiveSuccessor(TfToken const &predecessor,
134  TfToken const &successor) const;
135 
136  _TokenToInfoMap _libInfo;
137  _TokenToTokenMap _libsToModules;
138  _TokenSet _loadedSet;
139 
140  // This is only used to handle reentrant loading requests.
141  std::deque<TfToken> _remainingLoadWork;
142 };
143 
145 
147 
148 #endif // PXR_BASE_TF_SCRIPT_MODULE_LOADER_H
static T & GetInstance()
Definition: singleton.h:137
#define TF_API
Definition: api.h:40
TF_API hboost::python::dict GetModulesDict() const
TF_API_TEMPLATE_CLASS(TfSingleton< TfScriptModuleLoader >)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
static TF_API This & GetInstance()
Return the singleton instance.
**But if you need a result
Definition: thread.h:613
TF_API void WriteDotFile(std::string const &file) const
TF_API std::vector< std::string > GetModuleNames() const
Definition: token.h:87
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:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TF_API void LoadModules()