]> git.tdb.fi Git - libs/core.git/blobdiff - source/strings/lexicalcast.h
Filter the types that get fallback lexical conversion operators
[libs/core.git] / source / strings / lexicalcast.h
index b41654d1537a822f8a13af2d1a0616a4761d0e2f..80963f44dd3a233ff2d16b01ec007935d01e73c9 100644 (file)
@@ -1,16 +1,10 @@
-/* $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 <sstream>
 #include <string>
 #include <stdexcept>
+#include <msp/core/meta.h>
 #include "fmt.h"
 
 namespace Msp {
@@ -96,7 +90,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;
@@ -104,7 +130,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);
@@ -113,23 +140,51 @@ void operator>>(const LexicalConverter &c, T &v)
                throw lexical_error("conversion failure");
 }
 
-// The main interface to the lexical conversion machinery
+// Helper struct to provide partial template specialization
+
+template<typename T, typename F>
+struct LexicalCast;
 
 template<typename T>
-inline T lexical_cast(const std::string &s, const Fmt &f = Fmt())
+struct LexicalCast<T, std::string>
 {
-       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<typename T>
-inline std::string lexical_cast(const T &v, const Fmt &f = Fmt())
+template<typename F>
+struct LexicalCast<std::string, F>
+{
+       static std::string cast(const F &v, const Fmt &f = Fmt())
+       {
+               LexicalConverter conv(f);
+               conv<<v;
+               return conv.get();
+       }
+};
+
+template<>
+struct LexicalCast<std::string, std::string>
+{
+       static std::string cast(const std::string &v, const Fmt &f = Fmt())
+       {
+               LexicalConverter conv(f);
+               conv<<v;
+               return conv.get();
+       }
+};
+
+// The main interface to the lexical conversion machinery
+
+template<typename T, typename F>
+inline T lexical_cast(const F &v, const Fmt &f = Fmt())
 {
-       LexicalConverter conv(f);
-       conv<<v;
-       return conv.get();
+       return LexicalCast<T, F>::cast(v, f);
 }
 
 } // namespace Msp