CreatorMap.h 3.55 KB
Newer Older
1
2
3
4
5
6
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// ==                                                                        ==
// ============================================================================

/** \file CreatorMap.h */

#ifndef AMDIS_CREATORMAP_H
#define AMDIS_CREATORMAP_H

#include <map>
#include "Global.h"
#include "CreatorInterface.h"

namespace AMDiS {

  /** \ingroup Common
   * \brief
   * A CreatorMap is used to construct objects, which types depends on key words
   * determined at run time. For example the OEMSolverMap can create the different
   * solver types depending on the solver parameter of the init file. The benefit
   * of such creator maps is, that you can extend them only by writing an creator
   * class for your own new class and give the creator together with a key word
   * to the map. 
   */
  template<typename BaseClass>
41
42
43
  class CreatorMap
  {
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
44
    /// Adds a new creator together with the given key to the map.
45
    static void addCreator(std::string key, CreatorInterface<BaseClass>* creator) 
46
    {
47
48
49
50
51
52
53
      FUNCNAME("CreatorMap::addCreator()");
      init();
      TEST_EXIT(creatorMap[key] == NULL)
	("there is already a creator for key %s\n",key.c_str());
      creatorMap[key] = creator;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
54
    /// Creates a object of the type corresponding to key.
Thomas Witkowski's avatar
Thomas Witkowski committed
55
56
    static CreatorInterface<BaseClass>* getCreator(std::string key) 
    {
57
58
59
60
61
62
63
64
65
66
67
      FUNCNAME("CreatorMap::getCreator()");
      init();
      CreatorInterface<BaseClass> *creator = creatorMap[key];
      TEST_EXIT(creator)("no creator for key %s\n", key.c_str());
      return creator;
    }

    static void clear();

    static void addDefaultCreators();

Thomas Witkowski's avatar
Thomas Witkowski committed
68
69
  protected:
    /// Constructor is protected because derived maps should be singleton.
Thomas Witkowski's avatar
Thomas Witkowski committed
70
71
    static void init() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
72
73
74
75
76
77
78
      if (!initialized) {
	initialized = true;
	NullCreator<BaseClass> *nullCreator = new NullCreator<BaseClass>;
	addCreator("0", nullCreator);
	addDefaultCreators();
      }
    }
79
80

  protected:
Thomas Witkowski's avatar
Thomas Witkowski committed
81
    /// STL map containing the pairs of keys and creators.
82
83
84
85
    static std::map< std::string, CreatorInterface<BaseClass>* > creatorMap;

    static bool initialized;
  };
86
87

  template<typename BaseClass>
88
  std::map< std::string, CreatorInterface<BaseClass>* > CreatorMap<BaseClass>::creatorMap;
89
90

  template<typename BaseClass>
91
  bool CreatorMap<BaseClass>::initialized = false;
92
93
94

}

95
96
#include "CreatorMap.hh"

97
#endif