23 #ifndef STRING20100713H 24 #define STRING20100713H 26 #include <boost/lexical_cast.hpp> 34 #include <boost/algorithm/string.hpp> 35 #include <boost/mpl/and.hpp> 36 #include <boost/mpl/logical.hpp> 37 #include <boost/numeric/conversion/cast.hpp> 38 #include <boost/static_assert.hpp> 39 #include <boost/type_traits.hpp> 40 #include <boost/utility.hpp> 49 template <
typename To>
50 typename boost::enable_if<boost::is_arithmetic<To>, To>::type
51 _as_from_string(
const std::string& from)
55 return boost::lexical_cast<To>(from);
57 catch (boost::bad_lexical_cast&)
60 return std::numeric_limits<To>::has_quiet_NaN ? std::numeric_limits<To>::quiet_NaN()
61 :
std::numeric_limits<To>::max();
66 template <
typename To>
67 typename boost::enable_if<boost::is_enum<To>, To>::type _as_from_string(
const std::string& from)
71 return static_cast<To
>(boost::lexical_cast<
int>(from));
73 catch (boost::bad_lexical_cast&)
75 return static_cast<To
>(0);
79 template <
typename To>
80 typename boost::enable_if<boost::is_class<To>, To>::type _as_from_string(
const std::string& from)
84 return boost::lexical_cast<To>(from);
86 catch (boost::bad_lexical_cast&)
92 template <>
inline bool _as_from_string<bool>(
const std::string& from)
94 return (boost::iequals(from,
"true") || boost::iequals(from,
"1"));
97 template <>
inline std::string _as_from_string<std::string>(
const std::string& from)
102 template <
typename To,
typename From> std::string _as_to_string(
const From& from)
106 return boost::lexical_cast<std::string>(from);
108 catch (boost::bad_lexical_cast&)
110 return std::string();
115 template <>
inline std::string _as_to_string<std::string, bool>(
const bool& from)
117 return from ?
"true" :
"false";
120 template <>
inline std::string _as_to_string<std::string, std::string>(
const std::string& from)
125 template <
typename To,
typename From>
126 typename boost::disable_if<boost::is_same<To, From>, To>::type _as_numeric(
const From& from)
130 return boost::numeric_cast<To>(from);
132 catch (boost::bad_numeric_cast&)
135 return std::numeric_limits<To>::has_quiet_NaN ? std::numeric_limits<To>::quiet_NaN()
136 :
std::numeric_limits<To>::max();
140 template <
typename To,
typename From>
141 typename boost::enable_if<boost::is_same<To, From>, To>::type _as_numeric(
const From& from)
146 template <
typename To> To as(
const std::string& from) {
return _as_from_string<To>(from); }
148 template <
typename To,
typename From>
149 typename boost::enable_if<boost::is_same<To, std::string>, To>::type as(
const From& from)
151 return _as_to_string<To, From>(from);
154 template <
typename To,
typename From>
155 typename boost::enable_if<boost::mpl::and_<boost::is_arithmetic<To>, boost::is_arithmetic<From> >,
159 return _as_numeric<To, From>(from);
163 template <
typename To,
typename From>
164 typename boost::enable_if<boost::mpl::and_<boost::is_enum<To>, boost::is_arithmetic<From> >,
168 return static_cast<To
>(from);
171 enum FloatRepresentation
178 template <
typename To,
typename From>
179 To as(
const From& from,
int precision, FloatRepresentation rep = FLOAT_DEFAULT)
185 inline std::string as<std::string, double>(
const double& from,
int precision,
186 FloatRepresentation rep)
188 std::stringstream out;
191 case FLOAT_DEFAULT:
break;
193 case FLOAT_FIXED: out << std::fixed;
break;
194 case FLOAT_SCIENTIFIC: out << std::scientific;
break;
197 out << std::setprecision(precision) << from;
202 inline std::string as<std::string, float>(
const float& from,
int precision, FloatRepresentation rep)
204 std::stringstream out;
207 case FLOAT_DEFAULT:
break;
209 case FLOAT_FIXED: out << std::fixed;
break;
210 case FLOAT_SCIENTIFIC: out << std::scientific;
break;
213 out << std::setprecision(precision) << from;
The global namespace for the Goby project.