Belle II Software  release-08-01-10
ModuleManager Class Reference

The ModuleManager Class. More...

#include <ModuleManager.h>

Collaboration diagram for ModuleManager:

Public Member Functions

void registerModuleProxy (ModuleProxyBase *moduleProxy)
 Registers a module proxy. More...
 
void addModuleSearchPath (const std::string &path)
 Adds a new filepath to the list of filepaths which are searched for a requested module. More...
 
const std::list< std::string > & getModuleSearchPaths () const
 Returns a reference to the list of the modules search filepaths. More...
 
const std::map< std::string, std::string > & getAvailableModules () const
 Returns a map of all modules that were found in the module search paths. More...
 
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. More...
 
const std::list< std::shared_ptr< Module > > & getCreatedModules () const
 Returns a reference to the list of created modules. More...
 
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. More...
 
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. More...
 
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. More...
 

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. More...
 

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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ 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.

◆ registerModule()

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

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.

◆ 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.


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