diff options
Diffstat (limited to 'NT/src/rotor.h')
| -rw-r--r-- | NT/src/rotor.h | 138 |
1 files changed, 82 insertions, 56 deletions
diff --git a/NT/src/rotor.h b/NT/src/rotor.h index 6d56299..485ec8c 100644 --- a/NT/src/rotor.h +++ b/NT/src/rotor.h @@ -8,7 +8,11 @@ TJR-Jan-2014 NB when plugging in to framework - convert all cerr messages to logged -What next? +Test suite + +pipe to send commands? + +seperate framework? ie in Python- load server- send http commands */ #define ENABLE_TYPENAME(A) template<> struct TypeName<A> { static const char *Get() { return #A; }}; @@ -93,10 +97,15 @@ namespace Rotor { operator int () const { //overload C style cast to int return value; } - friend std::istream& operator>> (std::istream &in, Rotor::Enum &en); + //only works inside class definition? + friend std::istream &operator>> (std::istream &in, Rotor::Enum &en) { + in >> en.value; + return in; + }; + protected: + int value; private: std::vector<std::string> labels; - int value; }; class Audio_frame{ @@ -150,6 +159,47 @@ namespace Rotor { bool connectable; std::string input; }; + class Node { //base type for node pointers + public: + Node(){type="";type_id="";id="";description="";}; + virtual ~Node(){ + for (auto v:vars){ + delete v.second; + } + } + 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; + } + std::string get_type(){return type;}; + std::string& get_id(){return id;}; + std::string& get_description(){return description;}; + Json::Value to_json(); + virtual Node* clone(Json::Value &_settings)=0; + virtual std::string get_output_type()=0; + void create_connections(std::unordered_map<std::string,Node*> &nodes){ + Logger& logger = Logger::get(log_id); + for (auto var:vars) { + if (var.second->create_connection(nodes)) { + logger.information("Connected input '"+var.second->get_name()+"' of node '"+id+"' to node "+var.second->get_connection_id()); + } + } + } + protected: + std::unordered_map<std::string,Variable*> vars; + std::string type; + std::string id; + std::string type_id; + std::string description; + std::string title; + std::string ui_type; + std::string log_id; + }; template <class T> class Variable_type : public Variable { public: Variable_type(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable(_name,_description,_title){ @@ -172,10 +222,17 @@ namespace Rotor { } return false; } - Json::Value to_json(); - std::string get_type(){ //need this to output node templates - return TypeName<T>::Get(); - } + Json::Value to_json(){ + Json::Value json; + json["type"]=get_type(); + json["name"]=name; + json["connectable"]=connectable?"yes":"no"; + json["input"]=connection?connection->get_id():""; + return json; + } + std::string get_type(){ //need this to output node templates + return TypeName<T>::Get(); + } //have to cast connect and get_output to use templated return types bool connect(Node* target){ if (connectable){ @@ -200,6 +257,7 @@ namespace Rotor { T value; }; class Variable_array: public Variable { + //base type for a variable amount of inlets public: Variable_array(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable(_name,_description,_title){ connectable=_connectable; @@ -208,32 +266,41 @@ namespace Rotor { std::vector<Variable> values; }; template <class T> class Variable_array_type: public Variable_array { + //is it possible to overload array access operator here? public: Variable_array_type(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true): Variable_array(_name,_description,_title,connectable){}; void init(Json::Value s){ name=s["name"].asString(); if (!s["input"].empty()){ - add(s["input"].size()); + for (int i;i<s["input"].size();i++){ + add(s["input"][i]); + values[i].init(s["input"][i]); + } } } bool create_connection(std::unordered_map<std::string,Node*> &nodes){ - bool success=true; + bool success=false; //for (auto v:values){ //weirdly does not work even though it seems to! maybe it returns a copy of of the object? // v.create_connection(nodes); //} for (uint32_t i=0;i<values.size();i++) { - if (!values[i].create_connection(nodes)) success=false; + if (values[i].create_connection(nodes)) { + success=true; + } } return success; } Json::Value to_json(); - std::string get_type(){ - return TypeName<T>::Get(); - } + std::string get_type(){ + return TypeName<T>::Get(); + } bool connect(Node* target){ - //array does not connect this way + //array does not connect this way- just connect 1st inlet? return false; } + void add(Json::Value s){ + values.push_back(Variable_type<T>(s.get("name","").asString(),s.get("description","").asString(),s.get("title","").asString(),s.get("connectable",false).asBool())); + } void add(std::string _name="",std::string _description="",std::string _title="",bool _connectable=true){ values.push_back(Variable_type<T>(_name,_description,_title,_connectable)); } @@ -260,48 +327,7 @@ namespace Rotor { std::vector<Variable_type<T>> values; T value; }; - //could specialise a variable type operator double() etc to allow direct cast - class Node { //base type for node pointers - public: - Node(){type="";type_id="";id="";description="";}; - virtual ~Node(){ - for (auto v:vars){ - delete v.second; - } - } - 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; - } - std::string get_type(){return type;}; - std::string& get_id(){return id;}; - std::string& get_description(){return description;}; - Json::Value to_json(); - virtual Node* clone(Json::Value &_settings)=0; - virtual std::string get_output_type()=0; - void create_connections(std::unordered_map<std::string,Node*> &nodes){ - Logger& logger = Logger::get(log_id); - for (auto var:vars) { - if (var.second->create_connection(nodes)) { - logger.information("Connected input '"+var.second->get_name()+"' of node '"+id+"' to node "+var.second->get_connection_id()); - } - } - } - protected: - std::unordered_map<std::string,Variable*> vars; - std::string type; - std::string id; - std::string type_id; - std::string description; - std::string title; - std::string ui_type; - std::string log_id; - }; + template <class NT> class Node_type : public Node { public: virtual const NT& get_output(const Frame_parameters &frame){return value;}; |
