CreatorMap.h 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/** \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
37
   * determined at run time. For example the LinearSolverMap can create the different
38
39
40
41
42
43
   * 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>
44
45
  class CreatorMap
  {
46
47
48
  public:
    typedef std::map< std::string, CreatorInterface<BaseClass>* > CreatorMapType;
    
49
  public:
Thomas Witkowski's avatar
Thomas Witkowski committed
50
    /// Adds a new creator together with the given key to the map.
51
    static void addCreator(std::string key, CreatorInterface<BaseClass>* creator) 
52
    {
53
54
      FUNCNAME("CreatorMap::addCreator()");
      init();
55
      TEST_EXIT(creatorMap[key] == NULL)
56
57
58
	("there is already a creator for key %s\n",key.c_str());
      creatorMap[key] = creator;
    }
59
60
61
62
63
    
    static void addCreator(std::string backend, std::string key, CreatorInterface<BaseClass>* creator)
    {
      addCreator(backend + "_" + key, creator);
    }
64

Thomas Witkowski's avatar
Thomas Witkowski committed
65
    /// Creates a object of the type corresponding to key.
66
67
    static CreatorInterface<BaseClass>* getCreator(std::string key,
						   std::string initFileStr) 
Thomas Witkowski's avatar
Thomas Witkowski committed
68
    {
69
70
71
      FUNCNAME("CreatorMap::getCreator()");
      init();
      CreatorInterface<BaseClass> *creator = creatorMap[key];
72
73
74
75
      TEST_EXIT(creator)
	("No creator for key \"%s\" defined in init file for parameter \"%s\"\n", 
	  key.c_str(), initFileStr.c_str());
	
76
77
78
79
80
81
82
      return creator;
    }

    static void clear();

    static void addDefaultCreators();

Thomas Witkowski's avatar
Thomas Witkowski committed
83
84
  protected:
    /// Constructor is protected because derived maps should be singleton.
Thomas Witkowski's avatar
Thomas Witkowski committed
85
86
    static void init() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
87
88
89
90
91
92
93
      if (!initialized) {
	initialized = true;
	NullCreator<BaseClass> *nullCreator = new NullCreator<BaseClass>;
	addCreator("0", nullCreator);
	addDefaultCreators();
      }
    }
94
95

  protected:
Thomas Witkowski's avatar
Thomas Witkowski committed
96
    /// STL map containing the pairs of keys and creators.
97
    static CreatorMapType creatorMap;
98
99
100

    static bool initialized;
  };
101
102

  template<typename BaseClass>
103
  typename CreatorMap<BaseClass>::CreatorMapType CreatorMap<BaseClass>::creatorMap;
104
105

  template<typename BaseClass>
106
  bool CreatorMap<BaseClass>::initialized = false;
107
108
109

}

110
111
#include "CreatorMap.hh"

112
#endif