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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>
//simple really, to refer to a class from a pointer to the base class, the base class must refer to every member
//how to make a factory for a derived class carrying settings in the constructor
using namespace std;
class Node { //abstract base class
public:
virtual string get_name(){}; //base class
virtual Node* clone(unordered_map<string,string> &_settings){};
void set(unordered_map<string,string> &_settings){settings=_settings;type=settings["type"];};
unordered_map<string,string> settings;
string get_type(){ return type; };
string type;
};
class Node_o: public Node {
public:
virtual string get_occupation(){}; //needs to be an abstract subclass implementing the interface
};
class A_node: public Node_o {
public:
A_node(){};
A_node(unordered_map<string,string> &_settings){
settings=_settings;
type=_settings["type"];
cout << "making a " << type << endl;
};
A_node* clone(unordered_map<string,string> &_settings) { return new A_node(_settings);};
string get_name(){ return "A"; };
string get_occupation(){ return "an A"; };
};
class B_node: public Node_o {
public:
B_node(){};
B_node(unordered_map<string,string> &_settings) {
settings=_settings;
type=_settings["type"];
cout << "making a " << type << endl;
};
B_node* clone(unordered_map<string,string> &_settings) { return new B_node(_settings);};
string get_name(){ return "B"; };
string get_occupation(){ return "a B"; };
};
class C_node: public Node_o {
public:
C_node(){};
C_node(unordered_map<string,string> &_settings) {
settings=_settings;
type=_settings["type"];
cout << "making a " << type << endl;
};
C_node* clone(unordered_map<string,string> &_settings) { return new C_node(_settings);};
string get_name(){ return settings["type"]; };
string get_occupation(){ return "C node"; };
};
class Node_factory {
public:
template <typename T>
T* clone(T* proto) {
cout << "cloning.. " << endl;
return new T();
};
Node_factory() {
add_type("A",new A_node());
add_type("B",new B_node());
add_type("C",new C_node());
}
Node *get_node(string type) {
if (type=="A") return new A_node();
if (type=="B") return new B_node();
if (type=="C") return new C_node();
};
unordered_map<string,Node*> type_map;
void add_type(string type,Node *proto){
type_map[type]=proto;
};
Node *create(unordered_map<string,string> &settings) {
if (settings.find("type")!=settings.end()) {
if (type_map.find(settings["type"])!=type_map.end()) {
return type_map[settings["type"]]->clone(settings);
}
}
}
Node *create(string type,unordered_map<string,string> &settings) {
if (settings.find("type")!=settings.end()) {
if (type_map.find(settings["type"])!=type_map.end()) {
Node* t=clone(type_map[settings["type"]]);
cout << "cloned.. " << endl;
t->set(settings);
return t;
//T* t= new T();
}
}
}
//so when you get a pointer from an array of pointers to base class, thats all you get
/*
Node *create(string type) {
if (type_map.find(type)!=type_map.end()) {
return (Node*)clone(type_map[type]);
}
}
Node *create(string type,string desc) {
if (type_map.find(type)!=type_map.end()) {
return (Node*)clone(type_map[type],desc);
}
}
*/
};
int main() {
Node_factory f=Node_factory();
unordered_map<string,string> settings;
vector<Node*> nodes;
settings["type"]="A";
nodes.push_back(f.create("A",settings));
settings["type"]="B";
nodes.push_back(f.create(settings));
settings["type"]="C";
//nodes.push_back(f.create(settings));
for (auto &i: nodes) {
//cout << ((Node_o*)i)->get_type() << ", " << ((Node_o*)i)->get_occupation()<< endl;
cout << ((Node_o*)i)->get_occupation()<< endl;
}
}
|