25 #ifdef LEXICAL_CAST_DEBUG
26 #undef LEXICAL_CAST_HPP_INCLUDED
29 #ifndef LEXICAL_CAST_HPP_INCLUDED
30 #define LEXICAL_CAST_HPP_INCLUDED
32 #ifdef LEXICAL_CAST_DEBUG
42 #define DEBUG_THROW(id) throw id;
53 #include <type_traits>
55 #define DEBUG_THROW(id)
68 ,
typename ToEnable = void
69 ,
typename FromEnable =
void
87 template<
typename To,
typename From>
104 template<
typename To,
typename From>
113 const char*
what() const noexcept
115 return "bad_lexical_cast";
136 ,
typename FromEnable
140 To
operator()(From value, utils::optional<To> fallback)
const
145 std::stringstream sstr;
147 if(!(sstr << value && sstr >> result)) {
148 if(fallback) {
return *fallback; }
163 template <
typename From>
168 , std::enable_if_t<std::is_arithmetic_v<From>>
171 std::string
operator()(From value, utils::optional<std::string>)
const
173 DEBUG_THROW(
"specialized - To std::string - From arithmetic");
174 if constexpr (std::is_same_v<bool, From>) {
175 return value ?
"1" :
"0";
197 bool operator()(std::string_view str, utils::optional<bool> fallback)
const
199 DEBUG_THROW(
"specialized - To bool - From string");
203 }
else if(str ==
"0") {
205 }
else if (fallback) {
218 template <
typename To>
222 , std::enable_if_t<std::is_arithmetic_v<To>>
226 To
operator()(std::string_view str, utils::optional<To> fallback)
const
228 DEBUG_THROW(
"specialized - To arithmetic - From string");
235 if(ec == std::errc()) {
250 template <
typename To>
254 , std::enable_if_t<std::is_arithmetic_v<To>>
258 To
operator()(
const std::string& value, utils::optional<To> fallback)
const
262 return lexical_cast_default<To>(std::string_view(value), *fallback);
264 return lexical_cast<To>(std::string_view(value));
275 template <
class To,
class From>
279 , std::enable_if_t<std::is_arithmetic_v<To> >
280 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*> >
283 To
operator()(
const std::string& value, utils::optional<To> fallback)
const
287 return lexical_cast_default<To>(std::string_view(value), *fallback);
289 return lexical_cast<To>(std::string_view(value));
To lexical_cast_default(From value, To fallback=To())
Lexical cast converts one type to another with a fallback.
To lexical_cast(From value)
Lexical cast converts one type to another.
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::enable_if_t< std::is_integral_v< T >, from_chars_result > from_chars(const char *first, const char *last, T &value, int base=10)
void trim_for_from_chars(std::string_view &v)
Thrown when a lexical_cast fails.
const char * what() const noexcept
To operator()(const std::string &value, utils::optional< To > fallback) const
To operator()(const std::string &value, utils::optional< To > fallback) const
To operator()(std::string_view str, utils::optional< To > fallback) const
bool operator()(std::string_view str, utils::optional< bool > fallback) const
std::string operator()(From value, utils::optional< std::string >) const
Base class for the conversion.
To operator()(From value, utils::optional< To > fallback) const
std::string to_string() const