diff options
| author | Tim Redfern <tim@eclectronics.org> | 2013-12-31 12:41:42 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2013-12-31 12:41:42 +0000 |
| commit | 820b73f2abdf51bd59cb5b74739fb6f264b7c54e (patch) | |
| tree | 9322528d7c3e8e7aa23cdcbca35ebf17bba67529 /NT/src | |
| parent | d9abdcbce9f0c3c7dbfebc00827e05536cb196e4 (diff) | |
factory not working with templates
Diffstat (limited to 'NT/src')
| -rw-r--r-- | NT/src/nodes.h | 35 | ||||
| -rw-r--r-- | NT/src/rotor.cpp | 112 | ||||
| -rw-r--r-- | NT/src/rotor.h | 48 |
3 files changed, 83 insertions, 112 deletions
diff --git a/NT/src/nodes.h b/NT/src/nodes.h index 5bb8830..8ce7cdd 100644 --- a/NT/src/nodes.h +++ b/NT/src/nodes.h @@ -1,45 +1,51 @@ +#ifndef ROTOR_NODES_H +#define ROTOR_NODES_H + #include "rotor.h" -namespace Rotor { - class time: public Node_type<double> { + +using namespace std; + +namespace Rotor{ + class Time: public Node_type<double> { public: - time(){}; - time(map<string,string> &settings):time() { + Time(){}; + Time(map<string,string> &settings):Time() { init(settings); }; const double &get_output(const Frame_parameters &frame){ value=frame.time; return value; } - time* clone(map<string,string> &_settings) { return new time(_settings);}; + Time* clone(map<string,string> &_settings) { return new Time(_settings);}; private: double value; }; - class multiply: public Node_type<double> { + class Multiply: public Node_type<double> { public: - multiply(){ + Multiply(){ inlet=create_inlet<double>("inlet"); value=create_inlet<double>("value"); } - multiply(map<string,string> &settings):multiply() { + Multiply(map<string,string> &settings):Multiply() { init(settings); }; const double &get_output(const Frame_parameters &frame){ result=inlet->get(frame)*value->get(frame); return result; } - multiply* clone(map<string,string> &_settings) { return new multiply(_settings);}; + Multiply* clone(map<string,string> &_settings) { return new Multiply(_settings);}; private: Variable_type<double> *inlet; Variable_type<double> *value; double result; }; - class print: public Node_type<std::string> { + class Print: public Node_type<std::string> { public: - print(){ + Print(){ inlet=create_inlet<double>("inlet"); } - print(map<string,string> &settings):print() { + Print(map<string,string> &settings):Print() { init(settings); }; const std::string &get_output(const Frame_parameters &frame){ @@ -48,9 +54,12 @@ namespace Rotor { result=out.str(); return result; } - print* clone(map<string,string> &_settings) { return new print(_settings);}; + Print* clone(map<string,string> &_settings) { return new Print(_settings);}; private: Variable_type<double> *inlet; std::string result; }; } + + +#endif //ROTOR_NODES_H diff --git a/NT/src/rotor.cpp b/NT/src/rotor.cpp index c10520a..597ded6 100644 --- a/NT/src/rotor.cpp +++ b/NT/src/rotor.cpp @@ -2,81 +2,71 @@ #include "rotor.h" #include "nodes.h" +#include "factory.h" using namespace std; -using namespace Rotor; - -template <class T> -void Rotor::Variable_type<T>::init(std::string s){ - std::istringstream cur(s); - cur >> value; -} -template <class T> -bool Rotor::Variable_type<T>::connect(Node* target){ - if (connectable){ - if (dynamic_cast<Node_type<T>*>(target)){ - connection=target; - return true; - } +namespace Rotor{ + template <class T> + void Rotor::Variable_type<T>::init(std::string s){ + std::istringstream cur(s); + cur >> value; } - return false; -} -template <class T> -const T& Rotor::Variable_type<T>::get(const Frame_parameters &frame){ - if (connection){ - return (dynamic_cast<Node_type<T>*>(connection))->get_output(frame); + template <class T> + bool Rotor::Variable_type<T>::connect(Node* target){ + if (connectable){ + if (dynamic_cast<Node_type<T>*>(target)){ + connection=target; + return true; + } + } + return false; } - return value; -} - -template <class NT> -void Node_type<NT>::init(map<string,string> 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()); + template <class T> + const T& Rotor::Variable_type<T>::get(const Frame_parameters &frame){ + if (connection){ + return (dynamic_cast<Node_type<T>*>(connection))->get_output(frame); } - }; -} - -template <class NT> -bool Node_type<NT>::connect(string v,Node *t){ - auto var=vars.find(v); - if (var!=vars.end()){ - if ((*var).second->connect(t)){ - return true; + return value; + } + bool Node::connect(string v,Node *t){ + auto var=vars.find(v); + if (var!=vars.end()){ + if ((*var).second->connect(t)){ + return true; + } } + return false; + } + template <class NT> + void Node_type<NT>::init(map<string,string> 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()); + } + }; } - return false; -} - -template <class NT> -template <class IT> -Variable_type<IT>* Node_type<NT>::create_inlet(std::string name){ - vars[name]=new Variable_type<IT>(true); - return (dynamic_cast<Variable_type<IT>*>(vars[name])); } -template <class NT> -template <class IT> -Variable_type<IT>* Node_type<NT>::create_attribute(std::string name){ - vars[name]=new Variable_type<IT>(false); - return (dynamic_cast<Variable_type<IT>*>(vars[name])); -} +using namespace Rotor; int main(){ - Rotor::time t; - multiply m; - map<string,string> settings={{"value","2"}}; - m.init(settings); - if (!m.connect("inlet",&t)) printf("not connected...\n"); - Rotor::print p; - if (!p.connect("inlet",&m)) printf("not connected...\n"); + Node_factory f; + map<string,string> settings={{"type","time"}}; + Node *t=f.create(settings); + settings={{"value","2"},{"type","multiply"}}; + Node *m=f.create(settings); + if (!m->connect("inlet",t)) printf("not connected...\n"); + settings={{"type","print"}}; + Node *p=f.create(settings); + if (!p->connect("inlet",m)) printf("not connected...\n"); for (double t=0;t<10.0;t+=0.765){ Frame_parameters f=Frame_parameters(t,25.0,10.0,640,360); - printf("%04f %s\n",t,p.get_output(f).c_str()); - + printf("%04f %s\n",t,(dynamic_cast<Node_type<string>*>(p))->get_output(f).c_str()); } + delete t; + delete m; + delete p; } diff --git a/NT/src/rotor.h b/NT/src/rotor.h index 26f7fb0..49e978f 100644 --- a/NT/src/rotor.h +++ b/NT/src/rotor.h @@ -9,7 +9,7 @@ #include <unordered_map> #include <json/json.h> -#include "xmlIO.h" + namespace Rotor { @@ -68,7 +68,8 @@ namespace Rotor { delete v.second; } } - virtual Node* clone(map<string,string> &_settings)=0; + bool connect(std::string v,Node *t); + virtual Node* clone(std::map<std::string,std::string> &_settings)=0; std::string type; protected: std::unordered_map<std::string,Variable*> vars; @@ -77,43 +78,14 @@ namespace Rotor { public: virtual const NT& get_output(const Frame_parameters &frame)=0; void init(std::map<std::string,std::string> settings); - bool connect(std::string v,Node *t); - template <class IT> Variable_type<IT>* create_inlet(std::string name); - template <class IT> Variable_type<IT>* create_attribute(std::string name); - }; - class Node_factory{ - public: - Node_factory(); - ~Node_factory(){ - for (auto t:type_map) delete t.second; + template <class IT> Variable_type<IT>* create_inlet(std::string name){ + vars[name]=new Variable_type<IT>(true); + return (dynamic_cast<Variable_type<IT>*>(vars[name])); + } + template <class IT> Variable_type<IT>* create_attribute(std::string name){ + vars[name]=new Variable_type<IT>(false); + return (dynamic_cast<Variable_type<IT>*>(vars[name])); } - 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<Rotor::Node*> &category){ - add_type(type,proto); - category.push_back(proto); - }; - Node *create(std::map<std::string,std::string> &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: - std::map<std::string,Node*> type_map; - std::map<std::string,std::vector<Rotor::Node*> > category; }; } |
