[15] | 1 | #ifndef morpheo_tostring |
---|
| 2 | #define morpheo_tostring |
---|
[2] | 3 | |
---|
| 4 | /* |
---|
| 5 | * $Id: ToString.h 82 2008-05-01 16:48:45Z rosiere $ |
---|
| 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> |
---|
[82] | 18 | #include <stdio.h> |
---|
| 19 | #include <stdlib.h> |
---|
| 20 | #include <stdarg.h> |
---|
[2] | 21 | |
---|
| 22 | namespace morpheo { |
---|
| 23 | |
---|
[15] | 24 | template<typename T> inline std::string toString (const T& x) |
---|
[2] | 25 | { |
---|
[71] | 26 | std::ostringstream out(""); |
---|
[2] | 27 | out << x; |
---|
| 28 | return out.str(); |
---|
| 29 | } |
---|
| 30 | |
---|
[15] | 31 | template<> inline std::string toString<bool> (const bool& x) |
---|
[2] | 32 | { |
---|
[71] | 33 | std::ostringstream out(""); |
---|
[2] | 34 | //out << boolalpha << x; |
---|
| 35 | out << x; |
---|
| 36 | return out.str(); |
---|
| 37 | } |
---|
| 38 | |
---|
[15] | 39 | template<> inline std::string toString<float> (const float& x) |
---|
[2] | 40 | { |
---|
| 41 | const int sigdigits = std::numeric_limits<float>::digits10; |
---|
[71] | 42 | std::ostringstream out(""); |
---|
| 43 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 44 | return out.str(); |
---|
| 45 | } |
---|
| 46 | |
---|
[15] | 47 | template<> inline std::string toString<double> (const double& x) |
---|
[2] | 48 | { |
---|
| 49 | const int sigdigits = std::numeric_limits<double>::digits10; |
---|
[71] | 50 | std::ostringstream out(""); |
---|
| 51 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 52 | return out.str(); |
---|
| 53 | } |
---|
| 54 | |
---|
[15] | 55 | template<> inline std::string toString<long double>(const long double& x) |
---|
[2] | 56 | { |
---|
| 57 | const int sigdigits = std::numeric_limits<long double>::digits10; |
---|
[71] | 58 | std::ostringstream out(""); |
---|
| 59 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 60 | return out.str(); |
---|
| 61 | } |
---|
[66] | 62 | |
---|
[78] | 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 | } |
---|
[66] | 69 | |
---|
[78] | 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 | } |
---|
[66] | 76 | |
---|
[78] | 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 | } |
---|
[66] | 83 | |
---|
[78] | 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 | } |
---|
[66] | 90 | |
---|
[78] | 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 | } |
---|
[66] | 97 | |
---|
[78] | 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 | } |
---|
[82] | 104 | |
---|
| 105 | std::string toString (const char *fmt, ...); |
---|
[2] | 106 | |
---|
| 107 | }; // end namespace morpheo |
---|
| 108 | |
---|
| 109 | #endif |
---|