summaryrefslogtreecommitdiff
path: root/rotord/src/utils.h
blob: a36088b732be6f24beea5f4f1a171c0eb33194a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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

//float equality
bool fequal(const float u,const float v);
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);

//----------------------------------------with thanks to openframeworks

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