#pragma once #include #include #include namespace AMDiS { /** \ingroup Common * \brief * Interface for the implementation of the factory method pattern. * The creation of an object of a sub class of BaseClass is deligated * to a corresponding sub class of Creator. So it is possible to * manage a CreatorMap, which can be extended at run-time. An example is * the LinearSolverInterfaceMap: If you write your own LinearSolverInterface sub class and a * corresponding Creator >, you can add the creator together * with a key string to the LinearSolverInterfaceMap. Then you can create an LinearSolverInterface * depending of a key string read from the init file, which can also be * your own new solver. */ template class CreatorInterface { public: virtual ~CreatorInterface() = default; /** \brief * Must be implemented by sub classes of CreatorInterface. * Creates a new instance of the sub class of BaseClass. */ virtual std::unique_ptr create() = 0; }; /** * \ingroup Common * * \brief * Interface for creators with name. */ template class CreatorInterfaceName : public CreatorInterface { public: virtual std::unique_ptr create() final { error_exit("Should not be called. Call create(string) instead!"); return {}; }; /** \brief * Must be implemented by sub classes of CreatorInterfaceName. * Creates a new instance of the sub class of BaseClass by passing a * string to the constructor. */ virtual std::unique_ptr createWithString(std::string) = 0; }; /// cast a ptr of CreatorInterface to CreatorInterfaceName template inline CreatorInterfaceName* named(CreatorInterface* ptr) { auto result = dynamic_cast*>(ptr); test_exit_dbg(result, "Can not cast CreatorInterface to CreatorInterfaceName!"); return result; } } // end namespace AMDiS