-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToString.hpp
More file actions
75 lines (65 loc) · 1.65 KB
/
ToString.hpp
File metadata and controls
75 lines (65 loc) · 1.65 KB
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
#if !defined(ELUCIDATE__UTIL__TOSTRING_HPP)
#define ELUCIDATE__UTIL__TOSTRING_HPP
#include <ostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <variant>
#include <iomanip>
#include <climits>
#include <boost/variant/recursive_wrapper.hpp>
namespace Elucidate::Util {
template< typename T >
std::string toString(T const& t) {
std::stringstream s;
s << t;
return s.str();
}
template< typename T >
std::string toString(boost::recursive_wrapper<T> const& t) {
return toString(t.get());
}
template< typename... Members >
std::string toString(std::variant< Members... > const& v) {
return std::visit([](auto& t) { return toString(t); }, v);
}
template< typename T, typename U >
std::string toString(std::pair< T, U > const& p) {
std::stringstream s;
s << p.first;
s << ":";
s << p.second;
return s.str();
}
template< typename T >
std::string dumpHex(T t) {
constexpr std::size_t NUMBITS_PERHEX = 4;
std::stringstream out;
// prefer hardcoded 0x to std::showbase to prevent 0 showing without 0x
out << std::hex << std::internal << std::showbase
<< std::setfill('0') << std::setw(sizeof(T) * CHAR_BIT / NUMBITS_PERHEX)
<< t;
return out.str();
}
template< typename T >
std::string printEach(T const& t, const char *separator) {
std::stringstream s;
bool first = true;
for (auto const& x : t) {
if (!first) {
s << separator;
}
s << x;
first = false;
}
return s.str();
}
template<>
std::string printEach< std::vector< char* > >(
std::vector< char* > const& t, const char *separator);
template<>
std::string printEach< std::pair< int, char** > >(
std::pair< int, char** > const& t, const char *separator);
}
#endif