X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Flexicalcast.h;h=2ed0621291858c0d0f4d40fb37036fd5decc5700;hp=c6a17da3491d053b6460399fddea6473ea1ca839;hb=ea901644ac2d36256d1f38c7a3adaaee787fe1b9;hpb=42ff7c629b2aa7411963bdcc2259a06b1d1de6d3 diff --git a/source/strings/lexicalcast.h b/source/strings/lexicalcast.h index c6a17da..2ed0621 100644 --- a/source/strings/lexicalcast.h +++ b/source/strings/lexicalcast.h @@ -4,12 +4,13 @@ #include #include #include +#include #include "fmt.h" namespace Msp { /** -Thrown for errors in lexical conversions +Thrown for errors in lexical conversions. */ class lexical_error: public std::runtime_error { @@ -19,6 +20,9 @@ public: }; +/** +Thrown when the format is unsuitable for the type being converted. +*/ class format_mismatch: public lexical_error { public: @@ -34,14 +38,15 @@ class LexicalConverter { private: Fmt fmt; + bool filled; std::string buf; public: - LexicalConverter(const Fmt &f): fmt(f) { } - LexicalConverter(const std::string &s, const Fmt &f): fmt(f), buf(s) { } + LexicalConverter(const Fmt &f): fmt(f), filled(false) { } + LexicalConverter(const std::string &s, const Fmt &f): fmt(f), filled(true), buf(s) { } const Fmt &get_fmt() const { return fmt; } - const std::string &get() const { return buf; } + const std::string &get() const; void result(const std::string &); }; @@ -89,7 +94,39 @@ void operator>>(const LexicalConverter &, std::string &); // Generic operators using stringstream template -void operator<<(LexicalConverter &c, const T &v) +struct HasFormattedOutput: 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 +struct HasFormattedInput: 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 +typename EnableIf::value, void>::Yes +operator<<(LexicalConverter &c, const T &v) { std::ostringstream ss; ss< -void operator>>(const LexicalConverter &c, T &v) +typename EnableIf::value, void>::Yes +operator>>(const LexicalConverter &c, T &v) { std::istringstream ss(c.get()); ss.setf(std::ios_base::fmtflags(0), std::ios_base::skipws); @@ -106,8 +144,9 @@ void operator>>(const LexicalConverter &c, T &v) throw lexical_error("conversion failure"); } -// Helper struct to provide partial template specialization - +/** +Helper struct to provide partial template specialization. +*/ template struct LexicalCast; @@ -145,8 +184,10 @@ struct LexicalCast } }; -// The main interface to the lexical conversion machinery - +/** Perform a lexical conversion between a string and another type. The source +type can normally be deduced by the compiler, so this can be used just like the +standard C++ casts. A format may additionally be specified to force a specific +interpretation. */ template inline T lexical_cast(const F &v, const Fmt &f = Fmt()) {