File DerivedModule.h

Provides helpers for the general implementation of classes deriving from Module.

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
namespace DerivedModule

Functions

template<typename MPLMap>
boost::any resolve(const std::string &interface, const std::string &model)

Creates an InterfaceType shared_ptr of a matching model to an interface.

Helper function to implement a derived module’s get function:

// Transforms this:
boost::any Derived::get(const std::string& interface, const std::string& model) const {
  if(interface == Calculator::interface) {
    if(model == PlusCalculator::model) {
      return static_cast<Calculator>(std::make_shared<PlusCalculator>());
    }

    if(model == MinusCalculator::model) {
      return static_cast<Calculator>(std::make_shared<MinusCalculator>());
    }

    ...
  }

  if(interface == Printer::interface) {
    if(model == SlowPrinter::model) {
      return static_cast<Printer>(std::make_shared<SlowPrinter>());
    }
    if(model == EvenSlowerPrinter::model) {
      return static_cast<Printer>(std::make_shared<EvenSlowerPrinter>());
    }

    ...
  }

  ...

  throw Core::ClassNotImplementedError();
}

// Into this:
boost::any Derived::get(const std::string& interface, const std::string& model) const {
  // NOTE: Same map for has, announceInterfaces, anounceModels
  using Map = boost::mpl::map<
    boost::mpl::pair<Calculator, boost::mpl::vector<PlusCalculator, MinusCalculator>>,
    boost::mpl::pair<Printer, boost::mpl::vector<SlowPrinter, EvenSlowerPrinter>>
  >;

  boost::any resolved = DerivedModule::resolve<Map>(interface, model);
  if(resolved.empty()) {
    throw Core::ClassNotImplementedError {};
  }
  return resolved;
}

Return

a boost any containing a shared_ptr to the InterfaceType of the matched model, an empty boost::any otherwise.

Template Parameters
  • MPLMap: An mpl::map of InterfaceType -> mpl::vector<ModelType> InterfaceType::interface and ModelType::Model must be valid expressions

Parameters
  • interface: The interface to model

  • model: The model of interface to create

template<typename MPLMap>
bool has(const std::string &interface, const std::string &model)

Checks whether a module has a particular model for a particular interface.

Return

whether the module has a model for the interface

Template Parameters
  • MPLMap: An mpl::map of InterfaceType -> mpl::vector<ModelType> InterfaceType::interface and ModelType::Model must be valid expressions

Parameters
  • interface: The interface to check for

  • model: The model to check for

template<typename MPLMap>
std::vector<std::string> announceInterfaces()

Announces all interface names.

Return

A list of all interface names

Template Parameters
  • MPLMap: An mpl::map of InterfaceType -> mpl::vector<ModelType> InterfaceType::interface and ModelType::Model must be valid expressions

template<typename MPLMap>
std::vector<std::string> announceModels(const std::string &interface)

Announces all model names for a particular interface.

Return

The list of model names for a particular interface, or an empty list if no models exist for that interface.

Template Parameters
  • MPLMap: An mpl::map of InterfaceType -> mpl::vector<ModelType> InterfaceType::interface and ModelType::Model must be valid expressions

Parameters
  • interface: The interface for which to list model identifiers

namespace detail

Functions

bool caseInsensitiveEqual(const std::string &a, const std::string &b)
template<typename Sequence, typename Predicate, typename Executable>
auto exec_if(const Predicate &p, const Executable &e, Sequence * = 0)

Run-time “iteration” through type list, if predicate matches, call another function, otherwise return its none.

Return

Executable::none() if Predicate never matches, otherwise Executable(T) for the first T in Sequence that matches Predicate.

Template Parameters
  • Sequence: a boost::mpl compatible sequence type

  • Predicate: A type with a template<T> bool operator() (T* = 0) const member specifying whether to execute Executable for the type T in Sequence

  • Executable: A type fulfilling the following requirements:

    • Has a ResultType typedef

    • Has a template<T> ResultType operator() (T* = 0) const member which is executed if returns true for a type in Sequence

    • Has a static ResultType none() const member returning the base case

constexpr bool strEqual(const char *a, const char *b)
template<typename ...ModelTypes>
constexpr bool identifiersOverlap()
template<typename MPLVector, std::size_t... Inds>
constexpr bool identifierOverlapForwarder(std::index_sequence<Inds...>)
template<bool done = true>
struct exec_if_impl

Public Static Functions

template<typename Iter, typename LastIter, typename Pred, typename Exec>
static auto execute(Iter *, LastIter *, const Pred&, const Exec&)
template<>
struct exec_if_impl<false>

Public Static Functions

template<typename Iter, typename LastIter, typename Pred, typename Exec>
static auto execute(Iter *, LastIter *, const Pred &p, const Exec &e)
struct MapPairInterfaceIdentifierMatches

Public Functions

MapPairInterfaceIdentifierMatches(std::string id)
template<typename PairType>
bool operator()(PairType * = 0) const

Public Members

std::string identifier
struct ModelTypeIdentifierMatches

Public Functions

ModelTypeIdentifierMatches(std::string id)
template<typename ModelType>
bool operator()(ModelType * = 0) const

Public Members

std::string identifier
template<typename InterfaceType>
struct CreateInterfacePointer

Public Types

template<>
using InterfaceTypePtr = std::shared_ptr<InterfaceType>
template<>
using ResultType = boost::any

Public Functions

template<typename ModelType>
ResultType operator()(ModelType * = 0) const

Public Static Functions

static ResultType none()
struct ResolveModel

Public Types

using ResultType = boost::any

Public Functions

ResolveModel(std::string id)
template<typename PairType>
boost::any operator()(PairType * = 0) const

Public Members

std::string identifier

Public Static Functions

static ResultType none()
struct MatchFoundExecutor

Public Types

using ResultType = bool

Public Functions

template<typename T>
ResultType operator()(T * = 0) const

Public Static Functions

static ResultType none()
struct ModelExists

Public Types

using ResultType = bool

Public Functions

ModelExists(std::string id)
template<typename PairType>
ResultType operator()(PairType * = 0) const

Public Members

std::string identifier

Public Static Functions

static ResultType none()
struct ListModels

Public Types

using ResultType = std::vector<std::string>

Public Functions

template<typename PairType>
ResultType operator()(PairType * = 0) const

Public Static Functions

static ResultType none()