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 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 <boost/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 
46 PXR_NAMESPACE_OPEN_SCOPE
47 
59 
60  public:
61 
62  typedef TfScriptModuleLoader This;
63 
65  TF_API static This &GetInstance() {
67  }
68 
73  TF_API
74  void RegisterLibrary(TfToken const &name, TfToken const &moduleName,
75  std::vector<TfToken> const &predecessors);
76 
80  TF_API
81  void LoadModules();
82 
85  TF_API
86  void LoadModulesForLibrary(TfToken const &name);
87 
90  TF_API
91  std::vector<std::string> GetModuleNames() const;
92 
95  TF_API
96  boost::python::dict GetModulesDict() const;
97 
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 
110  typedef TfHashMap<TfToken, _LibInfo, TfToken::HashFunctor>
111  _TokenToInfoMap;
112 
113  typedef TfHashMap<TfToken, TfToken, TfToken::HashFunctor>
114  _TokenToTokenMap;
115 
116  typedef TfHashSet<TfToken, TfToken::HashFunctor>
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 
144 TF_API_TEMPLATE_CLASS(TfSingleton<TfScriptModuleLoader>);
145 
146 PXR_NAMESPACE_CLOSE_SCOPE
147 
148 #endif // PXR_BASE_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:87
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:175
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:141