diff options
| author | Tim Redfern <tim@eclectronics.org> | 2014-01-03 19:57:48 +0000 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2014-01-03 19:57:48 +0000 |
| commit | 815d1149f9fb6be2c1bc05fb04f574eb928e050e (patch) | |
| tree | 647ac891d00ab5561926a6566d37c70346df634a /NT | |
| parent | 820b73f2abdf51bd59cb5b74739fb6f264b7c54e (diff) | |
updated refactored files
Diffstat (limited to 'NT')
| -rw-r--r-- | NT/src/factory.cpp | 11 | ||||
| -rw-r--r-- | NT/src/factory.h | 47 | ||||
| -rw-r--r-- | NT/src/nodes.cpp | 0 | ||||
| -rw-r--r-- | NT/src/utils.cpp | 140 |
4 files changed, 198 insertions, 0 deletions
diff --git a/NT/src/factory.cpp b/NT/src/factory.cpp new file mode 100644 index 0000000..6888a19 --- /dev/null +++ b/NT/src/factory.cpp @@ -0,0 +1,11 @@ +#include "factory.h" + +using namespace Rotor; + +Node_factory::Node_factory(){ + //for now, statically load prototype map in constructor + + add_type("time",new Time(),"nodes"); + add_type("multiply",new Multiply(),"nodes"); + add_type("print",new Print(),"nodes"); +}
\ No newline at end of file diff --git a/NT/src/factory.h b/NT/src/factory.h new file mode 100644 index 0000000..ac204a0 --- /dev/null +++ b/NT/src/factory.h @@ -0,0 +1,47 @@ +#ifndef ROTOR_FACTORY_H +#define ROTOR_FACTORY_H + +#include "rotor.h" +#include "nodes.h" +#include "xmlIO.h" + +namespace Rotor { + class Node_factory{ + public: + 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::string category){ + add_type(type,proto); + 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; + }; + 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: + std::map<std::string,Node*> type_map; + std::map<std::string,std::vector<Rotor::Node*> > category_map; + }; +} + +#endif //ROTOR_H + diff --git a/NT/src/nodes.cpp b/NT/src/nodes.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/NT/src/nodes.cpp diff --git a/NT/src/utils.cpp b/NT/src/utils.cpp new file mode 100644 index 0000000..a3f0f8a --- /dev/null +++ b/NT/src/utils.cpp @@ -0,0 +1,140 @@ +#include "utils.h" + +using namespace std; + +//double equality +bool fequal(const double u,const double v){ + if (abs(u-v)<FLOAT_THRESHOLD) return true; + else return false; +}; +bool fless_or_equal(const double u,const double v){ + //v is less or equal to u + if (u-v>-FLOAT_THRESHOLD) return true; + else return false; +}; +bool fgreater_or_equal(const double u,const double v){ + //v is more or equal to u + if (v-u>-FLOAT_THRESHOLD) return true; + else return false; +}; +bool fless(const double u,const double v){ + //v is less than u + if (u-v>FLOAT_THRESHOLD) return true; + else return false; +}; +bool fgreater(const double u,const double v){ + //v is greater than u + if (v-u>FLOAT_THRESHOLD) return true; + else return false; +}; + +//----------------------------------------with thanks to openframeworks +template <> +string toHex(const string& value) { + ostringstream out; + // how many bytes are in the string + int numBytes = value.size(); + for(int i = 0; i < numBytes; i++) { + // print each byte as a 2-character wide hex value + out << setfill('0') << setw(2) << hex << (unsigned int) ((unsigned char)value[i]); + } + return out.str(); +} + +//---------------------------------------- +string toHex(const char* value) { + // this function is necessary if you want to print a string + // using a syntax like toHex("test") + return toHex((string) value); +} + +//---------------------------------------- +int toInt(const string& intString) { + int x = 0; + istringstream cur(intString); + cur >> x; + return x; +} + +//---------------------------------------- +int hexToInt(const string& intHexString) { + int x = 0; + istringstream cur(intHexString); + cur >> hex >> x; + return x; +} + +//---------------------------------------- +char hexToChar(const string& charHexString) { + int x = 0; + istringstream cur(charHexString); + cur >> hex >> x; + return (char) x; +} + +//---------------------------------------- +double hexToFloat(const string& doubleHexString) { + union intFloatUnion { + int x; + double f; + } myUnion; + myUnion.x = 0; + istringstream cur(doubleHexString); + cur >> hex >> myUnion.x; + return myUnion.f; +} + +//---------------------------------------- +string hexToString(const string& stringHexString) { + stringstream out; + stringstream stream(stringHexString); + // a hex string has two characters per byte + int numBytes = stringHexString.size() / 2; + for(int i = 0; i < numBytes; i++) { + string curByte; + // grab two characters from the hex string + stream >> setw(2) >> curByte; + // prepare to parse the two characters + stringstream curByteStream(curByte); + int cur = 0; + // parse the two characters as a hex-encoded int + curByteStream >> hex >> cur; + // add the int as a char to our output stream + out << (char) cur; + } + return out.str(); +} + +//---------------------------------------- +double toFloat(const string& doubleString) { + double x = 0; + istringstream cur(doubleString); + cur >> x; + return x; +} + +//---------------------------------------- +bool toBool(const string& boolString) { + static const string trueString = "true"; + static const string falseString = "false"; + string lower = Poco::toLower(boolString); + if(lower == trueString) { + return true; + } + if(lower == falseString) { + return false; + } + bool x = false; + istringstream cur(lower); + cur >> x; + return x; +} + +//---------------------------------------- +char toChar(const string& charString) { + char x = '\0'; + istringstream cur(charString); + cur >> x; + return x; +} + |
