alpaqa 1.0.0a10
Nonconvex constrained optimization
Loading...
Searching...
No Matches
from_chars-compat.ipp
Go to the documentation of this file.
1#pragma once
2
5#if __has_include(<charconv>)
6#include <charconv>
7#endif
8
9#if __cpp_lib_to_chars && (!defined(__clang__)) && \
10 (!defined(__GNUC__) || __GNUC__ > 1)
11#define ALPAQA_USE_FROM_CHARS_FLOAT 1
12#else
13#pragma message "Using std::stod as a fallback to replace std::from_chars"
14#define ALPAQA_USE_FROM_CHARS_FLOAT 0
15#endif
16#define ALPAQA_USE_FROM_CHARS_INT 1
17
18namespace {
19using namespace alpaqa::params;
20
21template <class T>
22 requires(
23#if ALPAQA_USE_FROM_CHARS_FLOAT
24 std::floating_point<T> ||
25#endif
26#if ALPAQA_USE_FROM_CHARS_INT
27 std::integral<T> ||
28#endif
29 false) // NOLINT(readability-simplify-boolean-expr)
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 '" +
36 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
37 "': " + std::make_error_code(ec).message());
38 return ptr;
39}
40template <class T>
41 requires(
42#if !ALPAQA_USE_FROM_CHARS_FLOAT
43 std::floating_point<T> ||
44#endif
45#if !ALPAQA_USE_FROM_CHARS_INT
46 std::integral<T> ||
47#endif
48 false) // NOLINT(readability-simplify-boolean-expr)
49const char *set_param_float_int(T &f, ParamString s) {
50 size_t end_index;
51 try {
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);
83 else
84 static_assert(std::is_same_v<T, void>); // false
85 } catch (std::exception &e) {
86 throw std::invalid_argument("Invalid value '" + std::string(s.value) +
87 "' for type '" +
88 demangled_typename(typeid(T)) + "' in '" +
89 std::string(s.full_key) + "': " + e.what());
90 }
91 return s.value.data() + end_index;
92}
93} // namespace
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.
Definition: params.hpp:18
std::string_view value
The value of the parameter to store.
Definition: params.hpp:22
Represents a parameter value encoded as a string in the format abc.def.key=value.
Definition: params.hpp:16