| 1 | #ifndef Morpheo_ToString_h |
|---|
| 2 | #define Morpheo_ToString_h |
|---|
| 3 | |
|---|
| 4 | /* |
|---|
| 5 | * $Id: ToString.h 143 2010-09-02 14:33:08Z 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 | #include <stdint.h> |
|---|
| 22 | |
|---|
| 23 | namespace morpheo { |
|---|
| 24 | |
|---|
| 25 | template<typename T> inline std::string toString (const T& x) |
|---|
| 26 | { |
|---|
| 27 | std::ostringstream out(""); |
|---|
| 28 | out << x; |
|---|
| 29 | return out.str(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | template<> inline std::string toString<bool> (const bool& x) |
|---|
| 33 | { |
|---|
| 34 | std::ostringstream out(""); |
|---|
| 35 | //out << boolalpha << x; |
|---|
| 36 | out << x; |
|---|
| 37 | return out.str(); |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | template<> inline std::string toString<float> (const float& x) |
|---|
| 41 | { |
|---|
| 42 | const int sigdigits = std::numeric_limits<float>::digits10; |
|---|
| 43 | std::ostringstream out(""); |
|---|
| 44 | out << std::setprecision(sigdigits) << x; |
|---|
| 45 | return out.str(); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | template<> inline std::string toString<double> (const double& x) |
|---|
| 49 | { |
|---|
| 50 | const int sigdigits = std::numeric_limits<double>::digits10; |
|---|
| 51 | std::ostringstream out(""); |
|---|
| 52 | out << std::setprecision(sigdigits) << x; |
|---|
| 53 | return out.str(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | template<> inline std::string toString<long double>(const long double& x) |
|---|
| 57 | { |
|---|
| 58 | const int sigdigits = std::numeric_limits<long double>::digits10; |
|---|
| 59 | std::ostringstream out(""); |
|---|
| 60 | out << std::setprecision(sigdigits) << x; |
|---|
| 61 | return out.str(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 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 | } |
|---|
| 70 | |
|---|
| 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 | } |
|---|
| 77 | |
|---|
| 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 | } |
|---|
| 84 | |
|---|
| 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 | } |
|---|
| 91 | |
|---|
| 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 | } |
|---|
| 98 | |
|---|
| 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 | } |
|---|
| 105 | |
|---|
| 106 | std::string toString (const char *fmt, ...); |
|---|
| 107 | |
|---|
| 108 | }; // end namespace morpheo |
|---|
| 109 | |
|---|
| 110 | #endif |
|---|