summaryrefslogtreecommitdiff
path: root/Ianimal/Ianimal.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Ianimal/Ianimal.cpp')
-rw-r--r--Ianimal/Ianimal.cpp95
1 files changed, 83 insertions, 12 deletions
diff --git a/Ianimal/Ianimal.cpp b/Ianimal/Ianimal.cpp
index 5aa70cd..2814757 100644
--- a/Ianimal/Ianimal.cpp
+++ b/Ianimal/Ianimal.cpp
@@ -1,37 +1,75 @@
#include <string>
#include <iostream>
-#include <map>
+#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() = 0; //signal output
+ 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 {
+
+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 {
+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 {
+class C_node: public Node_o {
public:
- string get_name(){ return "C"; };
- string get_occupation(){ return "a C"; };
+ 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() {
@@ -44,24 +82,57 @@ class Node_factory {
if (type=="B") return new B_node();
if (type=="C") return new C_node();
};
- map<string,Node*> type_map;
+ 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;
- nodes.push_back(f.get_node("A"));
- nodes.push_back(f.get_node("B"));
- nodes.push_back(f.get_node("C"));
+ 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 << i->get_name() << endl;
+ //cout << ((Node_o*)i)->get_type() << ", " << ((Node_o*)i)->get_occupation()<< endl;
+ cout << ((Node_o*)i)->get_occupation()<< endl;
}
} \ No newline at end of file