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;
51 #include <type_traits>
53 #define DEBUG_THROW(id)
66 ,
typename ToEnable = void
67 ,
typename FromEnable =
void
85 template<
typename To,
typename From>
102 template<
typename To,
typename From>
111 const char*
what() const noexcept
113 return "bad_lexical_cast";
134 ,
typename FromEnable
138 To
operator()(From value, utils::optional<To> fallback)
const
143 std::stringstream sstr;
145 if(!(sstr << value && sstr >> result)) {
146 if(fallback) {
return *fallback; }
161 template <
typename From>
166 , std::enable_if_t<std::is_integral_v<std::remove_pointer_t<From>>>
169 std::string
operator()(From value, utils::optional<std::string>)
const
171 DEBUG_THROW(
"specialized - To std::string - From integral (pointer)");
173 std::stringstream sstr;
186 template <
class From>
191 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
194 long long operator()(From value, utils::optional<long long> fallback)
const
196 DEBUG_THROW(
"specialized - To long long - From (const) char*");
199 return lexical_cast_default<long long>(std::string(value), *fallback);
201 return lexical_cast<long long>(std::string(value));
219 long long operator()(
const std::string& value, utils::optional<long long> fallback)
const
221 DEBUG_THROW(
"specialized - To long long - From std::string");
232 return std::stoll(value);
233 }
catch(
const std::invalid_argument&) {
234 }
catch(
const std::out_of_range&) {
250 template <
class To,
class From>
254 , std::enable_if_t<std::is_integral_v<To> && std::is_signed_v<To> && !std::is_same_v<To, long long>>
255 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
258 To
operator()(From value, utils::optional<To> fallback)
const
260 DEBUG_THROW(
"specialized - To signed - From (const) char*");
263 return lexical_cast_default<To>(std::string(value), *fallback);
265 return lexical_cast<To>(std::string(value));
279 , std::enable_if_t<std::is_integral_v<To> && std::is_signed_v<To> && !std::is_same_v<To, long long>>
282 To
operator()(
const std::string& value, utils::optional<To> fallback)
const
284 DEBUG_THROW(
"specialized - To signed - From std::string");
295 long res = std::stol(value);
296 if(std::numeric_limits<To>::lowest() <= res && std::numeric_limits<To>::max() >= res) {
297 return static_cast<To
>(res);
299 }
catch(
const std::invalid_argument&) {
300 }
catch(
const std::out_of_range&) {
316 template <
class To,
class From>
320 , std::enable_if_t<std::is_floating_point_v<To>>
321 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
324 To
operator()(From value, utils::optional<To> fallback)
const
326 DEBUG_THROW(
"specialized - To floating point - From (const) char*");
329 return lexical_cast_default<To>(std::string(value), *fallback);
331 return lexical_cast<To>(std::string(value));
345 , std::enable_if_t<std::is_floating_point_v<To>>
348 To
operator()(
const std::string& value, utils::optional<To> fallback)
const
350 DEBUG_THROW(
"specialized - To floating point - From std::string");
361 if(value.find_first_of(
"Xx") != std::string::npos) {
370 long double res = std::stold(value);
371 if((
static_cast<long double>(std::numeric_limits<To>::lowest()) <= res) && (
static_cast<long double>(std::numeric_limits<To>::max()) >= res)) {
372 return static_cast<To
>(res);
374 }
catch(
const std::invalid_argument&) {
375 }
catch(
const std::out_of_range&) {
393 template <
class From>
398 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
401 unsigned long long operator()(From value, utils::optional<unsigned long long> fallback)
const
404 "specialized - To unsigned long long - From (const) char*");
407 return lexical_cast_default<unsigned long long>(std::string(value), *fallback);
409 return lexical_cast<unsigned long long>(std::string(value));
427 unsigned long long operator()(
const std::string& value, utils::optional<unsigned long long> fallback)
const
429 DEBUG_THROW(
"specialized - To unsigned long long - From std::string");
440 return std::stoull(value);
441 }
catch(
const std::invalid_argument&) {
442 }
catch(
const std::out_of_range&) {
458 template <
class To,
class From>
462 , std::enable_if_t<std::is_unsigned_v<To> && !std::is_same_v<To, unsigned long long>>
463 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
466 To
operator()(From value, utils::optional<To> fallback)
const
468 DEBUG_THROW(
"specialized - To unsigned - From (const) char*");
471 return lexical_cast_default<To>(std::string(value), *fallback);
473 return lexical_cast<To>(std::string(value));
487 , std::enable_if_t<std::is_unsigned_v<To>>
490 To
operator()(
const std::string& value, utils::optional<To> fallback)
const
492 DEBUG_THROW(
"specialized - To unsigned - From std::string");
503 unsigned long res = std::stoul(value);
505 if(std::numeric_limits<To>::max() >= res) {
506 return static_cast<To
>(res);
508 }
catch(
const std::invalid_argument&) {
509 }
catch(
const std::out_of_range&) {
529 bool operator()(
const std::string& value, utils::optional<bool>)
const
531 DEBUG_THROW(
"specialized - To bool - From std::string");
543 template <
class From>
548 , std::enable_if_t<std::is_same_v<From, const char*> || std::is_same_v<From, char*>>
553 DEBUG_THROW(
"specialized - To bool - From (const) char*");
555 return lexical_cast<bool>(std::string(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.
Thrown when a lexical_cast fails.
const char * what() const noexcept
To operator()(From value, utils::optional< To > fallback) const
To operator()(From value, utils::optional< To > fallback) const
To operator()(From value, utils::optional< To > fallback) const
To operator()(const std::string &value, utils::optional< To > fallback) const
To operator()(const std::string &value, utils::optional< To > fallback) const
To operator()(const std::string &value, utils::optional< To > fallback) const
bool operator()(From value, utils::optional< bool >) const
bool operator()(const std::string &value, utils::optional< bool >) const
long long operator()(From value, utils::optional< long long > fallback) const
long long operator()(const std::string &value, utils::optional< long long > fallback) const
std::string operator()(From value, utils::optional< std::string >) const
unsigned long long operator()(From value, utils::optional< unsigned long long > fallback) const
unsigned long long operator()(const std::string &value, utils::optional< unsigned long long > fallback) const
Base class for the conversion.
To operator()(From value, utils::optional< To > fallback) const