alpaqa 1.0.0a13
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
10
11#define ALPAQA_USE_FROM_CHARS_INT 1
12#if defined(__clang__) // Clang
13#pragma message("Using std::stod as a fallback to replace std::from_chars")
14#define ALPAQA_USE_FROM_CHARS_FLOAT 0
15#elif defined(_MSC_VER) // MSVC
16#if _MSC_VER >= 1924
17#define ALPAQA_USE_FROM_CHARS_FLOAT 1
18#else
19#pragma message("Using std::stod as a fallback to replace std::from_chars")
20#define ALPAQA_USE_FROM_CHARS_FLOAT 0
21#endif
22#elif defined(__GNUC__) // GCC
23#if __GNUC__ >= 11
24#define ALPAQA_USE_FROM_CHARS_FLOAT 1
25#else
26#pragma message("Using std::stod as a fallback to replace std::from_chars")
27#define ALPAQA_USE_FROM_CHARS_FLOAT 0
28#endif
29#else // Unknown
30#pragma message("Unknown compiler: not using std::from_chars for floats")
31#define ALPAQA_USE_FROM_CHARS_FLOAT 0
32#endif
33
34#else // __cpp_lib_to_chars
35#define ALPAQA_USE_FROM_CHARS_INT 0
36#define ALPAQA_USE_FROM_CHARS_FLOAT 0
37#endif
38
39namespace {
40using namespace alpaqa::params;
41
42template <class T>
43 requires(
44#if ALPAQA_USE_FROM_CHARS_FLOAT
45 std::floating_point<T> ||
46#endif
47#if ALPAQA_USE_FROM_CHARS_INT
48 std::integral<T> ||
49#endif
50 false) // NOLINT(readability-simplify-boolean-expr)
51const char *set_param_float_int(T &f, ParamString s) {
52 const auto *val_end = s.value.data() + s.value.size();
53 auto [ptr, ec] = std::from_chars(s.value.data(), val_end, f);
54 if (ec != std::errc())
55 throw std::invalid_argument(
56 "Invalid value '" + std::string(s.value) + "' for type '" +
57 demangled_typename(typeid(T)) + "' in '" + std::string(s.full_key) +
58 "': " + std::make_error_code(ec).message());
59 return ptr;
60}
61template <class T>
62 requires(
63#if !ALPAQA_USE_FROM_CHARS_FLOAT
64 std::floating_point<T> ||
65#endif
66#if !ALPAQA_USE_FROM_CHARS_INT
67 std::integral<T> ||
68#endif
69 false) // NOLINT(readability-simplify-boolean-expr)
70const char *set_param_float_int(T &f, ParamString s) {
71 size_t end_index;
72 try {
73 if constexpr (std::is_same_v<T, float>)
74 f = std::stof(std::string(s.value), &end_index);
75 else if constexpr (std::is_same_v<T, double>)
76 f = std::stod(std::string(s.value), &end_index);
77 else if constexpr (std::is_same_v<T, long double>)
78 f = std::stold(std::string(s.value), &end_index);
79 else if constexpr (std::is_same_v<T, signed char>)
80 f = static_cast<signed char>(
81 std::stoi(std::string(s.value), &end_index, 0));
82 else if constexpr (std::is_same_v<T, short>)
83 f = static_cast<short>(
84 std::stoi(std::string(s.value), &end_index, 0));
85 else if constexpr (std::is_same_v<T, int>)
86 f = std::stoi(std::string(s.value), &end_index, 0);
87 else if constexpr (std::is_same_v<T, long>)
88 f = std::stol(std::string(s.value), &end_index, 0);
89 else if constexpr (std::is_same_v<T, long long>)
90 f = std::stoll(std::string(s.value), &end_index, 0);
91 else if constexpr (std::is_same_v<T, unsigned char>)
92 f = static_cast<unsigned char>(
93 std::stoul(std::string(s.value), &end_index, 0));
94 else if constexpr (std::is_same_v<T, unsigned short>)
95 f = static_cast<unsigned short>(
96 std::stoul(std::string(s.value), &end_index, 0));
97 else if constexpr (std::is_same_v<T, unsigned int>)
98 f = static_cast<unsigned int>(
99 std::stoul(std::string(s.value), &end_index, 0));
100 else if constexpr (std::is_same_v<T, unsigned long>)
101 f = std::stoul(std::string(s.value), &end_index, 0);
102 else if constexpr (std::is_same_v<T, unsigned long long>)
103 f = std::stoull(std::string(s.value), &end_index, 0);
104 else
105 static_assert(std::is_same_v<T, void>); // false
106 } catch (std::exception &e) {
107 throw std::invalid_argument("Invalid value '" + std::string(s.value) +
108 "' for type '" +
109 demangled_typename(typeid(T)) + "' in '" +
110 std::string(s.full_key) + "': " + e.what());
111 }
112 return s.value.data() + end_index;
113}
114} // 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
const char * set_param_float_int(T &f, ParamString s)