summaryrefslogtreecommitdiff
path: root/NT
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2014-01-19 19:54:38 +0000
committerTim Redfern <tim@eclectronics.org>2014-01-19 19:54:38 +0000
commitce528aa299501ecab63018f91b937766ffbc95be (patch)
tree34972d49d69c5cf926a3974a020e945ff4576a8c /NT
parent1e01ca3e89beca6870a3112c4513df66dd07dbb1 (diff)
variables writing json
Diffstat (limited to 'NT')
-rw-r--r--NT/src/factory.cpp99
-rw-r--r--NT/src/factory.h19
-rw-r--r--NT/src/nodes.h18
-rw-r--r--NT/src/rotor.cpp34
-rw-r--r--NT/src/rotor.h43
5 files changed, 54 insertions, 159 deletions
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<Image_node*> (type)!=nullptr) {
- for (auto& input: (dynamic_cast<Image_node*>(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<Node*>();
category_map[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;
- };
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<std::string,Node*> type_map;
std::map<std::string,std::vector<Rotor::Node*> > 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<string,string> &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<string,string> &_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<string,string> &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<string,string> &_settings) { return new Multiply(_settings);};
+ Multiply* clone(Json::Value &_settings) { return new Multiply(_settings);};
private:
Variable_array_type<double> *factors;
double result;
@@ -57,7 +57,7 @@ namespace Rotor{
inlet=create_inlet<double>("inlet");
type="print";
}
- Print(map<string,string> &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<string,string> &_settings) { return new Print(_settings);};
+ Print* clone(Json::Value &_settings) { return new Print(_settings);};
private:
Variable_type<double> *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 <class T>
+Json::Value Variable_type<T>::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 <class T>
+Json::Value Variable_array_type<T>::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<Variable_array*>(var.second)){
newvar["input"]=Json::arrayValue;
- for (auto& element: dynamic_cast<Variable_array*>(var.second)->values) {
- Json::Value newvalue;
- newvar["input"].append(newvalue);
+ for (auto& input: dynamic_cast<Variable_array*>(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 T> 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<T>::Get();
}
@@ -128,15 +125,12 @@ namespace Rotor {
template <class T> 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<T>::Get();
}
@@ -193,8 +187,8 @@ namespace Rotor {
}
return false;
}
- Json::Value list_json();
- virtual Node* clone(std::map<std::string,std::string> &_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 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());
- }
- }
- }
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<NT>::Get();};
template <class IT> Variable_type<IT>* create_inlet(std::string name){