diff options
| author | Tim Redfern <tim@eclectronics.org> | 2014-01-13 16:49:20 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2014-01-13 16:49:20 +0000 |
| commit | 52710ced0e723117afa2d40f3b21e1fa2d1ad2ad (patch) | |
| tree | b0bee9a85b7dd4199924eb7c3fbbea46b5c6b416 /NT | |
| parent | a31d487dd9474567bc12a0af7c9031350f1e192a (diff) | |
TypeNames
Diffstat (limited to 'NT')
| -rw-r--r-- | NT/src/factory.cpp | 96 | ||||
| -rw-r--r-- | NT/src/factory.h | 1 | ||||
| -rw-r--r-- | NT/src/nodes.h | 4 | ||||
| -rw-r--r-- | NT/src/rotor.cpp | 94 | ||||
| -rw-r--r-- | NT/src/rotor.h | 46 |
5 files changed, 142 insertions, 99 deletions
diff --git a/NT/src/factory.cpp b/NT/src/factory.cpp index 3567f83..dd39533 100644 --- a/NT/src/factory.cpp +++ b/NT/src/factory.cpp @@ -22,110 +22,18 @@ bool Node_factory::list_node(const string &t,xmlIO XML){ bool Node_factory::list_node(const string &t,Json::Value &JSON){ for (auto& type: type_map) { if (type.first==t) { - JSON["node"]=list_node(type.second); + JSON["node"]=type.second->list_json(); return true; } } JSON["error"]="Node /"+t+"/ not found"; return false; }; -Json::Value Node_factory::list_node(Rotor::Node* _node){ - Json::Value node; - node["node_type"]=_node->node_type; - node["title"]=_node->title; - node["output_type"]=_node->output_type; - node["description"]=_node->description; - node["node_id"]=_node->node_id; - node["ui_type"]=_node->ui_type; - if (_node->vars.size()){ - node["vars"]=Json::arrayValue; - for (auto& var: _node->vars) { - string type=var.second->get_type(); - Json::Value newvar; - newvar["type"]=type; - 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); - } - } - else { - newvar["input"]=Json::Value(); - } - node["vars"].append(newvar); - } - } -/* - if (_node->inputs.size()){ - node["signal_inputs"]=Json::arrayValue; - for (auto& input: _node->inputs) { - Json::Value signal_input; - signal_input["title"]=input->title; - signal_input["description"]=input->description; - node["signal_inputs"].append(signal_input); - } - } - if (dynamic_cast<Image_node*> (_node)!=nullptr) { - if ((dynamic_cast<Image_node*>(_node))->image_inputs.size()){ - node["image_inputs"]=Json::arrayValue; - for (auto& input: (dynamic_cast<Image_node*>(_node))->image_inputs) { - Json::Value image_input; - image_input["title"]=input->title; - image_input["description"]=input->description; - node["image_inputs"].append(image_input); - } - } - } - if (_node->parameters.size()){ - node["parameters"]=Json::arrayValue; - for (auto& param: _node->parameters) { - Json::Value parameter; - parameter["name"]=param.first; - parameter["type"]=param.second->type; - parameter["title"]=param.second->title; - parameter["description"]=param.second->description; - parameter["value"]=param.second->value; - parameter["min"]=param.second->min; - parameter["max"]=param.second->max; - parameter["step"]=param.second->step; - node["parameters"].append(parameter); - } - } - if (_node->attributes.size()){ - node["attributes"]=Json::arrayValue; - for (auto& attr: _node->attributes) { - Json::Value attribute; - attribute["name"]=attr.first; - attribute["title"]=attr.second->title; - attribute["description"]=attr.second->description; - if (attr.second->vals.size()){ //document attribute enumeration - attribute["value"]=attr.second->value; - attribute["type"]="enum"; - attribute["options"]=Json::arrayValue; - for (auto val: attr.second->vals){ - attribute["options"].append(val); - } - } - else { - attribute["type"]=attr.second->type; - if (attr.second->type=="array"){ - attribute["value"]=Json::arrayValue; - } - else attribute["value"]=attr.second->value; - } - node["attributes"].append(attribute); - } - } -*/ - return node; -} 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","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); diff --git a/NT/src/factory.h b/NT/src/factory.h index 8c44507..88e166f 100644 --- a/NT/src/factory.h +++ b/NT/src/factory.h @@ -31,7 +31,6 @@ namespace Rotor { 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); diff --git a/NT/src/nodes.h b/NT/src/nodes.h index f294756..ddb1a3d 100644 --- a/NT/src/nodes.h +++ b/NT/src/nodes.h @@ -8,7 +8,7 @@ using namespace std; namespace Rotor{ class Double_node: public Node_type<double> { public: - Double_node(){output_type="double";}; + Double_node(){}; }; class Time: public Double_node { public: @@ -46,7 +46,7 @@ namespace Rotor{ }; class String_node: public Node_type<string> { public: - String_node(){output_type="string";}; + String_node(){}; }; class Print: public String_node { public: diff --git a/NT/src/rotor.cpp b/NT/src/rotor.cpp index c08734c..33d52ad 100644 --- a/NT/src/rotor.cpp +++ b/NT/src/rotor.cpp @@ -7,6 +7,100 @@ using namespace std; using namespace Rotor; +Json::Value Node::list_json(){ + Json::Value node; + node["node_type"]=node_type; + node["title"]=title; + node["output_type"]=output_type(); + node["description"]=description; + node["node_id"]=node_id; + node["ui_type"]=ui_type; + + if (vars.size()){ + node["vars"]=Json::arrayValue; + for (auto& var: vars) { + string type=var.second->get_type(); + Json::Value newvar; + newvar["type"]=type; + 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); + } + } + else { + newvar["input"]=Json::Value(); + } + node["vars"].append(newvar); + } + } +/* + if (_node->inputs.size()){ + node["signal_inputs"]=Json::arrayValue; + for (auto& input: _node->inputs) { + Json::Value signal_input; + signal_input["title"]=input->title; + signal_input["description"]=input->description; + node["signal_inputs"].append(signal_input); + } + } + if (dynamic_cast<Image_node*> (_node)!=nullptr) { + if ((dynamic_cast<Image_node*>(_node))->image_inputs.size()){ + node["image_inputs"]=Json::arrayValue; + for (auto& input: (dynamic_cast<Image_node*>(_node))->image_inputs) { + Json::Value image_input; + image_input["title"]=input->title; + image_input["description"]=input->description; + node["image_inputs"].append(image_input); + } + } + } + if (_node->parameters.size()){ + node["parameters"]=Json::arrayValue; + for (auto& param: _node->parameters) { + Json::Value parameter; + parameter["name"]=param.first; + parameter["type"]=param.second->type; + parameter["title"]=param.second->title; + parameter["description"]=param.second->description; + parameter["value"]=param.second->value; + parameter["min"]=param.second->min; + parameter["max"]=param.second->max; + parameter["step"]=param.second->step; + node["parameters"].append(parameter); + } + } + if (_node->attributes.size()){ + node["attributes"]=Json::arrayValue; + for (auto& attr: _node->attributes) { + Json::Value attribute; + attribute["name"]=attr.first; + attribute["title"]=attr.second->title; + attribute["description"]=attr.second->description; + if (attr.second->vals.size()){ //document attribute enumeration + attribute["value"]=attr.second->value; + attribute["type"]="enum"; + attribute["options"]=Json::arrayValue; + for (auto val: attr.second->vals){ + attribute["options"].append(val); + } + } + else { + attribute["type"]=attr.second->type; + if (attr.second->type=="array"){ + attribute["value"]=Json::arrayValue; + } + else attribute["value"]=attr.second->value; + } + node["attributes"].append(attribute); + } + } +*/ + return node; +} + //factory generates linker errors if rotor.h implementation is seperated: why? int main(){ diff --git a/NT/src/rotor.h b/NT/src/rotor.h index 0b728ac..d261954 100644 --- a/NT/src/rotor.h +++ b/NT/src/rotor.h @@ -1,6 +1,8 @@ #ifndef ROTOR_H #define ROTOR_H +#define ENABLE_TYPENAME(A) template<> struct TypeName<A> { static const char *Get() { return #A; }}; + #include <string> #include <sstream> #include <iostream> @@ -8,7 +10,29 @@ #include <vector> #include <unordered_map> #include <json/json.h> +#include <typeinfo> + +template <typename T> +struct TypeName +{ + static const char* Get() + { + return typeid(T).name(); + } +}; +template <> +struct TypeName<int> { + static const char* Get() { + return "int"; + } +}; +template <> +struct TypeName<double> { + static const char* Get() { + return "double"; + } +}; namespace Rotor { @@ -62,7 +86,7 @@ namespace Rotor { cur >> value; } std::string get_type(){ //need this to output node templates - return "unknown"; + return TypeName<T>::Get(); } bool connect(Node* target){ if (connectable){ @@ -89,6 +113,22 @@ namespace Rotor { template <class T> class Variable_array_type: public Variable_array { public: Variable_array_type(){}; + void init(std::string s){ + std::istringstream cur(s); + cur >> value; + } + std::string get_type(){ //need this to output node templates + return TypeName<T>::Get(); + } + bool connect(Node* target){ + if (connectable){ + if (dynamic_cast<Node_type<T>*>(target)){ + connection=target; + return true; + } + } + return false; + } void add(int num=1){ for (int i=0;i<num;i++) values.push_back(T()); } @@ -133,14 +173,15 @@ namespace Rotor { } return false; } + Json::Value list_json(); virtual Node* clone(std::map<std::string,std::string> &_settings)=0; + virtual std::string output_type()=0; std::string node_type; std::string node_id; std::string id; std::string description; std::string title; std::string ui_type; - std::string output_type; std::unordered_map<std::string,Variable*> vars; }; template <class NT> class Node_type : public Node { @@ -154,6 +195,7 @@ namespace Rotor { } } } + std::string output_type(){return TypeName<NT>::Get();}; 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])); |
