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

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

6
#include <dune/amdis/common/Utility.hpp>
7
#include <dune/amdis/Output.hpp>
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:
    virtual ~CreatorInterface() {}

    /** \brief
     * Must be implemented by sub classes of CreatorInterface.
     * Creates a new instance of the sub class of BaseClass.
     */
34
    virtual shared_ptr<BaseClass> create() = 0;
35
36
37
38
39
40
41
42
43
44
45
46
  };

  /**
   * \ingroup Common
   *
   * \brief
   * Interface for creators with name.
   */
  template <class BaseClass>
  class CreatorInterfaceName : public CreatorInterface<BaseClass>
  {
  public:
47

48
    virtual shared_ptr<BaseClass> create() override
49
50
51
    {
      error_exit("Should not be called. Call create(string) instead!");
      return {};
52
    };
53

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


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

} // end namespace AMDiS