AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
CreatorMap.hpp
1#pragma once
2
3// std c++ headers
4#include <map>
5#include <string>
6
7// AMDiS includes
8#include <amdis/CreatorInterface.hpp>
9
10namespace AMDiS
11{
12 // forward declaration.
13 // All classes that need creators must specialize this class and implement
14 // a static void init() method.
15 template <class BaseClass>
17
18
28 template <class BaseClass>
30 {
31 public:
32 using CreatorMapType = std::map< std::string, CreatorInterface<BaseClass>* >;
33
34 public:
36 static void addCreator(std::string key, CreatorInterface<BaseClass>* creator)
37 {
38 init();
39 test_exit(!creatorMap[key], "There is already a creator for key {}", key);
40 creatorMap[key] = creator;
41 }
42
44 static CreatorInterface<BaseClass>* get(std::string key, std::string initFileStr)
45 {
46 init();
47
48 auto it = creatorMap.find(key);
49 if (it == creatorMap.end()) {
50 warning("No creator for key `{}` defined (used in init-file parameter `{}`). Falling back to `default`.", key, initFileStr);
51 key = "default";
52 }
53
54 auto creator = creatorMap[key];
55 test_exit(creator, "Undefined creator for `{}` (used in init-file parameter `{}`)", key, initFileStr);
56
57 return creator;
58 }
59
60 protected:
61 static void init()
62 {
63 if (!initialized) {
64 initialized = true;
66 }
67 }
68
69 protected:
71 static CreatorMapType creatorMap;
72
73 static bool initialized;
74 };
75
76 template <class BaseClass>
77 typename CreatorMap<BaseClass>::CreatorMapType CreatorMap<BaseClass>::creatorMap;
78
79 template <class BaseClass>
81
82} // end namespace AMDiS
Interface for the implementation of the factory method pattern. The creation of an object of a sub cl...
Definition: CreatorInterface.hpp:24
A CreatorMap is used to construct objects, which types depends on key words determined at run time....
Definition: CreatorMap.hpp:30
static CreatorInterface< BaseClass > * get(std::string key, std::string initFileStr)
Creates a object of the type corresponding to key.
Definition: CreatorMap.hpp:44
static void addCreator(std::string key, CreatorInterface< BaseClass > *creator)
Adds a new creator together with the given key to the map.
Definition: CreatorMap.hpp:36
static CreatorMapType creatorMap
STL map containing the pairs of keys and creators.
Definition: CreatorMap.hpp:71
Definition: CreatorMap.hpp:16