File ModuleManager.h

Copyright

This code is licensed under the 3-clause BSD license.

Copyright ETH Zurich, Laboratory for Physical Chemistry, Reiher Group.

See LICENSE.txt for details.

namespace Scine
namespace Core
class ModuleManager
#include <ModuleManager.h>

The manager for all dynamically loaded modules.

This class is a singleton, in order to ensure that there is only ever one instance of this class controlling all modules loaded.

Use this class by requesting a reference to the single existing instance:

const auto& manager = ModuleManager::getInstance()
if(manager.has<Calculator>("DFT")) {
  auto calculator = manager.get<Calculator>("DFT");
}

Public Functions

ModuleManager(ModuleManager const&)

Deleted copy constructor, as required by the singleton design pattern.

The constructor could also be private, however public deletion should generate more sensible error messages.

Parameters

void operator=(ModuleManager const&)

Deleted assignment operator, as required by the singleton design pattern.

The assignment operator could also be private, however public deletion should generate more sensible error messages.

Parameters

void load(const boost::filesystem::path &libraryPath)

Loads a library as a module.

Loads a shared library from a specified path and constructs the derived module class, adding it to the list of loaded modules.

Parameters
  • libraryPath: Either a full qualified path to the library, or merely the path to it and the name of the library without system-specific pre- and suffixes. E.g. “/usr/lib/libblas.so” or just “/usr/lib/blas”, both should work.

Exceptions
  • std::runtime_error: Under any of the following circumstances:

    • The file is not found

    • There is not enough memory

    • The library does not provide the moduleFactory alias entry point to construct a module base class pointer

void load(boost::dll::shared_library library)

Loads a module from a shared_library instance.

Parameters
  • library: A shared_library instance that contains the symbol alias “moduleFactory” that is a nullary function yielding a shared_ptr<Module>

std::vector<std::string> getLoadedModuleNames() const

Get a list of all loaded Modules.

Return

std::vector<std::string> Returns a vector of names corresponding to the Modules currently loaded.

std::vector<std::string> getLoadedInterfaces() const
template<typename Interface>
std::vector<std::string> getLoadedModels() const

Announces a list of all loaded models of a particular interface.

Return

A list of string-identifiers to the loaded models

Template Parameters
  • Interface: The interface class type (e.g. Calculator)

std::vector<std::string> getLoadedModels(const std::string &interface) const

Announces a list of all loaded models of a particular interface.

Return

A list of string-identifiers to the loaded models

Parameters
  • interface: The string identifier of a particular interface

template<typename Interface>
bool has(const std::string &model, const std::string &moduleName = "") const

Checks whether a particular model of an interface is available.

Return

Whether the model is available. If so, get() should be safe to call.

Template Parameters
  • Interface: The interface class type (e.g. Calculator).

Parameters
  • model: The string identifier of a particular model.

  • moduleName: The string identifier of a particular module. If given, queries for the exact module-model combination. Module names will be auto expanded in addition to being tested literally: ‘XYZ’ will also query for ‘XYZModule’.

bool has(const std::string &interface, const std::string &model, const std::string &moduleName = "") const

Checks whether a particular model of an interface is available.

Return

Whether the model is available. If so, get() should be safe to call.

Parameters
  • interface: The string identifier of an interface class type.

  • model: The string identifier of a particular model.

  • moduleName: The string identifier of a particular module. If given, queries for the exact module-model combination. Module names will be auto expanded in addition to being tested literally: ‘XYZ’ will also query for ‘XYZModule’.

template<typename Interface>
std::shared_ptr<Interface> get(const std::string &model, const std::string &moduleName = "") const

Creates a model of an interface and returns a base-class pointer.

Creates a derived class and yields a base-class pointer to it.

Return

A base-class pointer to the class derived from the specified interface.

Template Parameters
  • Interface: The interface class type (e.g. Calculator)

Parameters
  • model: The string identifier of a particular model (e.g. DefaultCalculator)

  • moduleName: The string identifier of a particular module. If given, queries for the exact module-model combination. Module names will be auto expanded in addition to being tested literally: ‘XYZ’ will also query for ‘XYZModule’.

Exceptions
  • std::runtime_error: 1) If no loaded module provides that particular interface model. 2) If the requested module does not exist. 3) If the requested module does not provide the interface model.

template<typename Interface, typename UnaryPredicate>
std::enable_if_t<!std::is_convertible<UnaryPredicate, std::string>::value, std::shared_ptr<Interface>> get(UnaryPredicate &&predicate, const std::string &moduleName = "") const

Find a model satisfying a predicate.

Return

A non-empty interface shared pointer

Template Parameters
  • Interface: The interface class type (e.g. Calculator)

  • UnaryPredicate: Unary predicate function or functor of signature (std::shared_ptr<Interface> -> bool)

Parameters
  • predicate: instance of UnaryPredicate

  • moduleName: String identifier of a module. If given, searches for models of Interface only in that module.

Exceptions
  • std::runtime_error: If no models of this interface are loaded or if no model matches the supplied predicate.

bool moduleLoaded(const std::string &moduleName) const

Checks whether a particular module is loaded.

Return

Whether the purported module is loaded

Parameters
  • moduleName: The self-reported name of the module

Public Static Functions

static ModuleManager &getInstance()

Static instance getter.

This static function assures the single instantiation of a ModuleManager for more information google the singleton design pattern.

Return

Returns a reference to the ModuleManager instance.

Private Functions

ModuleManager()
boost::any _get(const std::string &interface, const std::string &model, const std::string &moduleName = "") const

Fetches the any from the appropriate module.

Return

A boost::any-wrapped std::shared_ptr<Interface>

Parameters
  • interface: The interface identifier that the desired module matches

  • model: The model’s identifier

  • moduleName: The string identifier of a particular module. If given, queries for the exact module-model combination. Module names will be auto expanded in addition to being tested literally: ‘XYZ’ will also query for ‘XYZModule’.

Exceptions
  • std::runtime_error: 1) If no loaded module provides that particular interface model. 2) If the requested module does not exist. 3) If the requested module does not provide the interface model.

std::vector<boost::any> _getAll(const std::string &interface, const std::string &moduleName = "") const

Fetches all models of an interface, optionally just from a single module.

Return

a vector of boost::anys wrapping std::shared_ptr<Interface>. The vector may be empty, the shared_ptrs will not.

Parameters
  • interface: interface identifier that the module matches

  • moduleName: The string identifier of a particular model. If not empty, queries for models only in that particular module. Module names will also be expanded: ‘XYZ’ will also query for ‘XYZModule’

Private Static Attributes

std::vector<LibraryAndModules> _sources