X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Flexicalcast.h;h=393622d54771bb5197886a7423627fce6066b519;hp=b41654d1537a822f8a13af2d1a0616a4761d0e2f;hb=991fabc1956b73a4007859058fb44171000b452e;hpb=4a38eb20402936497cbb75527dd7a7b321f51415 diff --git a/source/strings/lexicalcast.h b/source/strings/lexicalcast.h index b41654d..393622d 100644 --- a/source/strings/lexicalcast.h +++ b/source/strings/lexicalcast.h @@ -1,36 +1,33 @@ -/* $Id$ - -This file is part of libmspstrings -Copyright © 2006-2008 Mikko Rasa -Distributed under the LGPL -*/ - #ifndef MSP_STRINGS_LEXICALCAST_H_ #define MSP_STRINGS_LEXICALCAST_H_ #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 { public: lexical_error(const std::string &w): runtime_error(w) { } - virtual ~lexical_error() throw() { } + ~lexical_error() throw() override = default; }; +/** +Thrown when the format is unsuitable for the type being converted. +*/ class format_mismatch: public lexical_error { public: format_mismatch(const std::string &w): lexical_error(w) { } - virtual ~format_mismatch() throw() { } + ~format_mismatch() throw() override = default; }; @@ -41,14 +38,15 @@ class LexicalConverter { private: Fmt fmt; + bool filled = false; std::string buf; public: LexicalConverter(const Fmt &f): fmt(f) { } - LexicalConverter(const std::string &s, const Fmt &f): fmt(f), buf(s) { } + 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 &); }; @@ -95,41 +93,94 @@ void operator>>(const LexicalConverter &, std::string &); // Generic operators using stringstream +struct CheckFormattedOutput: Sfinae +{ + static std::ostream &s; + template + static Yes f(int (*)[sizeof(s<(s))]); + using Sfinae::f; +}; + +struct CheckFormattedInput: Sfinae +{ + static std::istream &s; + template + static Yes f(int (*)[sizeof(s>>reinterpret_cast(s))]); + using Sfinae::f; +}; + +template struct HasFormattedOutput: Sfinae::Evaluate { }; +template struct HasFormattedInput: Sfinae::Evaluate { }; + + template -void operator<<(LexicalConverter &c, const T &v) +typename std::enable_if::value>::type +operator<<(LexicalConverter &c, const T &v) { std::ostringstream ss; - ss< -void operator>>(const LexicalConverter &c, T &v) +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"); } -// The main interface to the lexical conversion machinery +/** +Helper struct to provide partial template specialization. +*/ +template +struct LexicalCast; template -inline T lexical_cast(const std::string &s, const Fmt &f = Fmt()) +struct LexicalCast { - LexicalConverter conv(s, f); - T result; - conv>>result; - return result; -} + static T cast(const std::string &s, const Fmt &f = Fmt()) + { + LexicalConverter conv(s, f); + T result; + conv >> result; + return result; + } +}; -template -inline std::string lexical_cast(const T &v, const Fmt &f = Fmt()) +template +struct LexicalCast +{ + static std::string cast(const F &v, const Fmt &f = Fmt()) + { + LexicalConverter conv(f); + conv << v; + return conv.get(); + } +}; + +template<> +struct LexicalCast +{ + static std::string cast(const std::string &v, const Fmt &f = Fmt()) + { + LexicalConverter conv(f); + conv << v; + return conv.get(); + } +}; + +/** 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()) { - LexicalConverter conv(f); - conv<::cast(v, f); } } // namespace Msp