From d9abdcbce9f0c3c7dbfebc00827e05536cb196e4 Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Mon, 30 Dec 2013 08:55:15 +0000 Subject: adding node factory --- NT/src/utils.h | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 NT/src/utils.h (limited to 'NT/src/utils.h') diff --git a/NT/src/utils.h b/NT/src/utils.h new file mode 100644 index 0000000..61e2c42 --- /dev/null +++ b/NT/src/utils.h @@ -0,0 +1,97 @@ +#ifndef ROTOR_utils_H +#define ROTOR_utils_H + +#include +#include +#include +#include +#include + +#include "Poco/String.h" + +#define FLOAT_THRESHOLD .001f + +//double equality +bool fequal(const double u,const double v); +bool fless_or_equal(const double u,const double v); +bool fgreater_or_equal(const double u,const double v); +bool fless(const double u,const double v); +bool fgreater(const double u,const double v); + +//----------------------------------------with thanks to openframeworks + +template +std::string toString(const T& value){ + std::ostringstream out; + out << value; + return out.str(); +} + +/// like sprintf "%4f" format, in this example precision=4 +template +std::string toString(const T& value, int precision){ + std::ostringstream out; + out << std::fixed << std::setprecision(precision) << value; + return out.str(); +} + +/// like sprintf "% 4d" or "% 4f" format, in this example width=4, fill=' ' +template +std::string toString(const T& value, int width, char fill ){ + std::ostringstream out; + out << std::fixed << std::setfill(fill) << std::setw(width) << value; + return out.str(); +} + +/// like sprintf "%04.2d" or "%04.2f" format, in this example precision=2, width=4, fill='0' +template +std::string toString(const T& value, int precision, int width, char fill ){ + std::ostringstream out; + out << std::fixed << std::setfill(fill) << std::setw(width) << std::setprecision(precision) << value; + return out.str(); +} + +template +std::string toString(const std::vector& values) { + std::stringstream out; + int n = values.size(); + out << "{"; + if(n > 0) { + for(int i = 0; i < n - 1; i++) { + out << values[i] << ", "; + } + out << values[n - 1]; + } + out << "}"; + return out.str(); +} + +template +std::string toHex(const T& value) { + std::ostringstream out; + // pretend that the value is a bunch of bytes + unsigned char* valuePtr = (unsigned char*) &value; + // the number of bytes is determined by the datatype + int numBytes = sizeof(T); + // the bytes are stored backwards (least significant first) + for(int i = numBytes - 1; i >= 0; i--) { + // print each byte out as a 2-character wide hex value + out << std::setfill('0') << std::setw(2) << std::hex << (int) valuePtr[i]; + } + return out.str(); +} +template <> +std::string toHex(const std::string& value); +std::string toHex(const char* value); + +int hexToInt(const std::string& intHexstring); +char hexToChar(const std::string& charHexString); +double hexToFloat(const std::string& doubleHexString); +std::string hexToString(const std::string& stringHexString); + +int toInt(const std::string& intString); +char toChar(const std::string& charString); +double toFloat(const std::string& doubleString); +bool toBool(const std::string& boolString); + +#endif -- cgit v1.2.3