CreatorInterface.hpp 2 KB
Newer Older
1
2
3
4
5
6
7
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once

#include <string>

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.
     */
    virtual BaseClass* create() = 0;

    /// Can be implemented by sub classes.
    virtual void free(BaseClass*) {}

    ///
    virtual bool isNullCreator()
    {
      return false;
    }
  };

  /** \brief
   * A Creator which creates no object and returns NULL instead.
   * Used together with the key word 'no' in CreatorMap.
   */
  template <class BaseClass>
  class NullCreator : public CreatorInterface<BaseClass>
  {
    /// Creates no object.
    virtual BaseClass* create() override
    {
      return NULL;
    }

    /// Implementation of \ref CreatorInterface::isNullCreator()
    virtual bool isNullCreator() override
    {
      return true;
    }
  };


  /**
   * \ingroup Common
   *
   * \brief
   * Interface for creators with name.
   */
  template <class BaseClass>
  class CreatorInterfaceName : public CreatorInterface<BaseClass>
  {
  public:
    /// Sets \ref name
    void setName(std::string str)
    {
      name = str;
    }

  protected:
    std::string name;
  };

} // end namespace AMDiS