]> git.tdb.fi Git - libs/core.git/blobdiff - source/strings/lexicalcast.h
Throw from LexicalConverter::get if no conversion was performed
[libs/core.git] / source / strings / lexicalcast.h
index c6a17da3491d053b6460399fddea6473ea1ca839..25e3268126bde5d65cf19dca35d188c96dcebb5e 100644 (file)
@@ -4,6 +4,7 @@
 #include <sstream>
 #include <string>
 #include <stdexcept>
+#include <msp/core/meta.h>
 #include "fmt.h"
 
 namespace Msp {
@@ -34,14 +35,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 +91,39 @@ void operator>>(const LexicalConverter &, std::string &);
 // Generic operators using stringstream
 
 template<typename T>
-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<typename U>
+       static Yes f(int (*)[sizeof(s<<HasFormattedOutput<U>::v)]);
+       template<typename U>
+       static No f(...);
+
+       enum { value = Evaluate<sizeof(f<T>(0))>::value };
+};
+
+template<typename T>
+struct HasFormattedInput: Sfinae
+{
+       static std::istream &s;
+       static T &v;
+
+       template<typename U>
+       static Yes f(int (*)[sizeof(s>>HasFormattedOutput<U>::v)]);
+       template<typename U>
+       static No f(...);
+
+       enum { value = Evaluate<sizeof(f<T>(0))>::value };
+};
+
+
+template<typename T>
+typename EnableIf<HasFormattedOutput<T>::value, void>::Yes
+operator<<(LexicalConverter &c, const T &v)
 {
        std::ostringstream ss;
        ss<<c.get_fmt()<<v;
@@ -97,7 +131,8 @@ void operator<<(LexicalConverter &c, const T &v)
 }
 
 template<typename T>
-void operator>>(const LexicalConverter &c, T &v)
+typename EnableIf<HasFormattedInput<T>::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);