]> git.tdb.fi Git - libs/core.git/blobdiff - source/strings/lexicalcast.h
Add move semantics to Variant
[libs/core.git] / source / strings / lexicalcast.h
index 3bfc342a1f584e91cc218eb24b226c7a29c777da..ccb77909893091c4a929bbd51772ab5ef9046682 100644 (file)
-/* $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 <msp/core/except.h>
+#include <stdexcept>
+#include <msp/core/meta.h>
+#include <msp/core/mspcore_api.h>
 #include "fmt.h"
 
 namespace Msp {
 
 /**
-Thrown for errors in lexical conversions
+Thrown for errors in lexical conversions.
+*/
+class MSPCORE_API lexical_error: public std::runtime_error
+{
+public:
+       lexical_error(const std::string &w): runtime_error(w) { }
+};
+
+
+/**
+Thrown when the format is unsuitable for the type being converted.
 */
-class LexicalError: public Exception
+class MSPCORE_API format_mismatch: public lexical_error
 {
 public:
-       LexicalError(const std::string &w_): Exception(w_) { }
+       format_mismatch(const std::string &w): lexical_error(w) { }
 };
 
 
 /**
 Helper class for lexical_cast to facilitate operator overloading.
 */
-class LexicalConverter
+class MSPCORE_API 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 &);
 };
 
 
-void operator<<(LexicalConverter &, char);
-void operator<<(LexicalConverter &, signed char);
-void operator<<(LexicalConverter &, short);
-void operator<<(LexicalConverter &, int);
-void operator<<(LexicalConverter &, long);
-void operator<<(LexicalConverter &, unsigned char);
-void operator<<(LexicalConverter &, unsigned short);
-void operator<<(LexicalConverter &, unsigned);
-void operator<<(LexicalConverter &, unsigned long);
-#ifdef __GNUC__
-void operator<<(LexicalConverter &, long long);
-void operator<<(LexicalConverter &, unsigned long long);
-#endif
-void operator<<(LexicalConverter &, bool);
-void operator<<(LexicalConverter &, float);
-void operator<<(LexicalConverter &, double);
-void operator<<(LexicalConverter &, long double);
-void operator<<(LexicalConverter &, const std::string &);
-void operator<<(LexicalConverter &, const char *);
-void operator<<(LexicalConverter &, const void *);
-
-void operator>>(const LexicalConverter &, char &);
-void operator>>(const LexicalConverter &, signed char &);
-void operator>>(const LexicalConverter &, short &);
-void operator>>(const LexicalConverter &, int &);
-void operator>>(const LexicalConverter &, long &);
-void operator>>(const LexicalConverter &, unsigned char &);
-void operator>>(const LexicalConverter &, unsigned short &);
-void operator>>(const LexicalConverter &, unsigned int &);
-void operator>>(const LexicalConverter &, unsigned long &);
-#ifdef __GNUC__
-void operator>>(const LexicalConverter &, long long &);
-void operator>>(const LexicalConverter &, unsigned long long &);
-#endif
-void operator>>(const LexicalConverter &, bool &);
-void operator>>(const LexicalConverter &, float &);
-void operator>>(const LexicalConverter &, double &);
-void operator>>(const LexicalConverter &, long double &);
-void operator>>(const LexicalConverter &, std::string &);
+MSPCORE_API void operator<<(LexicalConverter &, char);
+MSPCORE_API void operator<<(LexicalConverter &, signed char);
+MSPCORE_API void operator<<(LexicalConverter &, short);
+MSPCORE_API void operator<<(LexicalConverter &, int);
+MSPCORE_API void operator<<(LexicalConverter &, long);
+MSPCORE_API void operator<<(LexicalConverter &, unsigned char);
+MSPCORE_API void operator<<(LexicalConverter &, unsigned short);
+MSPCORE_API void operator<<(LexicalConverter &, unsigned);
+MSPCORE_API void operator<<(LexicalConverter &, unsigned long);
+MSPCORE_API void operator<<(LexicalConverter &, long long);
+MSPCORE_API void operator<<(LexicalConverter &, unsigned long long);
+MSPCORE_API void operator<<(LexicalConverter &, bool);
+MSPCORE_API void operator<<(LexicalConverter &, float);
+MSPCORE_API void operator<<(LexicalConverter &, double);
+MSPCORE_API void operator<<(LexicalConverter &, long double);
+MSPCORE_API void operator<<(LexicalConverter &, const std::string &);
+MSPCORE_API void operator<<(LexicalConverter &, const char *);
+MSPCORE_API void operator<<(LexicalConverter &, const void *);
+
+MSPCORE_API void operator>>(const LexicalConverter &, char &);
+MSPCORE_API void operator>>(const LexicalConverter &, signed char &);
+MSPCORE_API void operator>>(const LexicalConverter &, short &);
+MSPCORE_API void operator>>(const LexicalConverter &, int &);
+MSPCORE_API void operator>>(const LexicalConverter &, long &);
+MSPCORE_API void operator>>(const LexicalConverter &, unsigned char &);
+MSPCORE_API void operator>>(const LexicalConverter &, unsigned short &);
+MSPCORE_API void operator>>(const LexicalConverter &, unsigned int &);
+MSPCORE_API void operator>>(const LexicalConverter &, unsigned long &);
+MSPCORE_API void operator>>(const LexicalConverter &, long long &);
+MSPCORE_API void operator>>(const LexicalConverter &, unsigned long long &);
+MSPCORE_API void operator>>(const LexicalConverter &, bool &);
+MSPCORE_API void operator>>(const LexicalConverter &, float &);
+MSPCORE_API void operator>>(const LexicalConverter &, double &);
+MSPCORE_API void operator>>(const LexicalConverter &, long double &);
+MSPCORE_API void operator>>(const LexicalConverter &, std::string &);
 
 // Generic operators using stringstream
 
+struct CheckFormattedOutput: Sfinae
+{
+       static std::ostream &s;
+       template<typename T>
+       static Yes f(int (*)[sizeof(s<<reinterpret_cast<const T &>(s))]);
+       using Sfinae::f;
+};
+
+struct CheckFormattedInput: Sfinae
+{
+       static std::istream &s;
+       template<typename T>
+       static Yes f(int (*)[sizeof(s>>reinterpret_cast<T &>(s))]);
+       using Sfinae::f;
+};
+
+template<typename T> struct HasFormattedOutput: Sfinae::Evaluate<CheckFormattedOutput, T> { };
+template<typename T> struct HasFormattedInput: Sfinae::Evaluate<CheckFormattedInput, T> { };
+
+
 template<typename T>
-void operator<<(LexicalConverter &c, const T &v)
+typename std::enable_if<HasFormattedOutput<T>::value>::type
+operator<<(LexicalConverter &c, const T &v)
 {
        std::ostringstream ss;
-       ss<<c.get_fmt()<<v;
+       ss << c.get_fmt() << v;
        c.result(ss.str());
 }
 
 template<typename T>
-void operator>>(const LexicalConverter &c, T &v)
+typename std::enable_if<HasFormattedInput<T>::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 LexicalError("Conversion failure");
+               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();
+       }
+};
+
+/** 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<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