35#include <boost/algorithm/string.hpp>
36#include <boost/lexical_cast.hpp>
37#include <boost/mpl/and.hpp>
38#include <boost/mpl/logical.hpp>
39#include <boost/numeric/conversion/cast.hpp>
40#include <boost/type_traits.hpp>
41#include <boost/utility.hpp>
51typename boost::enable_if<boost::is_arithmetic<To>, To>::type
56 return boost::lexical_cast<To>(from);
58 catch (boost::bad_lexical_cast&)
61 return std::numeric_limits<To>::has_quiet_NaN ? std::numeric_limits<To>::quiet_NaN()
62 : std::numeric_limits<To>::max();
68typename boost::enable_if<boost::is_enum<To>, To>::type
_as_from_string(
const std::string& from)
72 return static_cast<To
>(boost::lexical_cast<int>(from));
74 catch (boost::bad_lexical_cast&)
76 return static_cast<To
>(0);
81typename boost::enable_if<boost::is_class<To>, To>::type
_as_from_string(
const std::string& from)
85 return boost::lexical_cast<To>(from);
87 catch (boost::bad_lexical_cast&)
95 return (boost::iequals(from,
"true") || boost::iequals(from,
"1"));
103template <
typename To,
typename From> std::string
_as_to_string(
const From& from)
107 return boost::lexical_cast<std::string>(from);
109 catch (boost::bad_lexical_cast&)
111 return std::string();
118 return from ?
"true" :
"false";
126template <
typename To,
typename From>
127typename boost::disable_if<boost::is_same<To, From>, To>::type
_as_numeric(
const From& from)
131 return boost::numeric_cast<To>(from);
133 catch (boost::bad_numeric_cast&)
136 return std::numeric_limits<To>::has_quiet_NaN ? std::numeric_limits<To>::quiet_NaN()
137 : std::numeric_limits<To>::max();
141template <
typename To,
typename From>
142typename boost::enable_if<boost::is_same<To, From>, To>::type
_as_numeric(
const From& from)
147template <
typename To> To
as(
const std::string& from) {
return _as_from_string<To>(from); }
149template <
typename To,
typename From>
150typename boost::enable_if<boost::is_same<To, std::string>, To>::type
as(
const From& from)
152 return _as_to_string<To, From>(from);
155template <
typename To,
typename From>
156typename boost::enable_if<boost::mpl::and_<boost::is_arithmetic<To>, boost::is_arithmetic<From> >,
160 return _as_numeric<To, From>(from);
164template <
typename To,
typename From>
165typename boost::enable_if<boost::mpl::and_<boost::is_enum<To>, boost::is_arithmetic<From> >,
169 return static_cast<To
>(from);
179template <
typename To,
typename From>
189 std::stringstream out;
198 out << std::setprecision(precision) << from;
205 std::stringstream out;
214 out << std::setprecision(precision) << from;
bool _as_from_string< bool >(const std::string &from)
std::string as< std::string, double >(const double &from, int precision, FloatRepresentation rep)
std::string _as_from_string< std::string >(const std::string &from)
boost::disable_if< boost::is_same< To, From >, To >::type _as_numeric(const From &from)
std::string _as_to_string< std::string, std::string >(const std::string &from)
std::string as< std::string, float >(const float &from, int precision, FloatRepresentation rep)
boost::enable_if< boost::is_arithmetic< To >, To >::type _as_from_string(const std::string &from)
std::string _as_to_string(const From &from)
boost::enable_if< boost::mpl::and_< boost::is_same< To, double >, boost::is_same< From, boost::posix_time::ptime > >, To >::type as(const From &from)
std::string _as_to_string< std::string, bool >(const bool &from)
specialization of as() for bool -> string
The global namespace for the Goby project.