MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TextCodec.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "EnumReflect.hpp"
4
5#include <charconv>
6
7namespace MayaFlux::Reflect {
8
10
11/**
12 * @brief Parse whitespace separated finite numbers.
13 * @return The values, or std::nullopt for any malformed or non-finite token.
14 */
15template <typename T>
16 requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
17[[nodiscard]] std::optional<std::vector<T>> parse_numbers(std::string_view source)
18{
19 const auto whitespace = [](char value) {
20 return value == ' ' || value == '\t' || value == '\n' || value == '\r';
21 };
22
23 std::vector<T> values;
24 const char* current = source.data();
25 const char* end = current + source.size();
26 while (current != end) {
27 while (current != end && whitespace(*current))
28 ++current;
29 if (current == end)
30 break;
31
32 const bool positive_sign = *current == '+';
33 if (positive_sign)
34 ++current;
35
36 T value {};
37 const auto [next, status] = std::from_chars(current, end, value);
38 if ((positive_sign && (current == end || *current == '+' || *current == '-'))
39 || status != std::errc {} || next == current
40 || (std::is_floating_point_v<T> && !std::isfinite(value))
41 || (next != end && !whitespace(*next))) {
42 return std::nullopt;
43 }
44 values.push_back(value);
45 current = next;
46 }
47 return values;
48}
49
50/**
51 * @brief Parse exactly N whitespace separated finite numbers.
52 * @param error Receives Invalid for a malformed token and Cardinality for a wrong count.
53 */
54template <typename T, size_t N>
55[[nodiscard]] std::optional<std::array<T, N>> parse_tuple(std::string_view source,
56 TextError& error)
57{
58 const auto values = parse_numbers<T>(source);
59 if (!values) {
60 error = TextError::Invalid;
61 return std::nullopt;
62 }
63 if (values->size() != N) {
65 return std::nullopt;
66 }
67 std::array<T, N> result;
68 std::ranges::copy(*values, result.begin());
69 return result;
70}
71
72/**
73 * @brief Conversion of authored text to one value type.
74 *
75 * Any text leaf can use it: a document attribute, a delimited cell, a
76 * command argument. Specialize for a type that needs another spelling.
77 * A specialization provides extent (expected component count, 0 when not
78 * numeric) and decode(source, value, error).
79 */
80template <typename T>
81struct TextCodec { };
82
83template <typename T>
84 requires((std::is_integral_v<T> || std::is_floating_point_v<T>)
85 && !std::same_as<T, bool>)
86struct TextCodec<T> {
87 static constexpr size_t extent = 1;
88
89 static bool decode(std::string_view source, T& value, TextError& error)
90 {
91 const auto decoded = parse_tuple<T, 1>(source, error);
92 if (!decoded)
93 return false;
94 value = (*decoded)[0];
95 return true;
96 }
97};
98
99template <>
100struct TextCodec<bool> {
101 static constexpr size_t extent = 1;
102
103 static bool decode(std::string_view source, bool& value, TextError& error)
104 {
105 if (source == "true" || source == "1") {
106 value = true;
107 return true;
108 }
109 if (source == "false" || source == "0") {
110 value = false;
111 return true;
112 }
113 error = TextError::Invalid;
114 return false;
115 }
116};
117
118template <>
119struct TextCodec<std::string> {
120 static constexpr size_t extent = 0;
121
122 static bool decode(std::string_view source, std::string& value, TextError&)
123 {
124 value = source;
125 return true;
126 }
127};
128
129template <typename T>
130 requires std::is_enum_v<T>
131struct TextCodec<T> {
132 static constexpr size_t extent = 0;
133
134 static bool decode(std::string_view source, T& value, TextError& error)
135 {
136 const auto decoded = string_to_enum<T>(source);
137 if (!decoded) {
139 return false;
140 }
141 value = *decoded;
142 return true;
143 }
144};
145
146/**
147 * @brief A sequence of numbers: every whitespace separated token is one element.
148 *
149 * Marked with sequence so diagnostics speak of groups.
150 */
151template <typename T>
152 requires((std::is_integral_v<T> || std::is_floating_point_v<T>)
153 && !std::same_as<T, bool>)
154struct TextCodec<std::vector<T>> {
155 static constexpr size_t extent = 1;
156 static constexpr bool sequence = true;
157
158 static bool decode(std::string_view source, std::vector<T>& value, TextError& error)
159 {
160 auto decoded = parse_numbers<T>(source);
161 if (!decoded) {
162 error = TextError::Invalid;
163 return false;
164 }
165 value = std::move(*decoded);
166 return true;
167 }
168};
169
170/**
171 * @brief A GLM vector as whitespace separated finite components, one per component.
172 */
173template <GlmVectorType T>
174struct TextCodec<T> {
175 static constexpr size_t extent = glm_component_count<T>();
176
177 static bool decode(std::string_view source, T& value, TextError& error)
178 {
179 using Scalar = typename T::value_type;
180 constexpr size_t count = glm_component_count<T>();
181 const auto decoded = parse_tuple<Scalar, count>(source, error);
182 if (!decoded)
183 return false;
184 for (size_t i = 0; i < count; ++i)
185 value[static_cast<glm::length_t>(i)] = (*decoded)[i];
186 return true;
187 }
188};
189
190/**
191 * @brief A sequence of GLM vectors: numbers are read in groups of the vector's component count.
192 *
193 * Reports Cardinality when the number of values is not a multiple of the
194 * component count. An empty source gives an empty sequence.
195 */
196template <GlmVectorType T>
197struct TextCodec<std::vector<T>> {
198 static constexpr size_t extent = glm_component_count<T>();
199 static constexpr bool sequence = true;
200
201 static bool decode(std::string_view source, std::vector<T>& value, TextError& error)
202 {
203 using Scalar = typename T::value_type;
204 constexpr size_t count = glm_component_count<T>();
205 const auto numbers = parse_numbers<Scalar>(source);
206 if (!numbers) {
207 error = TextError::Invalid;
208 return false;
209 }
210 if (numbers->size() % count != 0) {
212 return false;
213 }
214 std::vector<T> decoded(numbers->size() / count);
215 for (size_t i = 0; i < decoded.size(); ++i) {
216 for (size_t j = 0; j < count; ++j)
217 decoded[i][static_cast<glm::length_t>(j)] = (*numbers)[i * count + j];
218 }
219 value = std::move(decoded);
220 return true;
221 }
222};
223
224template <typename T>
225concept TextDecodable = requires(std::string_view source, T& value, TextError& error) {
226 { TextCodec<T>::decode(source, value, error) } -> std::same_as<bool>;
227};
228
229template <TextDecodable T>
230struct TextCodec<std::optional<T>> {
231 static constexpr size_t extent = TextCodec<T>::extent;
232
233 static bool decode(std::string_view source, std::optional<T>& value,
234 TextError& error)
235 {
236 T decoded = value.value_or(T {});
237 if (!TextCodec<T>::decode(source, decoded, error))
238 return false;
239 value = std::move(decoded);
240 return true;
241 }
242};
243
244}
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match).
std::optional< std::vector< T > > parse_numbers(std::string_view source)
Parse whitespace separated finite numbers.
Definition TextCodec.hpp:17
std::optional< std::array< T, N > > parse_tuple(std::string_view source, TextError &error)
Parse exactly N whitespace separated finite numbers.
Definition TextCodec.hpp:55
static bool decode(std::string_view source, T &value, TextError &error)
Definition TextCodec.hpp:89
static constexpr size_t extent
static bool decode(std::string_view source, bool &value, TextError &error)
static bool decode(std::string_view source, std::optional< T > &value, TextError &error)
static bool decode(std::string_view source, std::string &value, TextError &)
static bool decode(std::string_view source, std::vector< T > &value, TextError &error)
Conversion of authored text to one value type.
Definition TextCodec.hpp:81