MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlTextCodec.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <charconv>
6
7namespace MayaFlux::IO {
8
10
11template <typename T>
12 requires(std::is_integral_v<T> || std::is_floating_point_v<T>)
13[[nodiscard]] std::optional<std::vector<T>> parse_xml_numbers(std::string_view source)
14{
15 const auto whitespace = [](char value) {
16 return value == ' ' || value == '\t' || value == '\n' || value == '\r';
17 };
18
19 std::vector<T> values;
20 const char* current = source.data();
21 const char* end = current + source.size();
22 while (current != end) {
23 while (current != end && whitespace(*current))
24 ++current;
25 if (current == end)
26 break;
27
28 const bool positive_sign = *current == '+';
29 if (positive_sign)
30 ++current;
31
32 T value {};
33 const auto [next, status] = std::from_chars(current, end, value);
34 if ((positive_sign && (current == end || *current == '+' || *current == '-'))
35 || status != std::errc {} || next == current
36 || (std::is_floating_point_v<T> && !std::isfinite(value))
37 || (next != end && !whitespace(*next))) {
38 return std::nullopt;
39 }
40 values.push_back(value);
41 current = next;
42 }
43 return values;
44}
45
46template <typename T, size_t N>
47[[nodiscard]] std::optional<std::array<T, N>> parse_xml_tuple(std::string_view source,
48 XmlTextError& error)
49{
50 const auto values = parse_xml_numbers<T>(source);
51 if (!values) {
53 return std::nullopt;
54 }
55 if (values->size() != N) {
57 return std::nullopt;
58 }
59 std::array<T, N> result;
60 std::ranges::copy(*values, result.begin());
61 return result;
62}
63
64/** @brief Convert XML attribute text to one described member value. */
65template <typename T>
66struct XmlTextCodec { };
67
68template <typename T>
69 requires((std::is_integral_v<T> || std::is_floating_point_v<T>)
70 && !std::same_as<T, bool>)
72 static constexpr size_t extent = 1;
73
74 static bool decode(std::string_view source, T& value, XmlTextError& error)
75 {
76 const auto decoded = parse_xml_tuple<T, 1>(source, error);
77 if (!decoded)
78 return false;
79 value = (*decoded)[0];
80 return true;
81 }
82};
83
84template <>
85struct XmlTextCodec<bool> {
86 static constexpr size_t extent = 1;
87
88 static bool decode(std::string_view source, bool& value, XmlTextError& error)
89 {
90 if (source == "true" || source == "1") {
91 value = true;
92 return true;
93 }
94 if (source == "false" || source == "0") {
95 value = false;
96 return true;
97 }
99 return false;
100 }
101};
102
103template <>
104struct XmlTextCodec<std::string> {
105 static constexpr size_t extent = 0;
106
107 static bool decode(std::string_view source, std::string& value, XmlTextError&)
108 {
109 value = source;
110 return true;
111 }
112};
113
114template <typename T>
115 requires std::is_enum_v<T>
116struct XmlTextCodec<T> {
117 static constexpr size_t extent = 0;
118
119 static bool decode(std::string_view source, T& value, XmlTextError& error)
120 {
121 const auto decoded = Reflect::string_to_enum<T>(source);
122 if (!decoded) {
124 return false;
125 }
126 value = *decoded;
127 return true;
128 }
129};
130
131template <GlmVectorType T>
132struct XmlTextCodec<T> {
133 static constexpr size_t extent = glm_component_count<T>();
134
135 static bool decode(std::string_view source, T& value, XmlTextError& error)
136 {
137 using Scalar = typename T::value_type;
138 constexpr size_t count = glm_component_count<T>();
139 const auto decoded = parse_xml_tuple<Scalar, count>(source, error);
140 if (!decoded)
141 return false;
142 for (size_t i = 0; i < count; ++i)
143 value[static_cast<glm::length_t>(i)] = (*decoded)[i];
144 return true;
145 }
146};
147
148template <typename T>
149concept XmlTextDecodable = requires(std::string_view source, T& value,
150 XmlTextError& error) {
151 { XmlTextCodec<T>::decode(source, value, error) } -> std::same_as<bool>;
152};
153
154template <XmlTextDecodable T>
155struct XmlTextCodec<std::optional<T>> {
156 static constexpr size_t extent = XmlTextCodec<T>::extent;
157
158 static bool decode(std::string_view source, std::optional<T>& value,
159 XmlTextError& error)
160 {
161 T decoded = value.value_or(T {});
162 if (!XmlTextCodec<T>::decode(source, decoded, error))
163 return false;
164 value = std::move(decoded);
165 return true;
166 }
167};
168
169}
std::optional< std::array< T, N > > parse_xml_tuple(std::string_view source, XmlTextError &error)
std::optional< std::vector< T > > parse_xml_numbers(std::string_view source)
constexpr std::optional< EnumType > string_to_enum(std::string_view str) noexcept
Universal string to enum converter using magic_enum (exact case match).
static constexpr size_t extent
static bool decode(std::string_view source, T &value, XmlTextError &error)
static bool decode(std::string_view source, bool &value, XmlTextError &error)
static constexpr size_t extent
static bool decode(std::string_view source, std::optional< T > &value, XmlTextError &error)
static bool decode(std::string_view source, std::string &value, XmlTextError &)
Convert XML attribute text to one described member value.