CreatorMap.h 3.62 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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \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>
    class CreatorMap
    {
    public:
      /** \brief
       * Adds a new creator together with the given key to the map.
       */
48
      static void addCreator(std::string key, CreatorInterface<BaseClass>* creator) 
49
50
51
52
53
54
55
56
57
58
59
      {
	FUNCNAME("CreatorMap::addCreator()");
	init();
	TEST_EXIT(creatorMap[key] == NULL)
	  ("there is already a creator for key %s\n",key.c_str());
	creatorMap[key] = creator;
      };

      /** \brief
       * Creates a object of the type corresponding to key.
       */
60
      static CreatorInterface<BaseClass>* getCreator(std::string key) {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	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 addDefaultCreators();

    protected:
      /** \brief
       * Constructor is protected because derived maps should be singleton.
       */
      static void init() {
	if(!initialized) {
	  initialized = true;
	  NullCreator<BaseClass> *nullCreator = new NullCreator<BaseClass>;
	  addCreator("no", nullCreator);
	  addCreator("0", nullCreator);
	  addDefaultCreators();
	}
      };

    protected:
      /** \brief
       * STL map containing the pairs of keys and creators.
       */
88
      static std::map< std::string, CreatorInterface<BaseClass>* > creatorMap;
89
90
91
92
93

      static bool initialized;
    };

  template<typename BaseClass>
94
    std::map< std::string, CreatorInterface<BaseClass>* > CreatorMap<BaseClass>::creatorMap;
95
96
97
98
99
100
101

  template<typename BaseClass>
    bool CreatorMap<BaseClass>::initialized = false;

}

#endif