1 | #ifndef morpheo_tostring |
---|
2 | #define morpheo_tostring |
---|
3 | |
---|
4 | /* |
---|
5 | * $Id$ |
---|
6 | * |
---|
7 | * with a stephane dubuisson initial idea |
---|
8 | * |
---|
9 | * [ Description ] |
---|
10 | * |
---|
11 | */ |
---|
12 | |
---|
13 | #include <iosfwd> |
---|
14 | #include <sstream> |
---|
15 | #include <iomanip> |
---|
16 | #include <string> |
---|
17 | #include <limits> |
---|
18 | |
---|
19 | namespace morpheo { |
---|
20 | |
---|
21 | template<typename T> inline std::string toString (const T& x) |
---|
22 | { |
---|
23 | std::ostringstream out(""); |
---|
24 | out << x; |
---|
25 | return out.str(); |
---|
26 | } |
---|
27 | |
---|
28 | template<> inline std::string toString<bool> (const bool& x) |
---|
29 | { |
---|
30 | std::ostringstream out(""); |
---|
31 | //out << boolalpha << x; |
---|
32 | out << x; |
---|
33 | return out.str(); |
---|
34 | } |
---|
35 | |
---|
36 | template<> inline std::string toString<float> (const float& x) |
---|
37 | { |
---|
38 | const int sigdigits = std::numeric_limits<float>::digits10; |
---|
39 | std::ostringstream out(""); |
---|
40 | out << std::setprecision(sigdigits) << x; |
---|
41 | return out.str(); |
---|
42 | } |
---|
43 | |
---|
44 | template<> inline std::string toString<double> (const double& x) |
---|
45 | { |
---|
46 | const int sigdigits = std::numeric_limits<double>::digits10; |
---|
47 | std::ostringstream out(""); |
---|
48 | out << std::setprecision(sigdigits) << x; |
---|
49 | return out.str(); |
---|
50 | } |
---|
51 | |
---|
52 | template<> inline std::string toString<long double>(const long double& x) |
---|
53 | { |
---|
54 | const int sigdigits = std::numeric_limits<long double>::digits10; |
---|
55 | std::ostringstream out(""); |
---|
56 | out << std::setprecision(sigdigits) << x; |
---|
57 | return out.str(); |
---|
58 | } |
---|
59 | |
---|
60 | template<> inline std::string toString< int8_t> (const int8_t& x) |
---|
61 | { |
---|
62 | std::ostringstream out(""); |
---|
63 | out << static_cast< int32_t>(x); |
---|
64 | return out.str(); |
---|
65 | } |
---|
66 | |
---|
67 | template<> inline std::string toString<uint8_t> (const uint8_t& x) |
---|
68 | { |
---|
69 | std::ostringstream out(""); |
---|
70 | out << static_cast<uint32_t>(x); |
---|
71 | return out.str(); |
---|
72 | } |
---|
73 | |
---|
74 | template<> inline std::string toString< int16_t> (const int16_t& x) |
---|
75 | { |
---|
76 | std::ostringstream out(""); |
---|
77 | out << static_cast< int32_t>(x); |
---|
78 | return out.str(); |
---|
79 | } |
---|
80 | |
---|
81 | template<> inline std::string toString<uint16_t> (const uint16_t& x) |
---|
82 | { |
---|
83 | std::ostringstream out(""); |
---|
84 | out << static_cast<uint32_t>(x); |
---|
85 | return out.str(); |
---|
86 | } |
---|
87 | |
---|
88 | template<> inline std::string toString< int32_t> (const int32_t& x) |
---|
89 | { |
---|
90 | std::ostringstream out(""); |
---|
91 | out << x; |
---|
92 | return out.str(); |
---|
93 | } |
---|
94 | |
---|
95 | template<> inline std::string toString<uint32_t> (const uint32_t& x) |
---|
96 | { |
---|
97 | std::ostringstream out(""); |
---|
98 | out << x; |
---|
99 | return out.str(); |
---|
100 | } |
---|
101 | |
---|
102 | }; // end namespace morpheo |
---|
103 | |
---|
104 | #endif |
---|