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
42 LexicalConverter(const Fmt &f): fmt(f), filled(false) { }
43 LexicalConverter(const std::string &s, const Fmt &f): fmt(f), filled(true), buf(s) { }
45 const Fmt &get_fmt() const { return fmt; }
46 const std::string &get() const;
47 void result(const std::string &);
51 void operator<<(LexicalConverter &, char);
52 void operator<<(LexicalConverter &, signed char);
53 void operator<<(LexicalConverter &, short);
54 void operator<<(LexicalConverter &, int);
55 void operator<<(LexicalConverter &, long);
56 void operator<<(LexicalConverter &, unsigned char);
57 void operator<<(LexicalConverter &, unsigned short);
58 void operator<<(LexicalConverter &, unsigned);
59 void operator<<(LexicalConverter &, unsigned long);
61 void operator<<(LexicalConverter &, long long);
62 void operator<<(LexicalConverter &, unsigned long long);
64 void operator<<(LexicalConverter &, bool);
65 void operator<<(LexicalConverter &, float);
66 void operator<<(LexicalConverter &, double);
67 void operator<<(LexicalConverter &, long double);
68 void operator<<(LexicalConverter &, const std::string &);
69 void operator<<(LexicalConverter &, const char *);
70 void operator<<(LexicalConverter &, const void *);
72 void operator>>(const LexicalConverter &, char &);
73 void operator>>(const LexicalConverter &, signed char &);
74 void operator>>(const LexicalConverter &, short &);
75 void operator>>(const LexicalConverter &, int &);
76 void operator>>(const LexicalConverter &, long &);
77 void operator>>(const LexicalConverter &, unsigned char &);
78 void operator>>(const LexicalConverter &, unsigned short &);
79 void operator>>(const LexicalConverter &, unsigned int &);
80 void operator>>(const LexicalConverter &, unsigned long &);
82 void operator>>(const LexicalConverter &, long long &);
83 void operator>>(const LexicalConverter &, unsigned long long &);
85 void operator>>(const LexicalConverter &, bool &);
86 void operator>>(const LexicalConverter &, float &);
87 void operator>>(const LexicalConverter &, double &);
88 void operator>>(const LexicalConverter &, long double &);
89 void operator>>(const LexicalConverter &, std::string &);
91 // Generic operators using stringstream
94 struct HasFormattedOutput: Sfinae
96 static std::ostream &s;
99 /* The expression must depend on the template parameter, or the compiler
100 will give an error. */
102 static Yes f(int (*)[sizeof(s<<HasFormattedOutput<U>::v)]);
106 enum { value = Evaluate<sizeof(f<T>(0))>::value };
110 struct HasFormattedInput: Sfinae
112 static std::istream &s;
116 static Yes f(int (*)[sizeof(s>>HasFormattedOutput<U>::v)]);
120 enum { value = Evaluate<sizeof(f<T>(0))>::value };
125 typename EnableIf<HasFormattedOutput<T>::value, void>::Yes
126 operator<<(LexicalConverter &c, const T &v)
128 std::ostringstream ss;
134 typename EnableIf<HasFormattedInput<T>::value, void>::Yes
135 operator>>(const LexicalConverter &c, T &v)
137 std::istringstream ss(c.get());
138 ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws);
140 if(ss.fail() || !ss.eof())
141 throw lexical_error("conversion failure");
144 // Helper struct to provide partial template specialization
146 template<typename T, typename F>
150 struct LexicalCast<T, std::string>
152 static T cast(const std::string &s, const Fmt &f = Fmt())
154 LexicalConverter conv(s, f);
162 struct LexicalCast<std::string, F>
164 static std::string cast(const F &v, const Fmt &f = Fmt())
166 LexicalConverter conv(f);
173 struct LexicalCast<std::string, std::string>
175 static std::string cast(const std::string &v, const Fmt &f = Fmt())
177 LexicalConverter conv(f);
183 // The main interface to the lexical conversion machinery
185 template<typename T, typename F>
186 inline T lexical_cast(const F &v, const Fmt &f = Fmt())
188 return LexicalCast<T, F>::cast(v, f);