36 template<
typename T,
typename R>
37 bool cast(
const T& in, R& out) {
41 return !!ss && ss.eof();
46 bool cast(
const T& in, std::string& out) {
55 inline bool cast(
const std::string& in, std::string& out) {
62 inline bool cast(
const std::string& in,
bool& b) {
65 else if(in ==
"false")
74 inline bool cast(
const std::string& in,
char& c) {
83 inline bool cast(
const std::string& in,
long& l) {
84 if(in.find_first_not_of(
"-0123456789") != std::string::npos)
86 std::size_t t = in.find_last_of(
'-');
87 if(t != std::string::npos && t != 0)
95 inline bool cast(
const std::string& in,
int& i) {
106 inline bool cast(
const std::string& in,
double& d) {
107 if(in.find_first_not_of(
"-0123456789.") != std::string::npos)
109 std::size_t t = in.find_last_of(
'-');
110 if(t != std::string::npos && t != 0)
112 t = in.find_first_of(
'.');
113 if(t != std::string::npos && (t == 0 || t == in.length()-1 || in.find_first_of(
'.', t+1) != std::string::npos))
115 d = atof(in.c_str());
121 inline bool cast(
const std::string& in,
float& f) {
132 inline bool cast(
const bool& in, std::string& out) {
133 out = (in ?
"true" :
"false");
141 template<
class T,
class R>
143 std::stringstream ss;
144 ss <<
"failed to cast value of type '" << demangle_type<T>() <<
"' to type '" << demangle_type<R>() <<
"'";
147 virtual const char*
what()
const throw() {
return m_what.c_str(); }
153 template<
typename R,
typename T>
165 template<
typename R,
typename T>
168 return safe_cast<R,T>(t);
170 std::cerr <<
"CAST ERROR: " << e.
what() << std::endl;