From ce528aa299501ecab63018f91b937766ffbc95be Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Sun, 19 Jan 2014 19:54:38 +0000 Subject: variables writing json --- NT/src/factory.cpp | 99 +++--------------------------------------------------- NT/src/factory.h | 19 ++--------- NT/src/nodes.h | 18 +++++----- NT/src/rotor.cpp | 34 ++++++++++++++++--- NT/src/rotor.h | 43 +++++------------------- 5 files changed, 54 insertions(+), 159 deletions(-) (limited to 'NT/src') diff --git a/NT/src/factory.cpp b/NT/src/factory.cpp index c02bcee..73a96f4 100644 --- a/NT/src/factory.cpp +++ b/NT/src/factory.cpp @@ -4,109 +4,18 @@ using namespace Rotor; Node_factory::Node_factory(){ //for now, statically load prototype map in constructor - add_type(new Time(),"nodes"); add_type(new Multiply(),"nodes"); add_type(new Print(),"nodes"); } -/* -bool Node_factory::list_node(const string &t,xmlIO XML){ - for (auto& type: type_map) { - if (type.first==t) { - list_node(type.second,XML); - return true; - } - } - XML.addValue("error","Node /"+t+"/ not found"); - return false; -}; -*/ -bool Node_factory::list_node(const string &t,Json::Value &JSON){ +bool Node_factory::list_node(const string &_type,Json::Value &json){ for (auto& type: type_map) { - if (type.first==t) { - JSON["node"]=type.second->list_json(); + if (type.first==_type) { + json["node"]=type.second->to_json(); return true; } } - JSON["error"]="Node /"+t+"/ not found"; + json["error"]="Node '"+_type+"' not found"; return false; }; -/* -void Node_factory::list_node(Rotor::Node* type,xmlIO XML,int i){ - XML.addTag("node"); - XML.addAttribute("node","output_type",type->output_type(),i); - XML.addAttribute("node","node_type",type->node_type,i); - XML.addAttribute("node","title",type->title,i); - XML.addAttribute("node","description",type->description,i); - XML.addAttribute("node","node_id",type->node_id,i); - XML.addAttribute("node","ui_type",type->ui_type,i); - XML.pushTag("node",i); - - //if (type->description!="") { - // XML.addTag("description"); - // XML.setValue("description",type->description,0); - //} - int j=0; - for (auto& input: type->inputs) { - XML.addTag("signal_input"); - XML.addAttribute("signal_input","title",input->title,j); - XML.addAttribute("signal_input","description",input->description,j); - j++; - } - j=0; - if (dynamic_cast (type)!=nullptr) { - for (auto& input: (dynamic_cast(type))->image_inputs) { - XML.addTag("image_input"); - XML.addAttribute("image_input","title",input->title,j); - XML.addAttribute("image_input","description",input->description,j); - j++; - } - } - j=0; - for (auto& parameter: type->parameters) { - XML.addTag("parameter"); - XML.addAttribute("parameter","name",parameter.first,j); - XML.addAttribute("parameter","type",parameter.second->type,j); - XML.addAttribute("parameter","title",parameter.second->title,j); - XML.addAttribute("parameter","description",parameter.second->description,j); - XML.addAttribute("parameter","value",parameter.second->value,j); - XML.addAttribute("parameter","min",parameter.second->min,j); - XML.addAttribute("parameter","max",parameter.second->max,j); - XML.addAttribute("parameter","step",parameter.second->step,j); - j++; - } - j=0; - for (auto& attribute: type->attributes) { - XML.addTag("attribute"); - XML.addAttribute("attribute","name",attribute.first,j); - XML.addAttribute("attribute","title",attribute.second->title,j); - XML.addAttribute("attribute","description",attribute.second->description,j); - if (attribute.second->vals.size()){ //document attribute enumeration - XML.addAttribute("attribute","value",attribute.second->value,j); - XML.addAttribute("attribute","type","enum",j); - XML.pushTag("attribute",j); - int k=0; - for (auto val: attribute.second->vals){ - XML.addTag("option"); - XML.addAttribute("option","value",val,k); - k++; - } - XML.popTag(); - } - else { - XML.addAttribute("attribute","type",attribute.second->type,j); - if (attribute.second->type=="array"){ - XML.pushTag("attribute",j); - XML.addTag("value"); - XML.popTag(); - } - else XML.addAttribute("attribute","value",attribute.second->value,j); - } - j++; - } - - XML.popTag(); -} - -*/ diff --git a/NT/src/factory.h b/NT/src/factory.h index b576c49..0482ebe 100644 --- a/NT/src/factory.h +++ b/NT/src/factory.h @@ -20,28 +20,15 @@ namespace Rotor { if (category_map.find(category)==category_map.end()) category_map[category]=vector(); category_map[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; - }; Node *create(Json::Value &settings){ if (type_map.find(settings["type"].asString())!=type_map.end()) { return type_map[settings["type"].asString()]->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); - void list_nodes(xmlIO XML); - void list_nodes(Json::Value &JSON); - void list_categories(xmlIO XML); - void list_categories(Json::Value &JSON); + bool list_node(const std::string &t,Json::Value &json); + void list_nodes(Json::Value &json); + void list_categories(Json::Value &json); private: std::map type_map; std::map > category_map; diff --git a/NT/src/nodes.h b/NT/src/nodes.h index ede81eb..64d14a2 100644 --- a/NT/src/nodes.h +++ b/NT/src/nodes.h @@ -13,14 +13,14 @@ namespace Rotor{ class Time: public Double_node { public: Time(){type="time";}; - Time(map &settings):Time() { + Time(Json::Value &settings):Time() { init(settings); }; const double &get_output(const Frame_parameters &frame){ value=frame.time; return value; } - Time* clone(map &_settings) { return new Time(_settings);}; + Time* clone(Json::Value &_settings) { return new Time(_settings);}; private: double value; }; @@ -34,15 +34,15 @@ namespace Rotor{ type_id="11c67850-7ce4-11e3-abf6-930ef8613c46"; ui_type="none"; } - Multiply(map &settings):Multiply() { + Multiply(Json::Value &settings):Multiply() { init(settings); }; const double &get_output(const Frame_parameters &frame){ - double val=1.0f; - for (auto var:factors->values) val*=var.get(frame); - return val; + result=1.0f; + for (auto var:factors->values) result*=var.get(frame); + return result; } - Multiply* clone(map &_settings) { return new Multiply(_settings);}; + Multiply* clone(Json::Value &_settings) { return new Multiply(_settings);}; private: Variable_array_type *factors; double result; @@ -57,7 +57,7 @@ namespace Rotor{ inlet=create_inlet("inlet"); type="print"; } - Print(map &settings):Print() { + Print(Json::Value &settings):Print() { init(settings); }; const std::string &get_output(const Frame_parameters &frame){ @@ -66,7 +66,7 @@ namespace Rotor{ result=out.str(); return result; } - Print* clone(map &_settings) { return new Print(_settings);}; + Print* clone(Json::Value &_settings) { return new Print(_settings);}; private: Variable_type *inlet; std::string result; diff --git a/NT/src/rotor.cpp b/NT/src/rotor.cpp index d244d49..eeffab9 100644 --- a/NT/src/rotor.cpp +++ b/NT/src/rotor.cpp @@ -7,7 +7,29 @@ using namespace std; using namespace Rotor; -Json::Value Node::list_json(){ +template +Json::Value Variable_type::to_json(){ + Json::Value json; + json["type"]=get_type(); + json["name"]=name; + json["connectable"]=connectable?"yes":"no"; + json["input"]=connection?connection->id:""; + return json; +} + +template +Json::Value Variable_array_type::to_json(){ + Json::Value json; + json["type"]=get_type(); + json["name"]=name; + json["connectable"]=connectable?"yes":"no"; + json["input"]=Json::arrayValue; + for (auto& input: values) { + json["input"].append(input.connection?input.connection->id:""); + } + return json; +} +Json::Value Node::to_json(){ Json::Value node; node["type"]=type; node["type_id"]=type_id; @@ -20,6 +42,8 @@ Json::Value Node::list_json(){ if (vars.size()){ node["vars"]=Json::arrayValue; for (auto& var: vars) { + node["vars"].append(var.second->to_json()); +/* string type=var.second->get_type(); Json::Value newvar; newvar["type"]=var.second->get_type(); @@ -27,15 +51,15 @@ Json::Value Node::list_json(){ newvar["connectable"]=var.second->connectable?"yes":"no"; if (dynamic_cast(var.second)){ newvar["input"]=Json::arrayValue; - for (auto& element: dynamic_cast(var.second)->values) { - Json::Value newvalue; - newvar["input"].append(newvalue); + for (auto& input: dynamic_cast(var.second)->values) { + newvar["input"].append(input.to_json()); } } else { - newvar["input"]=Json::Value(); + newvar["input"]=var.second->to_json(); } node["vars"].append(newvar); +*/ } } /* diff --git a/NT/src/rotor.h b/NT/src/rotor.h index 0138191..9228388 100644 --- a/NT/src/rotor.h +++ b/NT/src/rotor.h @@ -78,9 +78,9 @@ namespace Rotor { class Variable { //pure virtual base type for variable pointers public: Variable(){connection=nullptr;}; - virtual void init(std::string s)=0; - virtual void init(Json::Value s)=0; virtual ~Variable(){}; + virtual Json::Value to_json()=0; + virtual void init(Json::Value s)=0; virtual bool connect(Node* target)=0; virtual std::string get_type()=0; Node* connection; @@ -89,16 +89,13 @@ namespace Rotor { }; template class Variable_type : public Variable { public: - Variable_type(std::string n,bool _c){name=n;connectable=_c;}; - void init(std::string s){ - std::istringstream cur(s); - cur >> value; - } + Variable_type(std::string _name,bool _connectable){name=_name;connectable=_connectable;}; void init(Json::Value s){ std::istringstream cur(s["value"].asString()); cur >> value; name=s["name"].asString(); } + Json::Value to_json(); std::string get_type(){ //need this to output node templates return TypeName::Get(); } @@ -128,15 +125,12 @@ namespace Rotor { template class Variable_array_type: public Variable_array { public: Variable_array_type(std::string n){name=n;connectable=true;}; - void init(std::string s){ - std::istringstream cur(s); - cur >> value; - } void init(Json::Value s){ std::istringstream cur(s["value"].asString()); cur >> value; name=s["name"].asString(); } + Json::Value to_json(); std::string get_type(){ //need this to output node templates return TypeName::Get(); } @@ -193,8 +187,8 @@ namespace Rotor { } return false; } - Json::Value list_json(); - virtual Node* clone(std::map &_settings)=0; + Json::Value to_json(); + virtual Node* clone(Json::Value &_settings)=0; virtual std::string output_type()=0; std::string type; std::string id; @@ -207,32 +201,13 @@ namespace Rotor { 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()); - } - } - } void init(Json::Value settings){ - if (settings["vars"]){ + if (settings["vars"]!=nullptr){ for ( uint32_t i = 0; i < settings["vars"].size(); ++i ) { vars[settings["vars"][i]["name"].asString()]->init(settings["vars"][i]); } } - - /* - - - for (auto s:settings["input"].Members) { - 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()); - } - } - */ - if (settings["id"]) id=settings["id"]; + if (settings["id"]!=nullptr) id=settings["id"].asString(); } std::string output_type(){return TypeName::Get();}; template Variable_type* create_inlet(std::string name){ -- cgit v1.2.3