1 #ifndef MSP_STRINGS_LEXICALCAST_H_
2 #define MSP_STRINGS_LEXICALCAST_H_
7 #include <msp/core/meta.h>
8 #include <msp/core/mspcore_api.h>
14 Thrown for errors in lexical conversions.
16 class MSPCORE_API lexical_error: public std::runtime_error
19 lexical_error(const std::string &w): runtime_error(w) { }
24 Thrown when the format is unsuitable for the type being converted.
26 class MSPCORE_API format_mismatch: public lexical_error
29 format_mismatch(const std::string &w): lexical_error(w) { }
34 Helper class for lexical_cast to facilitate operator overloading.
36 class MSPCORE_API LexicalConverter
44 LexicalConverter(const Fmt &f): fmt(f) { }
45 LexicalConverter(const std::string &s, const Fmt &f): fmt(f), filled(true), buf(s) { }
47 const Fmt &get_fmt() const { return fmt; }
48 const std::string &get() const;
49 void result(const std::string &);
53 MSPCORE_API void operator<<(LexicalConverter &, char);
54 MSPCORE_API void operator<<(LexicalConverter &, signed char);
55 MSPCORE_API void operator<<(LexicalConverter &, short);
56 MSPCORE_API void operator<<(LexicalConverter &, int);
57 MSPCORE_API void operator<<(LexicalConverter &, long);
58 MSPCORE_API void operator<<(LexicalConverter &, unsigned char);
59 MSPCORE_API void operator<<(LexicalConverter &, unsigned short);
60 MSPCORE_API void operator<<(LexicalConverter &, unsigned);
61 MSPCORE_API void operator<<(LexicalConverter &, unsigned long);
62 MSPCORE_API void operator<<(LexicalConverter &, long long);
63 MSPCORE_API void operator<<(LexicalConverter &, unsigned long long);
64 MSPCORE_API void operator<<(LexicalConverter &, bool);
65 MSPCORE_API void operator<<(LexicalConverter &, float);
66 MSPCORE_API void operator<<(LexicalConverter &, double);
67 MSPCORE_API void operator<<(LexicalConverter &, long double);
68 MSPCORE_API void operator<<(LexicalConverter &, const std::string &);
69 MSPCORE_API void operator<<(LexicalConverter &, const char *);
70 MSPCORE_API void operator<<(LexicalConverter &, const void *);
72 MSPCORE_API void operator>>(const LexicalConverter &, char &);
73 MSPCORE_API void operator>>(const LexicalConverter &, signed char &);
74 MSPCORE_API void operator>>(const LexicalConverter &, short &);
75 MSPCORE_API void operator>>(const LexicalConverter &, int &);
76 MSPCORE_API void operator>>(const LexicalConverter &, long &);
77 MSPCORE_API void operator>>(const LexicalConverter &, unsigned char &);
78 MSPCORE_API void operator>>(const LexicalConverter &, unsigned short &);
79 MSPCORE_API void operator>>(const LexicalConverter &, unsigned int &);
80 MSPCORE_API void operator>>(const LexicalConverter &, unsigned long &);
81 MSPCORE_API void operator>>(const LexicalConverter &, long long &);
82 MSPCORE_API void operator>>(const LexicalConverter &, unsigned long long &);
83 MSPCORE_API void operator>>(const LexicalConverter &, bool &);
84 MSPCORE_API void operator>>(const LexicalConverter &, float &);
85 MSPCORE_API void operator>>(const LexicalConverter &, double &);
86 MSPCORE_API void operator>>(const LexicalConverter &, long double &);
87 MSPCORE_API void operator>>(const LexicalConverter &, std::string &);
89 // Generic operators using stringstream
91 struct CheckFormattedOutput: Sfinae
93 static std::ostream &s;
95 static Yes f(int (*)[sizeof(s<<reinterpret_cast<const T &>(s))]);
99 struct CheckFormattedInput: Sfinae
101 static std::istream &s;
103 static Yes f(int (*)[sizeof(s>>reinterpret_cast<T &>(s))]);
107 template<typename T> struct HasFormattedOutput: Sfinae::Evaluate<CheckFormattedOutput, T> { };
108 template<typename T> struct HasFormattedInput: Sfinae::Evaluate<CheckFormattedInput, T> { };
112 typename std::enable_if<HasFormattedOutput<T>::value>::type
113 operator<<(LexicalConverter &c, const T &v)
115 std::ostringstream ss;
116 ss << c.get_fmt() << v;
121 typename std::enable_if<HasFormattedInput<T>::value>::type
122 operator>>(const LexicalConverter &c, T &v)
124 std::istringstream ss(c.get());
125 ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws);
127 if(ss.fail() || !ss.eof())
128 throw lexical_error("conversion failure");
132 Helper struct to provide partial template specialization.
134 template<typename T, typename F>
138 struct LexicalCast<T, std::string>
140 static T cast(const std::string &s, const Fmt &f = Fmt())
142 LexicalConverter conv(s, f);
150 struct LexicalCast<std::string, F>
152 static std::string cast(const F &v, const Fmt &f = Fmt())
154 LexicalConverter conv(f);
161 struct LexicalCast<std::string, std::string>
163 static std::string cast(const std::string &v, const Fmt &f = Fmt())
165 LexicalConverter conv(f);
171 /** Perform a lexical conversion between a string and another type. The source
172 type can normally be deduced by the compiler, so this can be used just like the
173 standard C++ casts. A format may additionally be specified to force a specific
175 template<typename T, typename F>
176 inline T lexical_cast(const F &v, const Fmt &f = Fmt())
178 return LexicalCast<T, F>::cast(v, f);