24 #ifndef LAUNCHER_PLUGINS_SAFE_CONVERT_HPP
25 #define LAUNCHER_PLUGINS_SAFE_CONVERT_HPP
32 #include <boost/lexical_cast.hpp>
33 #include <boost/numeric/conversion/cast.hpp>
34 #include <Optional.hpp>
39 namespace launcher_plugins {
40 namespace safe_convert {
53 T stringTo(
const std::string& in_strValue, T in_defaultValue)
57 return boost::lexical_cast<T>(in_strValue);
59 catch(boost::bad_lexical_cast&)
61 return in_defaultValue;
74 Optional<T> stringTo(
const std::string& str)
80 result = boost::lexical_cast<T>(str);
82 catch(boost::bad_lexical_cast&)
100 template <
typename T>
101 T stringTo(
const std::string& in_strValue,
103 std::ios_base& (*in_f)(std::ios_base&))
105 std::istringstream iss(in_strValue);
107 if ((iss >> in_f >> result).fail())
108 return in_defaultValue;
120 inline std::string numberToString(
double in_input,
bool in_localeIndependent =
true)
124 std::ostringstream stream;
125 if (in_localeIndependent)
126 stream.imbue(std::locale::classic());
127 stream << std::fixed;
131 CATCH_UNEXPECTED_EXCEPTION
134 return std::string();
146 template <
typename T>
147 std::string numberToString(T input,
bool localeIndependent =
true)
151 std::ostringstream stream;
152 if (localeIndependent)
153 stream.imbue(std::locale::classic());
158 CATCH_UNEXPECTED_EXCEPTION
161 return std::string();
174 template <
typename TInput,
typename TOutput>
175 TOutput numberTo(TInput input, TOutput defaultValue)
179 return boost::numeric_cast<TOutput>(input);
196 template <
typename TInput,
typename TOutput>
197 Optional<TOutput> numberTo(TInput input)
199 Optional<TOutput> result;
203 result = boost::numeric_cast<TOutput>(input);
216 #endif // LAUNCHER_PLUGINS_SAFE_CONVERT_HPP