Loading...
Searching...
No Matches
scriptModuleLoader.h
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"
15#include "pxr/base/tf/token.h"
17
18// XXX: This include is a hack to avoid build errors due to
19// incompatible macro definitions in pyport.h on macOS.
20#include <locale>
21#include <boost/python/dict.hpp>
22
23#include <deque>
24#include "pxr/base/tf/hashmap.h"
25#include "pxr/base/tf/hashset.h"
26#include <string>
27#include <vector>
28
29PXR_NAMESPACE_OPEN_SCOPE
30
42
43 public:
44
46
48 TF_API static This &GetInstance() {
50 }
51
56 TF_API
57 void RegisterLibrary(TfToken const &name, TfToken const &moduleName,
58 std::vector<TfToken> const &predecessors);
59
63 TF_API
65
68 TF_API
69 void LoadModulesForLibrary(TfToken const &name);
70
73 TF_API
74 std::vector<std::string> GetModuleNames() const;
75
78 TF_API
79 boost::python::dict GetModulesDict() const;
80
83 TF_API
84 void WriteDotFile(std::string const &file) const;
85
86 private:
87
88 struct _LibInfo {
89 _LibInfo() {}
90 std::vector<TfToken> predecessors, successors;
91 };
92
93 typedef TfHashMap<TfToken, _LibInfo, TfToken::HashFunctor>
94 _TokenToInfoMap;
95
96 typedef TfHashMap<TfToken, TfToken, TfToken::HashFunctor>
97 _TokenToTokenMap;
98
99 typedef TfHashSet<TfToken, TfToken::HashFunctor>
100 _TokenSet;
101
103 virtual ~TfScriptModuleLoader();
104 friend class TfSingleton<This>;
105
106 void _AddSuccessor(TfToken const &lib, TfToken const &successor);
107 void _LoadModulesFor(TfToken const &name);
108 void _LoadUpTo(TfToken const &name);
109 void _GetOrderedDependenciesRecursive(TfToken const &lib,
110 TfToken::HashSet *seenLibs,
111 std::vector<TfToken> *result) const;
112 void _GetOrderedDependencies(std::vector<TfToken> const &input,
113 std::vector<TfToken> *result) const;
114 void _TopologicalSort(std::vector<TfToken> *result) const;
115
116 bool _HasTransitiveSuccessor(TfToken const &predecessor,
117 TfToken const &successor) const;
118
119 _TokenToInfoMap _libInfo;
120 _TokenToTokenMap _libsToModules;
121 _TokenSet _loadedSet;
122
123 // This is only used to handle reentrant loading requests.
124 std::deque<TfToken> _remainingLoadWork;
125};
126
127TF_API_TEMPLATE_CLASS(TfSingleton<TfScriptModuleLoader>);
128
129PXR_NAMESPACE_CLOSE_SCOPE
130
131#endif // PXR_BASE_TF_SCRIPT_MODULE_LOADER_H
Provides low-level facilities for shared libraries with script bindings to register themselves with t...
TF_API boost::python::dict GetModulesDict() const
Return a python dict containing all currently known modules under their canonical names.
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...
static TF_API This & GetInstance()
Return the singleton instance.
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...
TF_API void WriteDotFile(std::string const &file) const
Write a graphviz dot-file for the dependency graph of all.
TF_API std::vector< std::string > GetModuleNames() const
Return a list of all currently known modules in a valid dependency order.
Manage a single instance of an object (see.
Definition: singleton.h:105
static T & GetInstance()
Return a reference to an object of type T, creating it if necessary.
Definition: singleton.h:120
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
TfHashSet< TfToken, TfToken::HashFunctor > HashSet
Predefined type for TfHashSet of tokens, since it's so awkward to manually specify.
Definition: token.h:158
Enable a concrete base class for use with TfWeakPtr.
Definition: weakBase.h:124
Manage a single instance of an object.
TfToken class for efficient string referencing and hashing, plus conversions to and from stl string c...