CreatorInterface.hpp 2.1 KB
Newer Older
1
2
#pragma once

3
#include <memory>
4
5
#include <string>

6
#include <amdis/Output.hpp>
7

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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<BaseClass>. 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<LinearSolverInterface<T> >, 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 BaseClass>
  class CreatorInterface
  {
  public:
26
    virtual ~CreatorInterface() = default;
27
28
29
30
31

    /** \brief
     * Must be implemented by sub classes of CreatorInterface.
     * Creates a new instance of the sub class of BaseClass.
     */
32
    virtual std::unique_ptr<BaseClass> create() = 0;
33
34
35
36
37
38
39
40
41
  };

  /**
   * \ingroup Common
   *
   * \brief
   * Interface for creators with name.
   */
  template <class BaseClass>
42
43
  class CreatorInterfaceName
      : public CreatorInterface<BaseClass>
44
45
  {
  public:
46

47
    virtual std::unique_ptr<BaseClass> create() final
48
49
50
    {
      error_exit("Should not be called. Call create(string) instead!");
      return {};
51
    };
52

53
54
    /** \brief
     * Must be implemented by sub classes of CreatorInterfaceName.
55
     * Creates a new instance of the sub class of BaseClass by passing a
56
57
     * string to the constructor.
     */
58
    virtual std::unique_ptr<BaseClass> createWithString(std::string) = 0;
59
  };
60
61


62
63
64
65
66
  /// cast a ptr of CreatorInterface to CreatorInterfaceName
  template <class BaseClass>
  inline CreatorInterfaceName<BaseClass>* named(CreatorInterface<BaseClass>* ptr)
  {
    auto result = dynamic_cast<CreatorInterfaceName<BaseClass>*>(ptr);
67
    test_exit_dbg(result, "Can not cast CreatorInterface to CreatorInterfaceName!");
68
69
    return result;
  }
70
71

} // end namespace AMDiS