All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
scriptModuleLoader.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 TF_SCRIPT_MODULE_LOADER_H
25 #define 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 #include <boost/python/dict.hpp>
36 
37 #include <deque>
38 #include "pxr/base/tf/hashmap.h"
39 #include "pxr/base/tf/hashset.h"
40 #include <string>
41 #include <vector>
42 
43 PXR_NAMESPACE_OPEN_SCOPE
44 
56 
57  public:
58 
59  typedef TfScriptModuleLoader This;
60 
62  TF_API static This &GetInstance() {
64  }
65 
70  TF_API
71  void RegisterLibrary(TfToken const &name, TfToken const &moduleName,
72  std::vector<TfToken> const &predecessors);
73 
77  TF_API
78  void LoadModules();
79 
82  TF_API
83  void LoadModulesForLibrary(TfToken const &name);
84 
87  TF_API
88  std::vector<std::string> GetModuleNames() const;
89 
92  TF_API
93  boost::python::dict GetModulesDict() const;
94 
97  TF_API
98  void WriteDotFile(std::string const &file) const;
99 
100  private:
101 
102  struct _LibInfo {
103  _LibInfo() {}
104  std::vector<TfToken> predecessors, successors;
105  };
106 
107  typedef TfHashMap<TfToken, _LibInfo, TfToken::HashFunctor>
108  _TokenToInfoMap;
109 
110  typedef TfHashMap<TfToken, TfToken, TfToken::HashFunctor>
111  _TokenToTokenMap;
112 
113  typedef TfHashSet<TfToken, TfToken::HashFunctor>
114  _TokenSet;
115 
117  virtual ~TfScriptModuleLoader();
118  friend class TfSingleton<This>;
119 
120  void _AddSuccessor(TfToken const &lib, TfToken const &successor);
121  void _LoadModulesFor(TfToken const &name);
122  void _LoadUpTo(TfToken const &name);
123  void _GetOrderedDependenciesRecursive(TfToken const &lib,
124  TfToken::HashSet *seenLibs,
125  std::vector<TfToken> *result) const;
126  void _GetOrderedDependencies(std::vector<TfToken> const &input,
127  std::vector<TfToken> *result) const;
128  void _TopologicalSort(std::vector<TfToken> *result) const;
129 
130  bool _HasTransitiveSuccessor(TfToken const &predecessor,
131  TfToken const &successor) const;
132 
133  _TokenToInfoMap _libInfo;
134  _TokenToTokenMap _libsToModules;
135  _TokenSet _loadedSet;
136 
137  // This is only used to handle reentrant loading requests.
138  std::deque<TfToken> _remainingLoadWork;
139 };
140 
141 TF_API_TEMPLATE_CLASS(TfSingleton<TfScriptModuleLoader>);
142 
143 PXR_NAMESPACE_CLOSE_SCOPE
144 
145 #endif // TF_SCRIPT_MODULE_LOADER_H
Manage a single instance of an object (see.
Definition: singleton.h:122
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:89
TF_API void WriteDotFile(std::string const &file) const
Write a graphviz dot-file for the dependency graph of all.
TF_API boost::python::dict GetModulesDict() const
Return a python dict containing all currently known modules under their canonical names...
static TF_API This & GetInstance()
Return the singleton instance.
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:137
TF_API std::vector< std::string > GetModuleNames() const
Return a list of all currently known modules in a valid dependency order.
Provides low-level facilities for shared libraries with script bindings to register themselves with t...
TF_API void RegisterLibrary(TfToken const &name, TfToken const &moduleName, std::vector< TfToken > const &predecessors)
Register a library named name and with script module moduleName and libraries which must be loaded fi...
TfHashSet< TfToken, TfToken::HashFunctor > HashSet
Predefined type for TfHashSet of tokens, since it&#39;s so awkward to manually specify.
Definition: token.h:177
TF_API void LoadModules()
Load all the script modules for any libraries registered using RegisterLibrary if necessary...
TF_API void LoadModulesForLibrary(TfToken const &name)
Load all the script modules for any libraries registered using RegisterLibrary that depend on library...
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:142