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