summaryrefslogtreecommitdiff
path: root/NT/src/rotor.h
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2013-12-30 08:55:15 +0000
committerTim Redfern <tim@eclectronics.org>2013-12-30 08:55:15 +0000
commitd9abdcbce9f0c3c7dbfebc00827e05536cb196e4 (patch)
tree342cb8fe4e6c176fb07aee11ede41bb5263deee7 /NT/src/rotor.h
parentf7813a5324be39d13ab536c245d15dfc602a7849 (diff)
adding node factory
Diffstat (limited to 'NT/src/rotor.h')
-rw-r--r--NT/src/rotor.h120
1 files changed, 44 insertions, 76 deletions
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 <string>
#include <sstream>
#include <iostream>
#include <map>
+#include <vector>
#include <unordered_map>
+#include <json/json.h>
+#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<std::string> options;
-
- };
-
-
-//rotor variable types
-
class Node { //base type for node pointers
public:
virtual ~Node(){
for (auto v:vars){
- //std::cerr<<"deleting "<<v.first<<" ("<<(*v.second)<<")"<<std::endl;
delete v.second;
}
}
+ virtual Node* clone(map<string,string> &_settings)=0;
+ std::string type;
protected:
std::unordered_map<std::string,Variable*> vars;
};
template <class NT> class Node_type : public Node {
public:
virtual const NT& get_output(const Frame_parameters &frame)=0;
- void init(std::map<std::string,std::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());
- }
- };
- }
- 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 <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 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 time: public Node_type<double> {
+ 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<double> {
- public:
- multiply(){
- inlet=create_inlet<double>("inlet");
- value=create_inlet<double>("value");
- }
- const double &get_output(const Frame_parameters &frame){
- result=inlet->get(frame)*value->get(frame);
- return result;
- }
- private:
- Variable_type<double> *inlet;
- Variable_type<double> *value;
- double result;
- };
-class print: public Node_type<std::string> {
- public:
- print(){
- inlet=create_inlet<double>("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<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:
- Variable_type<double> *inlet;
- std::string result;
+ std::map<std::string,Node*> type_map;
+ std::map<std::string,std::vector<Rotor::Node*> > 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