#include <sstream>
#include <string>
#include <stdexcept>
+#include <msp/core/meta.h>
#include "fmt.h"
namespace Msp {
/**
-Thrown for errors in lexical conversions
+Thrown for errors in lexical conversions.
*/
class lexical_error: public std::runtime_error
{
};
+/**
+Thrown when the format is unsuitable for the type being converted.
+*/
class format_mismatch: public lexical_error
{
public:
{
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 &);
};
// 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;
}
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);
throw lexical_error("conversion failure");
}
-// Helper struct to provide partial template specialization
-
+/**
+Helper struct to provide partial template specialization.
+*/
template<typename T, typename F>
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<typename T, typename F>
inline T lexical_cast(const F &v, const Fmt &f = Fmt())
{