alpaqa 1.0.0a15
Nonconvex constrained optimization
Loading...
Searching...
No Matches
from_chars-wrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <charconv>
4#include <stdexcept>
5#include <system_error>
6
7#if __cpp_lib_to_chars
8
9#define ALPAQA_USE_FROM_CHARS_INT 1
10#if defined(__clang__) // Clang
11#pragma message("Using std::stod as a fallback to replace std::from_chars")
12#define ALPAQA_USE_FROM_CHARS_FLOAT 0
13#elif defined(_MSC_VER) // MSVC
14#if _MSC_VER >= 1924
15#define ALPAQA_USE_FROM_CHARS_FLOAT 1
16#else
17#pragma message("Using std::stod as a fallback to replace std::from_chars")
18#define ALPAQA_USE_FROM_CHARS_FLOAT 0
19#endif
20#elif defined(__GNUC__) // GCC
21#if __GNUC__ >= 11
22#define ALPAQA_USE_FROM_CHARS_FLOAT 1
23#else
24#pragma message("Using std::stod as a fallback to replace std::from_chars")
25#define ALPAQA_USE_FROM_CHARS_FLOAT 0
26#endif
27#else // Unknown
28#pragma message("Unknown compiler: not using std::from_chars for floats")
29#define ALPAQA_USE_FROM_CHARS_FLOAT 0
30#endif
31
32#else // __cpp_lib_to_chars
33#define ALPAQA_USE_FROM_CHARS_INT 0
34#define ALPAQA_USE_FROM_CHARS_FLOAT 0
35#endif
36
37namespace alpaqa::util {
38
39template <class T>
40 requires(
41#if ALPAQA_USE_FROM_CHARS_FLOAT
42 std::floating_point<T> ||
43#endif
44 false) // NOLINT(readability-simplify-boolean-expr)
45std::from_chars_result
46from_chars(const char *first, const char *last, T &value,
47 std::chars_format fmt = std::chars_format::general) {
48 return std::from_chars(first, last, value, fmt);
49}
50
51template <class T>
52 requires(
53#if ALPAQA_USE_FROM_CHARS_INT
54 std::integral<T> ||
55#endif
56 false) // NOLINT(readability-simplify-boolean-expr)
57std::from_chars_result from_chars(const char *first, const char *last, T &value,
58 int base = 10) {
59 return std::from_chars(first, last, value, base);
60}
61
62template <class T, class... Args>
63 requires(
64#if !ALPAQA_USE_FROM_CHARS_FLOAT
65 std::floating_point<T> ||
66#endif
67 false) // NOLINT(readability-simplify-boolean-expr)
68std::from_chars_result from_chars(
69 const char *first, const char *last, T &value,
70 [[maybe_unused]] std::chars_format fmt = std::chars_format::general) {
71 size_t end_index = 0;
72 try {
73 if constexpr (std::is_same_v<T, float>)
74 value = std::stof(std::string(first, last), &end_index);
75 else if constexpr (std::is_same_v<T, double>)
76 value = std::stod(std::string(first, last), &end_index);
77 else if constexpr (std::is_same_v<T, long double>)
78 value = std::stold(std::string(first, last), &end_index);
79 else
80 static_assert(std::is_same_v<T, void>); // false
81 } catch (std::invalid_argument &e) {
82 return {
83 .ptr = first,
84 .ec = std::errc::invalid_argument,
85 };
86 } catch (std::out_of_range &e) {
87 return {
88 .ptr = first + end_index,
89 .ec = std::errc::result_out_of_range,
90 };
91 }
92 return {
93 .ptr = first + end_index,
94 .ec = std::errc(),
95 };
96}
97
98template <class T, class... Args>
99 requires(
100#if !ALPAQA_USE_FROM_CHARS_INT
101 std::integral<T> ||
102#endif
103 false) // NOLINT(readability-simplify-boolean-expr)
104std::from_chars_result from_chars(const char *first, const char *last, T &value,
105 int base = 10) {
106 size_t end_index = 0;
107 try {
108 if constexpr (std::is_same_v<T, signed char>)
109 value = static_cast<signed char>(
110 std::stoi(std::string(first, last), &end_index, base));
111 else if constexpr (std::is_same_v<T, short>)
112 value = static_cast<short>(
113 std::stoi(std::string(first, last), &end_index, base));
114 else if constexpr (std::is_same_v<T, int>)
115 value = std::stoi(std::string(first, last), &end_index, base);
116 else if constexpr (std::is_same_v<T, long>)
117 value = std::stol(std::string(first, last), &end_index, base);
118 else if constexpr (std::is_same_v<T, long long>)
119 value = std::stoll(std::string(first, last), &end_index, base);
120 else if constexpr (std::is_same_v<T, unsigned char>)
121 value = static_cast<unsigned char>(
122 std::stoul(std::string(first, last), &end_index, base));
123 else if constexpr (std::is_same_v<T, unsigned short>)
124 value = static_cast<unsigned short>(
125 std::stoul(std::string(first, last), &end_index, base));
126 else if constexpr (std::is_same_v<T, unsigned int>)
127 value = static_cast<unsigned int>(
128 std::stoul(std::string(first, last), &end_index, base));
129 else if constexpr (std::is_same_v<T, unsigned long>)
130 value = std::stoul(std::string(first, last), &end_index, base);
131 else if constexpr (std::is_same_v<T, unsigned long long>)
132 value = std::stoull(std::string(first, last), &end_index, base);
133 else
134 static_assert(std::is_same_v<T, void>); // false
135 } catch (std::invalid_argument &e) {
136 return {
137 .ptr = first,
138 .ec = std::errc::invalid_argument,
139 };
140 } catch (std::out_of_range &e) {
141 return {
142 .ptr = first + end_index,
143 .ec = std::errc::result_out_of_range,
144 };
145 }
146 return {
147 .ptr = first + end_index,
148 .ec = std::errc(),
149 };
150}
151} // namespace alpaqa::util
std::from_chars_result from_chars(const char *first, const char *last, T &value, std::chars_format fmt=std::chars_format::general)
constexpr const auto inf
Definition config.hpp:85