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