[88] | 1 | #ifndef Morpheo_ToString_h |
---|
| 2 | #define Morpheo_ToString_h |
---|
[2] | 3 | |
---|
| 4 | /* |
---|
| 5 | * $Id: ToString.h 143 2010-09-02 14:33:08Z 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> |
---|
[143] | 21 | #include <stdint.h> |
---|
[2] | 22 | |
---|
| 23 | namespace morpheo { |
---|
| 24 | |
---|
[15] | 25 | template<typename T> inline std::string toString (const T& x) |
---|
[2] | 26 | { |
---|
[71] | 27 | std::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 | { |
---|
[71] | 34 | std::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; |
---|
[71] | 43 | std::ostringstream out(""); |
---|
| 44 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 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; |
---|
[71] | 51 | std::ostringstream out(""); |
---|
| 52 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 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; |
---|
[71] | 59 | std::ostringstream out(""); |
---|
| 60 | out << std::setprecision(sigdigits) << x; |
---|
[2] | 61 | return out.str(); |
---|
| 62 | } |
---|
[66] | 63 | |
---|
[78] | 64 | template<> inline std::string toString< int8_t> (const int8_t& x) |
---|
| 65 | { |
---|
| 66 | std::ostringstream out(""); |
---|
| 67 | out << static_cast< int32_t>(x); |
---|
| 68 | return out.str(); |
---|
| 69 | } |
---|
[66] | 70 | |
---|
[78] | 71 | template<> inline std::string toString<uint8_t> (const uint8_t& x) |
---|
| 72 | { |
---|
| 73 | std::ostringstream out(""); |
---|
| 74 | out << static_cast<uint32_t>(x); |
---|
| 75 | return out.str(); |
---|
| 76 | } |
---|
[66] | 77 | |
---|
[78] | 78 | template<> inline std::string toString< int16_t> (const int16_t& x) |
---|
| 79 | { |
---|
| 80 | std::ostringstream out(""); |
---|
| 81 | out << static_cast< int32_t>(x); |
---|
| 82 | return out.str(); |
---|
| 83 | } |
---|
[66] | 84 | |
---|
[78] | 85 | template<> inline std::string toString<uint16_t> (const uint16_t& x) |
---|
| 86 | { |
---|
| 87 | std::ostringstream out(""); |
---|
| 88 | out << static_cast<uint32_t>(x); |
---|
| 89 | return out.str(); |
---|
| 90 | } |
---|
[66] | 91 | |
---|
[78] | 92 | template<> inline std::string toString< int32_t> (const int32_t& x) |
---|
| 93 | { |
---|
| 94 | std::ostringstream out(""); |
---|
| 95 | out << x; |
---|
| 96 | return out.str(); |
---|
| 97 | } |
---|
[66] | 98 | |
---|
[78] | 99 | template<> inline std::string toString<uint32_t> (const uint32_t& x) |
---|
| 100 | { |
---|
| 101 | std::ostringstream out(""); |
---|
| 102 | out << x; |
---|
| 103 | return out.str(); |
---|
| 104 | } |
---|
[82] | 105 | |
---|
| 106 | std::string toString (const char *fmt, ...); |
---|
[2] | 107 | |
---|
| 108 | }; // end namespace morpheo |
---|
| 109 | |
---|
| 110 | #endif |
---|