X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fstrings%2Flexicalcast.h;h=ccb77909893091c4a929bbd51772ab5ef9046682;hb=HEAD;hp=2ed0621291858c0d0f4d40fb37036fd5decc5700;hpb=ea901644ac2d36256d1f38c7a3adaaee787fe1b9;p=libs%2Fcore.git diff --git a/source/strings/lexicalcast.h b/source/strings/lexicalcast.h index 2ed0621..ccb7790 100644 --- a/source/strings/lexicalcast.h +++ b/source/strings/lexicalcast.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "fmt.h" namespace Msp { @@ -12,37 +13,35 @@ namespace Msp { /** Thrown for errors in lexical conversions. */ -class lexical_error: public std::runtime_error +class MSPCORE_API lexical_error: public std::runtime_error { public: lexical_error(const std::string &w): runtime_error(w) { } - virtual ~lexical_error() throw() { } }; /** Thrown when the format is unsuitable for the type being converted. */ -class format_mismatch: public lexical_error +class MSPCORE_API format_mismatch: public lexical_error { public: format_mismatch(const std::string &w): lexical_error(w) { } - virtual ~format_mismatch() throw() { } }; /** Helper class for lexical_cast to facilitate operator overloading. */ -class LexicalConverter +class MSPCORE_API LexicalConverter { private: Fmt fmt; - bool filled; + bool filled = false; std::string buf; public: - LexicalConverter(const Fmt &f): fmt(f), filled(false) { } + LexicalConverter(const Fmt &f): fmt(f) { } LexicalConverter(const std::string &s, const Fmt &f): fmt(f), filled(true), buf(s) { } const Fmt &get_fmt() const { return fmt; } @@ -51,95 +50,80 @@ public: }; -void operator<<(LexicalConverter &, char); -void operator<<(LexicalConverter &, signed char); -void operator<<(LexicalConverter &, short); -void operator<<(LexicalConverter &, int); -void operator<<(LexicalConverter &, long); -void operator<<(LexicalConverter &, unsigned char); -void operator<<(LexicalConverter &, unsigned short); -void operator<<(LexicalConverter &, unsigned); -void operator<<(LexicalConverter &, unsigned long); -#ifdef __GNUC__ -void operator<<(LexicalConverter &, long long); -void operator<<(LexicalConverter &, unsigned long long); -#endif -void operator<<(LexicalConverter &, bool); -void operator<<(LexicalConverter &, float); -void operator<<(LexicalConverter &, double); -void operator<<(LexicalConverter &, long double); -void operator<<(LexicalConverter &, const std::string &); -void operator<<(LexicalConverter &, const char *); -void operator<<(LexicalConverter &, const void *); - -void operator>>(const LexicalConverter &, char &); -void operator>>(const LexicalConverter &, signed char &); -void operator>>(const LexicalConverter &, short &); -void operator>>(const LexicalConverter &, int &); -void operator>>(const LexicalConverter &, long &); -void operator>>(const LexicalConverter &, unsigned char &); -void operator>>(const LexicalConverter &, unsigned short &); -void operator>>(const LexicalConverter &, unsigned int &); -void operator>>(const LexicalConverter &, unsigned long &); -#ifdef __GNUC__ -void operator>>(const LexicalConverter &, long long &); -void operator>>(const LexicalConverter &, unsigned long long &); -#endif -void operator>>(const LexicalConverter &, bool &); -void operator>>(const LexicalConverter &, float &); -void operator>>(const LexicalConverter &, double &); -void operator>>(const LexicalConverter &, long double &); -void operator>>(const LexicalConverter &, std::string &); +MSPCORE_API void operator<<(LexicalConverter &, char); +MSPCORE_API void operator<<(LexicalConverter &, signed char); +MSPCORE_API void operator<<(LexicalConverter &, short); +MSPCORE_API void operator<<(LexicalConverter &, int); +MSPCORE_API void operator<<(LexicalConverter &, long); +MSPCORE_API void operator<<(LexicalConverter &, unsigned char); +MSPCORE_API void operator<<(LexicalConverter &, unsigned short); +MSPCORE_API void operator<<(LexicalConverter &, unsigned); +MSPCORE_API void operator<<(LexicalConverter &, unsigned long); +MSPCORE_API void operator<<(LexicalConverter &, long long); +MSPCORE_API void operator<<(LexicalConverter &, unsigned long long); +MSPCORE_API void operator<<(LexicalConverter &, bool); +MSPCORE_API void operator<<(LexicalConverter &, float); +MSPCORE_API void operator<<(LexicalConverter &, double); +MSPCORE_API void operator<<(LexicalConverter &, long double); +MSPCORE_API void operator<<(LexicalConverter &, const std::string &); +MSPCORE_API void operator<<(LexicalConverter &, const char *); +MSPCORE_API void operator<<(LexicalConverter &, const void *); + +MSPCORE_API void operator>>(const LexicalConverter &, char &); +MSPCORE_API void operator>>(const LexicalConverter &, signed char &); +MSPCORE_API void operator>>(const LexicalConverter &, short &); +MSPCORE_API void operator>>(const LexicalConverter &, int &); +MSPCORE_API void operator>>(const LexicalConverter &, long &); +MSPCORE_API void operator>>(const LexicalConverter &, unsigned char &); +MSPCORE_API void operator>>(const LexicalConverter &, unsigned short &); +MSPCORE_API void operator>>(const LexicalConverter &, unsigned int &); +MSPCORE_API void operator>>(const LexicalConverter &, unsigned long &); +MSPCORE_API void operator>>(const LexicalConverter &, long long &); +MSPCORE_API void operator>>(const LexicalConverter &, unsigned long long &); +MSPCORE_API void operator>>(const LexicalConverter &, bool &); +MSPCORE_API void operator>>(const LexicalConverter &, float &); +MSPCORE_API void operator>>(const LexicalConverter &, double &); +MSPCORE_API void operator>>(const LexicalConverter &, long double &); +MSPCORE_API void operator>>(const LexicalConverter &, std::string &); // Generic operators using stringstream -template -struct HasFormattedOutput: Sfinae +struct CheckFormattedOutput: Sfinae { static std::ostream &s; - static T &v; - - /* The expression must depend on the template parameter, or the compiler - will give an error. */ - template - static Yes f(int (*)[sizeof(s<::v)]); - template - static No f(...); - - enum { value = Evaluate(0))>::value }; + template + static Yes f(int (*)[sizeof(s<(s))]); + using Sfinae::f; }; -template -struct HasFormattedInput: Sfinae +struct CheckFormattedInput: Sfinae { static std::istream &s; - static T &v; - - template - static Yes f(int (*)[sizeof(s>>HasFormattedOutput::v)]); - template - static No f(...); - - enum { value = Evaluate(0))>::value }; + template + static Yes f(int (*)[sizeof(s>>reinterpret_cast(s))]); + using Sfinae::f; }; +template struct HasFormattedOutput: Sfinae::Evaluate { }; +template struct HasFormattedInput: Sfinae::Evaluate { }; + template -typename EnableIf::value, void>::Yes +typename std::enable_if::value>::type operator<<(LexicalConverter &c, const T &v) { std::ostringstream ss; - ss< -typename EnableIf::value, void>::Yes +typename std::enable_if::value>::type operator>>(const LexicalConverter &c, T &v) { std::istringstream ss(c.get()); ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws); - ss>>v; + ss >> v; if(ss.fail() || !ss.eof()) throw lexical_error("conversion failure"); } @@ -157,7 +141,7 @@ struct LexicalCast { LexicalConverter conv(s, f); T result; - conv>>result; + conv >> result; return result; } }; @@ -168,7 +152,7 @@ struct LexicalCast static std::string cast(const F &v, const Fmt &f = Fmt()) { LexicalConverter conv(f); - conv< static std::string cast(const std::string &v, const Fmt &f = Fmt()) { LexicalConverter conv(f); - conv<