From 1ff03ea63d79549db86422837f47916a42f23eb9 Mon Sep 17 00:00:00 2001 From: Comment Date: Sun, 8 Sep 2013 13:11:02 +0100 Subject: refactoring utils --- rotord/src/utils.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'rotord/src/utils.h') diff --git a/rotord/src/utils.h b/rotord/src/utils.h index 3859afe..d9df488 100644 --- a/rotord/src/utils.h +++ b/rotord/src/utils.h @@ -1,4 +1,13 @@ +#ifndef ROTOR_utils_H +#define ROTOR_utils_H + #include +#include +#include +#include +#include + +#include "Poco/String.h" #define FLOAT_THRESHOLD .001f @@ -8,3 +17,80 @@ bool fless_or_equal(const float u,const float v); bool fgreater_or_equal(const float u,const float v); bool fless(const float u,const float v); bool fgreater(const float u,const float v); + + +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); +float hexToFloat(const std::string& floatHexString); +std::string hexToString(const std::string& stringHexString); + +int toInt(const std::string& intString); +char toChar(const std::string& charString); +float toFloat(const std::string& floatString); +bool toBool(const std::string& boolString); + +#endif -- cgit v1.2.3