5#if __has_include(<charconv>)
9#if __cpp_lib_to_chars && (!defined(__clang__)) && \
10 (!defined(__GNUC__) || __GNUC__ > 1)
11#define ALPAQA_USE_FROM_CHARS_FLOAT 1
13#pragma message "Using std::stod as a fallback to replace std::from_chars"
14#define ALPAQA_USE_FROM_CHARS_FLOAT 0
16#define ALPAQA_USE_FROM_CHARS_INT 1
23#if ALPAQA_USE_FROM_CHARS_FLOAT
24 std::floating_point<T> ||
26#if ALPAQA_USE_FROM_CHARS_INT
30const char *set_param_float_int(T &f,
ParamString s) {
31 const auto *val_end = s.
value.data() + s.
value.size();
32 auto [ptr, ec] = std::from_chars(s.
value.data(), val_end, f);
33 if (ec != std::errc())
34 throw std::invalid_argument(
35 "Invalid value '" + std::string(s.
value) +
"' for type '" +
37 "': " + std::make_error_code(ec).message());
42#if !ALPAQA_USE_FROM_CHARS_FLOAT
43 std::floating_point<T> ||
45#if !ALPAQA_USE_FROM_CHARS_INT
49const char *set_param_float_int(T &f,
ParamString s) {
52 if constexpr (std::is_same_v<T, float>)
53 f = std::stof(std::string(s.
value), &end_index);
54 else if constexpr (std::is_same_v<T, double>)
55 f = std::stod(std::string(s.
value), &end_index);
56 else if constexpr (std::is_same_v<T, long double>)
57 f = std::stold(std::string(s.
value), &end_index);
58 else if constexpr (std::is_same_v<T, signed char>)
59 f =
static_cast<signed char>(
60 std::stoi(std::string(s.
value), &end_index, 0));
61 else if constexpr (std::is_same_v<T, short>)
62 f =
static_cast<short>(
63 std::stoi(std::string(s.
value), &end_index, 0));
64 else if constexpr (std::is_same_v<T, int>)
65 f = std::stoi(std::string(s.
value), &end_index, 0);
66 else if constexpr (std::is_same_v<T, long>)
67 f = std::stol(std::string(s.
value), &end_index, 0);
68 else if constexpr (std::is_same_v<T, long long>)
69 f = std::stoll(std::string(s.
value), &end_index, 0);
70 else if constexpr (std::is_same_v<T, unsigned char>)
71 f =
static_cast<unsigned char>(
72 std::stoul(std::string(s.
value), &end_index, 0));
73 else if constexpr (std::is_same_v<T, unsigned short>)
74 f =
static_cast<unsigned short>(
75 std::stoul(std::string(s.
value), &end_index, 0));
76 else if constexpr (std::is_same_v<T, unsigned int>)
77 f =
static_cast<unsigned int>(
78 std::stoul(std::string(s.
value), &end_index, 0));
79 else if constexpr (std::is_same_v<T, unsigned long>)
80 f = std::stoul(std::string(s.
value), &end_index, 0);
81 else if constexpr (std::is_same_v<T, unsigned long long>)
82 f = std::stoull(std::string(s.
value), &end_index, 0);
84 static_assert(std::is_same_v<T, void>);
85 }
catch (std::exception &e) {
86 throw std::invalid_argument(
"Invalid value '" + std::string(s.
value) +
89 std::string(s.
full_key) +
"': " + e.what());
91 return s.
value.data() + end_index;
std::string demangled_typename(const std::type_info &t)
Get the pretty name of the given type as a string.
std::string_view full_key
Full key string, used for diagnostics.
std::string_view value
The value of the parameter to store.
Represents a parameter value encoded as a string in the format abc.def.key=value.