1 #ifndef MSP_STRINGS_LEXICALCAST_H_
2 #define MSP_STRINGS_LEXICALCAST_H_
12 Thrown for errors in lexical conversions
14 class lexical_error: public std::runtime_error
17 lexical_error(const std::string &w): runtime_error(w) { }
18 virtual ~lexical_error() throw() { }
22 class format_mismatch: public lexical_error
25 format_mismatch(const std::string &w): lexical_error(w) { }
26 virtual ~format_mismatch() throw() { }
31 Helper class for lexical_cast to facilitate operator overloading.
33 class LexicalConverter
40 LexicalConverter(const Fmt &f): fmt(f) { }
41 LexicalConverter(const std::string &s, const Fmt &f): fmt(f), buf(s) { }
43 const Fmt &get_fmt() const { return fmt; }
44 const std::string &get() const { return buf; }
45 void result(const std::string &);
49 void operator<<(LexicalConverter &, char);
50 void operator<<(LexicalConverter &, signed char);
51 void operator<<(LexicalConverter &, short);
52 void operator<<(LexicalConverter &, int);
53 void operator<<(LexicalConverter &, long);
54 void operator<<(LexicalConverter &, unsigned char);
55 void operator<<(LexicalConverter &, unsigned short);
56 void operator<<(LexicalConverter &, unsigned);
57 void operator<<(LexicalConverter &, unsigned long);
59 void operator<<(LexicalConverter &, long long);
60 void operator<<(LexicalConverter &, unsigned long long);
62 void operator<<(LexicalConverter &, bool);
63 void operator<<(LexicalConverter &, float);
64 void operator<<(LexicalConverter &, double);
65 void operator<<(LexicalConverter &, long double);
66 void operator<<(LexicalConverter &, const std::string &);
67 void operator<<(LexicalConverter &, const char *);
68 void operator<<(LexicalConverter &, const void *);
70 void operator>>(const LexicalConverter &, char &);
71 void operator>>(const LexicalConverter &, signed char &);
72 void operator>>(const LexicalConverter &, short &);
73 void operator>>(const LexicalConverter &, int &);
74 void operator>>(const LexicalConverter &, long &);
75 void operator>>(const LexicalConverter &, unsigned char &);
76 void operator>>(const LexicalConverter &, unsigned short &);
77 void operator>>(const LexicalConverter &, unsigned int &);
78 void operator>>(const LexicalConverter &, unsigned long &);
80 void operator>>(const LexicalConverter &, long long &);
81 void operator>>(const LexicalConverter &, unsigned long long &);
83 void operator>>(const LexicalConverter &, bool &);
84 void operator>>(const LexicalConverter &, float &);
85 void operator>>(const LexicalConverter &, double &);
86 void operator>>(const LexicalConverter &, long double &);
87 void operator>>(const LexicalConverter &, std::string &);
89 // Generic operators using stringstream
92 void operator<<(LexicalConverter &c, const T &v)
94 std::ostringstream ss;
100 void operator>>(const LexicalConverter &c, T &v)
102 std::istringstream ss(c.get());
103 ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws);
105 if(ss.fail() || !ss.eof())
106 throw lexical_error("conversion failure");
109 // The main interface to the lexical conversion machinery
112 inline T lexical_cast(const std::string &s, const Fmt &f = Fmt())
114 LexicalConverter conv(s, f);
121 inline std::string lexical_cast(const T &v, const Fmt &f = Fmt())
123 LexicalConverter conv(f);