diff options
| author | Tim Redfern <tim@herge.(none)> | 2013-07-04 10:49:55 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@herge.(none)> | 2013-07-04 10:49:55 +0100 |
| commit | fddff1ff3e50260b6518e754714b46e28fefaea6 (patch) | |
| tree | 3aa94083cde9a5d0086ae3e1365d4c7bcbd0e94d /Ianimal | |
| parent | 729e602bbf825f909193fd130d68ee9eeafb0eae (diff) | |
tidying
Diffstat (limited to 'Ianimal')
| -rw-r--r-- | Ianimal/Ianimal.cpp | 138 | ||||
| -rwxr-xr-x | Ianimal/make | 1 |
2 files changed, 0 insertions, 139 deletions
diff --git a/Ianimal/Ianimal.cpp b/Ianimal/Ianimal.cpp deleted file mode 100644 index 2814757..0000000 --- a/Ianimal/Ianimal.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#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; - } -}
\ No newline at end of file diff --git a/Ianimal/make b/Ianimal/make deleted file mode 100755 index 7c1cc2e..0000000 --- a/Ianimal/make +++ /dev/null @@ -1 +0,0 @@ -g++ Ianimal.cpp -std=c++11 -o Ianimal |
