diff options
| author | Comment <tim@gray.(none)> | 2013-09-08 13:11:02 +0100 |
|---|---|---|
| committer | Comment <tim@gray.(none)> | 2013-09-08 13:11:02 +0100 |
| commit | 1ff03ea63d79549db86422837f47916a42f23eb9 (patch) | |
| tree | 1631546f60cb830749d6fe64d8af0911d9afcfe5 /rotord/src/utils.h | |
| parent | 512a2738e9f2fc74f109dabada357cc20c2a3412 (diff) | |
refactoring utils
Diffstat (limited to 'rotord/src/utils.h')
| -rw-r--r-- | rotord/src/utils.h | 86 |
1 files changed, 86 insertions, 0 deletions
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 <cmath> +#include <string> +#include <sstream> +#include <vector> +#include <iomanip> + +#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 <class T> +std::string toString(const T& value){ + std::ostringstream out; + out << value; + return out.str(); +} + +/// like sprintf "%4f" format, in this example precision=4 +template <class T> +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 <class T> +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 <class T> +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<class T> +std::string toString(const std::vector<T>& 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 <class T> +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 |
