X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Flexicalcast.h;h=22b45e88c55eef60e433b6253d0070b77c9242a5;hp=25e3268126bde5d65cf19dca35d188c96dcebb5e;hb=4f04dc8e2dcb48d524c141cb14ce05b655608454;hpb=468ec9f6554c89b52e10a280411c9b9d99ef451b diff --git a/source/strings/lexicalcast.h b/source/strings/lexicalcast.h index 25e3268..22b45e8 100644 --- a/source/strings/lexicalcast.h +++ b/source/strings/lexicalcast.h @@ -10,7 +10,7 @@ namespace Msp { /** -Thrown for errors in lexical conversions +Thrown for errors in lexical conversions. */ class lexical_error: public std::runtime_error { @@ -20,6 +20,9 @@ public: }; +/** +Thrown when the format is unsuitable for the type being converted. +*/ class format_mismatch: public lexical_error { public: @@ -90,43 +93,32 @@ 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 operator<<(LexicalConverter &c, const T &v) { std::ostringstream ss; - ss<>(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"); } -// Helper struct to provide partial template specialization - +/** +Helper struct to provide partial template specialization. +*/ template struct LexicalCast; @@ -153,7 +146,7 @@ struct LexicalCast { LexicalConverter conv(s, f); T result; - conv>>result; + conv >> result; return result; } }; @@ -164,7 +157,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< inline T lexical_cast(const F &v, const Fmt &f = Fmt()) {