1 #ifndef MSP_STRINGS_LEXICALCAST_H_
2 #define MSP_STRINGS_LEXICALCAST_H_
7 #include <msp/core/meta.h>
13 Thrown for errors in lexical conversions
15 class lexical_error: public std::runtime_error
18 lexical_error(const std::string &w): runtime_error(w) { }
19 virtual ~lexical_error() throw() { }
23 class format_mismatch: public lexical_error
26 format_mismatch(const std::string &w): lexical_error(w) { }
27 virtual ~format_mismatch() throw() { }
32 Helper class for lexical_cast to facilitate operator overloading.
34 class LexicalConverter
41 LexicalConverter(const Fmt &f): fmt(f) { }
42 LexicalConverter(const std::string &s, const Fmt &f): fmt(f), buf(s) { }
44 const Fmt &get_fmt() const { return fmt; }
45 const std::string &get() const { return buf; }
46 void result(const std::string &);
50 void operator<<(LexicalConverter &, char);
51 void operator<<(LexicalConverter &, signed char);
52 void operator<<(LexicalConverter &, short);
53 void operator<<(LexicalConverter &, int);
54 void operator<<(LexicalConverter &, long);
55 void operator<<(LexicalConverter &, unsigned char);
56 void operator<<(LexicalConverter &, unsigned short);
57 void operator<<(LexicalConverter &, unsigned);
58 void operator<<(LexicalConverter &, unsigned long);
60 void operator<<(LexicalConverter &, long long);
61 void operator<<(LexicalConverter &, unsigned long long);
63 void operator<<(LexicalConverter &, bool);
64 void operator<<(LexicalConverter &, float);
65 void operator<<(LexicalConverter &, double);
66 void operator<<(LexicalConverter &, long double);
67 void operator<<(LexicalConverter &, const std::string &);
68 void operator<<(LexicalConverter &, const char *);
69 void operator<<(LexicalConverter &, const void *);
71 void operator>>(const LexicalConverter &, char &);
72 void operator>>(const LexicalConverter &, signed char &);
73 void operator>>(const LexicalConverter &, short &);
74 void operator>>(const LexicalConverter &, int &);
75 void operator>>(const LexicalConverter &, long &);
76 void operator>>(const LexicalConverter &, unsigned char &);
77 void operator>>(const LexicalConverter &, unsigned short &);
78 void operator>>(const LexicalConverter &, unsigned int &);
79 void operator>>(const LexicalConverter &, unsigned long &);
81 void operator>>(const LexicalConverter &, long long &);
82 void operator>>(const LexicalConverter &, unsigned long long &);
84 void operator>>(const LexicalConverter &, bool &);
85 void operator>>(const LexicalConverter &, float &);
86 void operator>>(const LexicalConverter &, double &);
87 void operator>>(const LexicalConverter &, long double &);
88 void operator>>(const LexicalConverter &, std::string &);
90 // Generic operators using stringstream
93 struct HasFormattedOutput: Sfinae
95 static std::ostream &s;
98 /* The expression must depend on the template parameter, or the compiler
99 will give an error. */
101 static Yes f(int (*)[sizeof(s<<HasFormattedOutput<U>::v)]);
105 enum { value = Evaluate<sizeof(f<T>(0))>::value };
109 struct HasFormattedInput: Sfinae
111 static std::istream &s;
115 static Yes f(int (*)[sizeof(s>>HasFormattedOutput<U>::v)]);
119 enum { value = Evaluate<sizeof(f<T>(0))>::value };
124 typename EnableIf<HasFormattedOutput<T>::value, void>::Yes
125 operator<<(LexicalConverter &c, const T &v)
127 std::ostringstream ss;
133 typename EnableIf<HasFormattedInput<T>::value, void>::Yes
134 operator>>(const LexicalConverter &c, T &v)
136 std::istringstream ss(c.get());
137 ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws);
139 if(ss.fail() || !ss.eof())
140 throw lexical_error("conversion failure");
143 // Helper struct to provide partial template specialization
145 template<typename T, typename F>
149 struct LexicalCast<T, std::string>
151 static T cast(const std::string &s, const Fmt &f = Fmt())
153 LexicalConverter conv(s, f);
161 struct LexicalCast<std::string, F>
163 static std::string cast(const F &v, const Fmt &f = Fmt())
165 LexicalConverter conv(f);
172 struct LexicalCast<std::string, std::string>
174 static std::string cast(const std::string &v, const Fmt &f = Fmt())
176 LexicalConverter conv(f);
182 // The main interface to the lexical conversion machinery
184 template<typename T, typename F>
185 inline T lexical_cast(const F &v, const Fmt &f = Fmt())
187 return LexicalCast<T, F>::cast(v, f);