1 | #ifndef Morpheo_ToString_h |
---|
2 | #define Morpheo_ToString_h |
---|
3 | |
---|
4 | /* |
---|
5 | * $Id: ToString.h 88 2008-12-10 18:31:39Z rosiere $ |
---|
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 | #include <stdio.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdarg.h> |
---|
21 | |
---|
22 | namespace morpheo { |
---|
23 | |
---|
24 | template<typename T> inline std::string toString (const T& x) |
---|
25 | { |
---|
26 | std::ostringstream out(""); |
---|
27 | out << x; |
---|
28 | return out.str(); |
---|
29 | } |
---|
30 | |
---|
31 | template<> inline std::string toString<bool> (const bool& x) |
---|
32 | { |
---|
33 | std::ostringstream out(""); |
---|
34 | //out << boolalpha << x; |
---|
35 | out << x; |
---|
36 | return out.str(); |
---|
37 | } |
---|
38 | |
---|
39 | template<> inline std::string toString<float> (const float& x) |
---|
40 | { |
---|
41 | const int sigdigits = std::numeric_limits<float>::digits10; |
---|
42 | std::ostringstream out(""); |
---|
43 | out << std::setprecision(sigdigits) << x; |
---|
44 | return out.str(); |
---|
45 | } |
---|
46 | |
---|
47 | template<> inline std::string toString<double> (const double& x) |
---|
48 | { |
---|
49 | const int sigdigits = std::numeric_limits<double>::digits10; |
---|
50 | std::ostringstream out(""); |
---|
51 | out << std::setprecision(sigdigits) << x; |
---|
52 | return out.str(); |
---|
53 | } |
---|
54 | |
---|
55 | template<> inline std::string toString<long double>(const long double& x) |
---|
56 | { |
---|
57 | const int sigdigits = std::numeric_limits<long double>::digits10; |
---|
58 | std::ostringstream out(""); |
---|
59 | out << std::setprecision(sigdigits) << x; |
---|
60 | return out.str(); |
---|
61 | } |
---|
62 | |
---|
63 | template<> inline std::string toString< int8_t> (const int8_t& x) |
---|
64 | { |
---|
65 | std::ostringstream out(""); |
---|
66 | out << static_cast< int32_t>(x); |
---|
67 | return out.str(); |
---|
68 | } |
---|
69 | |
---|
70 | template<> inline std::string toString<uint8_t> (const uint8_t& x) |
---|
71 | { |
---|
72 | std::ostringstream out(""); |
---|
73 | out << static_cast<uint32_t>(x); |
---|
74 | return out.str(); |
---|
75 | } |
---|
76 | |
---|
77 | template<> inline std::string toString< int16_t> (const int16_t& x) |
---|
78 | { |
---|
79 | std::ostringstream out(""); |
---|
80 | out << static_cast< int32_t>(x); |
---|
81 | return out.str(); |
---|
82 | } |
---|
83 | |
---|
84 | template<> inline std::string toString<uint16_t> (const uint16_t& x) |
---|
85 | { |
---|
86 | std::ostringstream out(""); |
---|
87 | out << static_cast<uint32_t>(x); |
---|
88 | return out.str(); |
---|
89 | } |
---|
90 | |
---|
91 | template<> inline std::string toString< int32_t> (const int32_t& x) |
---|
92 | { |
---|
93 | std::ostringstream out(""); |
---|
94 | out << x; |
---|
95 | return out.str(); |
---|
96 | } |
---|
97 | |
---|
98 | template<> inline std::string toString<uint32_t> (const uint32_t& x) |
---|
99 | { |
---|
100 | std::ostringstream out(""); |
---|
101 | out << x; |
---|
102 | return out.str(); |
---|
103 | } |
---|
104 | |
---|
105 | std::string toString (const char *fmt, ...); |
---|
106 | |
---|
107 | }; // end namespace morpheo |
---|
108 | |
---|
109 | #endif |
---|