Belle II Software development
ModuleManager Class Reference

The ModuleManager Class. More...

#include <ModuleManager.h>

Public Member Functions

void registerModuleProxy (ModuleProxyBase *moduleProxy)
 Registers a module proxy.
 
void addModuleSearchPath (const std::string &path)
 Adds a new filepath to the list of filepaths which are searched for a requested module.
 
const std::list< std::string > & getModuleSearchPaths () const
 Returns a reference to the list of the modules search filepaths.
 
const std::map< std::string, std::string > & getAvailableModules () const
 Returns a map of all modules that were found in the module search paths.
 
std::shared_ptr< ModuleregisterModule (const std::string &moduleName, std::string sharedLibPath="") noexcept(false)
 Creates an instance of a module and registers it to the ModuleManager.
 
const std::list< std::shared_ptr< Module > > & getCreatedModules () const
 Returns a reference to the list of created modules.
 
void reset ()
 Delete all created modules.
 

Static Public Member Functions

static ModuleManagerInstance ()
 Exception is thrown if the requested module could not be created by the ModuleManager.
 
static std::list< std::shared_ptr< Module > > getModulesByProperties (const std::list< std::shared_ptr< Module > > &modulePathList, unsigned int propertyFlags)
 Returns a list of those modules which carry property flags matching the specified ones.
 
static bool allModulesHaveFlag (const std::list< std::shared_ptr< Module > > &list, unsigned int flag)
 Returns true if and only if all modules in list have the given flag (or list is empty).
 

Private Member Functions

 ModuleManager ()
 The constructor is hidden to avoid that someone creates an instance of this class.
 
 ModuleManager (const ModuleManager &)
 Disable/Hide the copy constructor.
 
ModuleManageroperator= (const ModuleManager &)
 Disable/Hide the copy assignment operator.
 
 ~ModuleManager ()
 The ModuleManager destructor.
 

Static Private Member Functions

static void fillModuleNameLibMap (std::map< std::string, std::string > &moduleNameLibMap, const std::filesystem::directory_entry &mapPath)
 Adds the module names defined in the map file to the list of known module names.
 

Private Attributes

std::list< std::string > m_moduleSearchPathList
 List of all checked and validated filepaths that are searched for map files.
 
std::map< std::string, std::string > m_moduleNameLibMap
 Maps the module name to the filename of the shared library which containes the module.
 
std::map< std::string, ModuleProxyBase * > m_registeredProxyMap
 Maps the module name to a pointer of its proxy.
 
std::list< std::shared_ptr< Module > > m_createdModulesList
 List of all created modules.
 

Detailed Description

The ModuleManager Class.

This class is responsible for creating and managing new module instances. The module manager can only find those modules, whose shared library files are located in one of the directories known to the module manager. Directories can be added to the directory list with the method addModuleSearchPath(). By adding a new directory, the directory is searched for *.map files which specify the kind of modules that can be found in the shared library files. The map files are parsed and the found modules are added to an internal list.

If a shared library is loaded by the module manager, all modules inside the library automatically register themselves to the module manager. This is done by a so-called proxy class. Each module has a proxy class attached to it, which does the registration with the module manager and is later used to create the actual instance of the module. By using a proxy class, the module itself is only instantiated if the user requests it.

The creation of a new module instance works as following: 1) The user requests a new module by calling the method registerModule() with the module name given as a parameter.
2) The ModuleManager checks if a proxy class was already registered for the given module name. If yes, the proxy creates a new module instance. Otherwise the modules known to the manager are searched for the given module name. If the module is known to the module manager the associated shared library is dynamically loaded.

This class is designed as a singleton.

Definition at line 53 of file ModuleManager.h.

Constructor & Destructor Documentation

◆ ~ModuleManager()

~ModuleManager ( )
privatedefault

The ModuleManager destructor.

Deletes the ModuleManager.

Member Function Documentation

◆ addModuleSearchPath()

void addModuleSearchPath ( const std::string &  path)

Adds a new filepath to the list of filepaths which are searched for a requested module.

If a module is requested by the method registerModule(), all module search paths are scanned for the first occurrence of a shared library carrying the module name in its filename. E.g modules in the path added with the first call to addModuleSearchPath() will take precedence over those added later on.

Parameters
pathThe module search path which should be added to the list of paths.

Definition at line 43 of file ModuleManager.cc.

44{
45 if (FileSystem::isDir(path)) {
46 m_moduleSearchPathList.push_back(path);
47
48 //Search the path for map files and add the contained module names to the known module names
49 auto fullPath = std::filesystem::absolute(std::filesystem::path(path));
50 std::filesystem::directory_iterator endIter;
51
52
53 map<string, string> moduleNameLibMap;
54 for (std::filesystem::directory_iterator dirItr(fullPath); dirItr != endIter; ++dirItr) {
55 //Only files in the given folder are taken, subfolders are not used.
56 if (std::filesystem::is_regular_file(dirItr->status())) {
57 if (std::filesystem::path(dirItr->path()).extension() == MAP_FILE_EXTENSION) {
58 fillModuleNameLibMap(moduleNameLibMap, *dirItr);
59 }
60 }
61 }
62 //put modules into central map, if they haven't been added yet
63 m_moduleNameLibMap.insert(moduleNameLibMap.begin(), moduleNameLibMap.end());
64 }
65}
static bool isDir(const std::string &filename)
Check if filename points to an existing directory.
Definition: FileSystem.cc:51
std::list< std::string > m_moduleSearchPathList
List of all checked and validated filepaths that are searched for map files.
std::map< std::string, std::string > m_moduleNameLibMap
Maps the module name to the filename of the shared library which containes the module.
static void fillModuleNameLibMap(std::map< std::string, std::string > &moduleNameLibMap, const std::filesystem::directory_entry &mapPath)
Adds the module names defined in the map file to the list of known module names.

◆ allModulesHaveFlag()

bool allModulesHaveFlag ( const std::list< std::shared_ptr< Module > > &  list,
unsigned int  flag 
)
static

Returns true if and only if all modules in list have the given flag (or list is empty).

Definition at line 140 of file ModuleManager.cc.

141{
142 for (const auto& m : list) {
143 if (!m->hasProperties(flag))
144 return false;
145 }
146 return true;
147}

◆ fillModuleNameLibMap()

void fillModuleNameLibMap ( std::map< std::string, std::string > &  moduleNameLibMap,
const std::filesystem::directory_entry &  mapPath 
)
staticprivate

Adds the module names defined in the map file to the list of known module names.

If the given map already contains this module, an error is raised

Parameters
moduleNameLibMapmap to be filled.
mapPathThe filename (path+name) of the map file which should be parsed for module names.

Definition at line 154 of file ModuleManager.cc.

156{
157 //Check if the associated shared library file exists
158 string sharedLibPath = std::filesystem::path(mapPath).replace_extension(LIB_FILE_EXTENSION).string();
159 if (!FileSystem::fileExists(sharedLibPath)) {
160 B2WARNING("The shared library file: " << sharedLibPath << " doesn't exist, but is required by " << mapPath.path().string());
161 return;
162 }
163
164 //Open the map file and parse the content line by line
165 ifstream mapFile(mapPath.path().string().c_str());
166 string currentLine;
167
168 //Read each line of the map file and use boost regular expression to find the module name string in brackets.
169 std::regex expression("^REG_MODULE\\((.+)\\)$");
170 std::match_results<std::string::const_iterator> matchResult;
171
172 int lineNr{0};
173 while (getline(mapFile, currentLine)) {
174 ++lineNr;
175
176 if (!std::regex_match(currentLine, matchResult, expression)) {
177 B2ERROR("Problem parsing map file " << mapPath << ": Invalid entry in line " << lineNr << ", skipping remaining file");
178 return;
179 }
180
181 string moduleName(matchResult[1].first, matchResult[1].second);
182 //Add result to map
183 if (moduleNameLibMap.count(moduleName) == 0) {
184 moduleNameLibMap.insert(make_pair(moduleName, sharedLibPath));
185 } else {
186 B2ERROR("There seems to be more than one module called '" << moduleName <<
187 "'. Since module names are unique, you must rename one of them!");
188 }
189 }
190
191 //Close the map file
192 mapFile.close();
193}
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:32

◆ getAvailableModules()

const map< string, string > & getAvailableModules ( ) const

Returns a map of all modules that were found in the module search paths.

The key of the map represents the module name and the value the shared library file in which the module is defined.

Returns
A map of all modules that were found in the module search paths.

Definition at line 74 of file ModuleManager.cc.

75{
76 return m_moduleNameLibMap;
77}

◆ getCreatedModules()

const ModulePtrList & getCreatedModules ( ) const

Returns a reference to the list of created modules.

Returns
A reference to the list of created modules.

Definition at line 124 of file ModuleManager.cc.

125{
127}
std::list< std::shared_ptr< Module > > m_createdModulesList
List of all created modules.

◆ getModulesByProperties()

ModulePtrList getModulesByProperties ( const std::list< std::shared_ptr< Module > > &  modulePathList,
unsigned int  propertyFlags 
)
static

Returns a list of those modules which carry property flags matching the specified ones.

Loops over all module instances specified in a list and adds those to the returned list whose property flags match the given property flags.

Parameters
modulePathListThe list containing all module instances added to a path.
propertyFlagsThe flags for the module properties.
Returns
A list containing those modules which carry property flags matching the specified ones.

Definition at line 130 of file ModuleManager.cc.

131{
132 ModulePtrList tmpModuleList;
133
134 for (const ModulePtr& module : modulePathList)
135 if (module->hasProperties(propertyFlags)) tmpModuleList.push_back(module);
136
137 return tmpModuleList;
138}
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition: Module.h:43
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
Definition: Module.h:584

◆ getModuleSearchPaths()

const list< string > & getModuleSearchPaths ( ) const

Returns a reference to the list of the modules search filepaths.

Returns
A reference to the list of the modules search filepaths.

Definition at line 68 of file ModuleManager.cc.

69{
71}

◆ Instance()

ModuleManager & Instance ( )
static

Exception is thrown if the requested module could not be created by the ModuleManager.

Static method to get a reference to the ModuleManager instance.

Returns
A reference to an instance of this class.

Definition at line 24 of file ModuleManager.cc.

25{
26 static ModuleManager instance;
27 return instance;
28}
The ModuleManager Class.
Definition: ModuleManager.h:53

◆ registerModule()

ModulePtr registerModule ( const std::string &  moduleName,
std::string  sharedLibPath = "" 
)

Creates an instance of a module and registers it to the ModuleManager.

Each module carries an unique name, which is used to find the corresponding shared library file. The found shared library is dynamically loaded and the module proxy registered. Using the proxy an instance of the module is created and returned.

The search order is as following:
1) First the method checks if a proxy associated with the module name was already registered
2) If not, the method checks if a shared library path was given and tries to load the module from that shared library
3) If not, the method searches for the module name in the map of known modules due to the given search paths

Parameters
moduleNameThe unique name of the Module which should be created.
sharedLibPathOptional: The shared library from which the module should be registered (not a map file!).
Returns
A shared pointer to the created module instance.

Definition at line 80 of file ModuleManager.cc.

81{
82 auto moduleIter = m_registeredProxyMap.find(moduleName);
83
84 // Print an error message and then raise the exception ...
85 auto error = [&moduleName](const std::string & text) -> void {
86 auto exception = ModuleNotCreatedError() << moduleName << text;
87 B2ERROR(exception.what());
88 throw exception;
89 };
90
91 //If the proxy of the module was not already registered, load the corresponding shared library first
92 if (moduleIter == m_registeredProxyMap.end()) {
93 // no library specified, try to find it from map of known modules
94 if (sharedLibPath.empty()) {
95 auto libIter = m_moduleNameLibMap.find(moduleName);
96 if (libIter != m_moduleNameLibMap.end()) {
97 sharedLibPath = libIter->second;
98 } else {
99 error("The module is not known to the framework!");
100 }
101 }
102 // Now we have a library name (provided or determined, try to load)
103 if (!FileSystem::isFile(sharedLibPath)) {
104 error("Could not load shared library " + sharedLibPath + ", file does not exist!");
105 }
106 if (!FileSystem::loadLibrary(sharedLibPath)) {
107 error("Could not load shared library " + sharedLibPath + ".");
108 }
109 //Check if the loaded shared library file contained the module
110 moduleIter = m_registeredProxyMap.find(moduleName);
111 if (moduleIter == m_registeredProxyMap.end()) {
112 error("The shared library " + sharedLibPath + " does not contain the module!");
113 }
114 }
115
116 //Create an instance of the module found or loaded in the previous steps and return it.
117 //The iterator cannot point to the end of the map, we checked in all branches.
118 ModulePtr currModulePtr = moduleIter->second->createModule();
119 m_createdModulesList.push_back(currModulePtr);
120 return currModulePtr;
121}
static bool loadLibrary(std::string library, bool fullname=true)
Load a shared library.
Definition: FileSystem.cc:63
static bool isFile(const std::string &filename)
Check if filename points to an existing file.
Definition: FileSystem.cc:45
std::map< std::string, ModuleProxyBase * > m_registeredProxyMap
Maps the module name to a pointer of its proxy.

◆ registerModuleProxy()

void registerModuleProxy ( ModuleProxyBase moduleProxy)

Registers a module proxy.

Each module has a proxy assigned to it, which registers itself to the ModuleManager. The proxy is then responsible to create an instance of a module.

Parameters
moduleProxyPointer to the module proxy which should be registered.

Definition at line 31 of file ModuleManager.cc.

32{
33 //Only register a module proxy if it was not yet registered.
34 if (m_registeredProxyMap.count(moduleProxy->getModuleName()) == 0) {
35 m_registeredProxyMap.insert(make_pair(moduleProxy->getModuleName(), moduleProxy));
36 } else {
37 B2ERROR("There seems to be more than one module called '" << moduleProxy->getModuleName() <<
38 "'. Since module names are unique, you must rename one of them!");
39 }
40}
const std::string & getModuleName() const
Returns the module name of the module associated to this proxy.
Definition: Module.h:630

◆ reset()

void reset ( )

Delete all created modules.

Definition at line 199 of file ModuleManager.cc.

200{
201 m_createdModulesList.clear();
202}

Member Data Documentation

◆ m_createdModulesList

std::list< std::shared_ptr<Module> > m_createdModulesList
private

List of all created modules.

Definition at line 159 of file ModuleManager.h.

◆ m_moduleNameLibMap

std::map<std::string, std::string> m_moduleNameLibMap
private

Maps the module name to the filename of the shared library which containes the module.

Definition at line 156 of file ModuleManager.h.

◆ m_moduleSearchPathList

std::list<std::string> m_moduleSearchPathList
private

List of all checked and validated filepaths that are searched for map files.

Definition at line 153 of file ModuleManager.h.

◆ m_registeredProxyMap

std::map<std::string, ModuleProxyBase*> m_registeredProxyMap
private

Maps the module name to a pointer of its proxy.

Definition at line 158 of file ModuleManager.h.


The documentation for this class was generated from the following files: