From d9abdcbce9f0c3c7dbfebc00827e05536cb196e4 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Mon, 30 Dec 2013 08:55:15 +0000 Subject: adding node factory --- NT/src/rotor.h | 120 +++++++++++++++++++++------------------------------------ 1 file changed, 44 insertions(+), 76 deletions(-) (limited to 'NT/src/rotor.h') diff --git a/NT/src/rotor.h b/NT/src/rotor.h index 2d8c7d7..26f7fb0 100644 --- a/NT/src/rotor.h +++ b/NT/src/rotor.h @@ -1,9 +1,15 @@ +#ifndef ROTOR_H +#define ROTOR_H + #include #include #include #include +#include #include +#include +#include "xmlIO.h" namespace Rotor { @@ -51,102 +57,64 @@ namespace Rotor { public: Variable_type(bool _c){connectable=_c;}; void init(std::string s); - bool connect(Node* target); + bool connect(Node* target); const T& get(const Frame_parameters &frame); T value; }; - class enum{ - public: - vector options; - - }; - - -//rotor variable types - class Node { //base type for node pointers public: virtual ~Node(){ for (auto v:vars){ - //std::cerr<<"deleting "< &_settings)=0; + std::string type; protected: std::unordered_map vars; }; template class Node_type : public Node { public: virtual const NT& get_output(const Frame_parameters &frame)=0; - void init(std::map settings){ - for (auto s:settings) { - if (vars.find(s.first)!=vars.end()){ - vars[s.first]->init(s.second); - //printf("set %s to %s\n",s.first.c_str(),s.second.c_str()); - } - }; - } - bool connect(std::string v,Node *t){ - auto var=vars.find(v); - if (var!=vars.end()){ - if ((*var).second->connect(t)){ - return true; - } - } - return false; - } - template Variable_type* create_inlet(std::string name){ - vars[name]=new Variable_type(true); - return (dynamic_cast*>(vars[name])); - } - template Variable_type* create_attribute(std::string name){ - vars[name]=new Variable_type(false); - return (dynamic_cast*>(vars[name])); - } + void init(std::map settings); + bool connect(std::string v,Node *t); + template Variable_type* create_inlet(std::string name); + template Variable_type* create_attribute(std::string name); }; - class time: public Node_type { + class Node_factory{ public: - const double &get_output(const Frame_parameters &frame){ - value=frame.time; - return value; - } - private: - double value; - }; - class multiply: public Node_type { - public: - multiply(){ - inlet=create_inlet("inlet"); - value=create_inlet("value"); - } - const double &get_output(const Frame_parameters &frame){ - result=inlet->get(frame)*value->get(frame); - return result; - } - private: - Variable_type *inlet; - Variable_type *value; - double result; - }; -class print: public Node_type { - public: - print(){ - inlet=create_inlet("inlet"); - } - const std::string &get_output(const Frame_parameters &frame){ -std::ostringstream out; - out << inlet->get(frame); - result=out.str(); - return result; + Node_factory(); + ~Node_factory(){ + for (auto t:type_map) delete t.second; } + void add_type(std::string type,Node* proto){ + type_map[type]=proto; + type_map[type]->type=type; + }; + void add_type(std::string type,Node* proto,std::vector &category){ + add_type(type,proto); + category.push_back(proto); + }; + Node *create(std::map &settings){ + if (settings.find("type")!=settings.end()) { + if (type_map.find(settings["type"])!=type_map.end()) { + return type_map[settings["type"]]->clone(settings); + } + } + return NULL; + }; + bool list_node(const std::string &t,xmlIO XML); + bool list_node(const std::string &t,Json::Value &JSON); + void list_node(Rotor::Node* type,xmlIO XML,int i=0); + Json::Value list_node(Rotor::Node* type); + void list_nodes(xmlIO XML); + void list_nodes(Json::Value &JSON); + void list_categories(xmlIO XML); + void list_categories(Json::Value &JSON); private: - Variable_type *inlet; - std::string result; + std::map type_map; + std::map > category; }; - } -//next:: make a couple of nodes that do something -//test them -//make loading and saving functions -//xml or json? +#endif //ROTOR_H \ No newline at end of file -- cgit v1.2.3