3 This file is part of libmspstrings
4 Copyright © 2006-2008 Mikko Rasa
5 Distributed under the LGPL
8 #ifndef MSP_STRINGS_LEXICALCAST_H_
9 #define MSP_STRINGS_LEXICALCAST_H_
13 #include <msp/core/except.h>
19 Thrown for errors in lexical conversions
21 class LexicalError: public Exception
24 LexicalError(const std::string &w_): Exception(w_) { }
29 Helper class for lexical_cast to facilitate operator overloading.
31 class LexicalConverter
38 LexicalConverter(const Fmt &f): fmt(f) { }
39 LexicalConverter(const std::string &s, const Fmt &f): fmt(f), buf(s) { }
41 const Fmt &get_fmt() const { return fmt; }
42 const std::string &get() const { return buf; }
43 void result(const std::string &);
47 void operator<<(LexicalConverter &, char);
48 void operator<<(LexicalConverter &, signed char);
49 void operator<<(LexicalConverter &, short);
50 void operator<<(LexicalConverter &, int);
51 void operator<<(LexicalConverter &, long);
52 void operator<<(LexicalConverter &, unsigned char);
53 void operator<<(LexicalConverter &, unsigned short);
54 void operator<<(LexicalConverter &, unsigned);
55 void operator<<(LexicalConverter &, unsigned long);
57 void operator<<(LexicalConverter &, long long);
58 void operator<<(LexicalConverter &, unsigned long long);
60 void operator<<(LexicalConverter &, bool);
61 void operator<<(LexicalConverter &, float);
62 void operator<<(LexicalConverter &, double);
63 void operator<<(LexicalConverter &, long double);
64 void operator<<(LexicalConverter &, const std::string &);
65 void operator<<(LexicalConverter &, const char *);
66 void operator<<(LexicalConverter &, const void *);
68 void operator>>(const LexicalConverter &, char &);
69 void operator>>(const LexicalConverter &, signed char &);
70 void operator>>(const LexicalConverter &, short &);
71 void operator>>(const LexicalConverter &, int &);
72 void operator>>(const LexicalConverter &, long &);
73 void operator>>(const LexicalConverter &, unsigned char &);
74 void operator>>(const LexicalConverter &, unsigned short &);
75 void operator>>(const LexicalConverter &, unsigned int &);
76 void operator>>(const LexicalConverter &, unsigned long &);
78 void operator>>(const LexicalConverter &, long long &);
79 void operator>>(const LexicalConverter &, unsigned long long &);
81 void operator>>(const LexicalConverter &, bool &);
82 void operator>>(const LexicalConverter &, float &);
83 void operator>>(const LexicalConverter &, double &);
84 void operator>>(const LexicalConverter &, long double &);
85 void operator>>(const LexicalConverter &, std::string &);
87 // Generic operators using stringstream
90 void operator<<(LexicalConverter &c, const T &v)
92 std::ostringstream ss;
98 void operator>>(const LexicalConverter &c, T &v)
100 std::istringstream ss(c.get());
101 ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws);
103 if(ss.fail() || !ss.eof())
104 throw LexicalError("Conversion failure");
107 // The main interface to the lexical conversion machinery
110 inline T lexical_cast(const std::string &s, const Fmt &f = Fmt())
112 LexicalConverter conv(s, f);
119 inline std::string lexical_cast(const T &v, const Fmt &f = Fmt())
121 LexicalConverter conv(f);