MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlElementBinding.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "XmlReader.hpp"
4
7
8namespace MayaFlux::IO {
9
10template <Reflect::Described T>
11bool overlay_xml_element(T& target, const XmlElementView& element,
12 std::string& error, uint64_t* error_row = nullptr);
13
14/** @brief Decode one XML child element into a described member. */
15template <typename T>
16struct XmlElementCodec { };
17
18template <Reflect::Described T>
20 static bool decode(const XmlElementView& element, T& value,
21 std::string& error, uint64_t* error_row)
22 {
23 return overlay_xml_element(value, element, error, error_row);
24 }
25};
26
27template <Reflect::Described T>
28struct XmlElementCodec<std::vector<T>> {
29 static bool decode(const XmlElementView& element, std::vector<T>& value,
30 std::string& error, uint64_t* error_row)
31 {
32 T item {};
33 if (!overlay_xml_element(item, element, error, error_row))
34 return false;
35 value.push_back(std::move(item));
36 return true;
37 }
38};
39
40template <Reflect::Described T>
41struct XmlElementCodec<std::optional<T>> {
42 static bool decode(const XmlElementView& element, std::optional<T>& value,
43 std::string& error, uint64_t* error_row)
44 {
45 T item = value.value_or(T {});
46 if (!overlay_xml_element(item, element, error, error_row))
47 return false;
48 value = std::move(item);
49 return true;
50 }
51};
52
53template <typename T>
54concept XmlElementDecodable = requires(const XmlElementView& element, T& value,
55 std::string& error, uint64_t* error_row) {
56 { XmlElementCodec<T>::decode(element, value, error, error_row) }
57 -> std::same_as<bool>;
58};
59
60/** @brief Overlay XML attributes and same-named children onto a described value. */
61template <Reflect::Described T>
62bool overlay_xml_element(T& target, const XmlElementView& element,
63 std::string& error, uint64_t* error_row)
64{
65 if (error_row)
66 *error_row = element.row();
68 [&](const auto& visit) {
69 return element.for_each_field([&](const XmlFieldView& field) {
70 if (error_row)
71 *error_row = field.row;
72 return visit(field.name, field);
73 });
74 },
75 [&](std::string_view name, const XmlFieldView& field, auto& value) {
76 using Value = std::remove_cvref_t<decltype(value)>;
77 if (field.kind == XmlFieldKind::Attribute)
78 return Reflect::decode_text_field(name, field.value, value, element.name(), error);
79 if constexpr (XmlElementDecodable<Value>)
80 return XmlElementCodec<Value>::decode(*field.element, value, error, error_row);
81 error = "<" + std::string(element.name()) + "> has an unsupported child <"
82 + std::string(name) + ">";
83 return false;
84 },
85 [&](std::string_view name) {
86 error = "<" + std::string(element.name()) + "> has an unsupported field "
87 + std::string(name);
88 return false;
89 });
90}
91
92}
std::string_view name() const noexcept
Definition XmlReader.cpp:24
uint64_t row() const noexcept
Definition XmlReader.hpp:32
bool for_each_field(Fn &&visitor) const
Visit attributes and immediate child elements in document order.
Definition XmlReader.hpp:73
Borrowed XML element with named attributes and immediate child elements.
Definition XmlReader.hpp:30
bool overlay_xml_element(T &target, const XmlElementView &element, std::string &error, uint64_t *error_row=nullptr)
Overlay XML attributes and same-named children onto a described value.
bool decode_text_field(std::string_view name, std::string_view source, T &value, std::string_view label, std::string &error)
Decode one authored text value into a described member.
bool overlay_described_fields(T &target, Fields &&fields, Decode &&decode, Unknown &&unknown)
Apply present named fields to a described value.
static bool decode(const XmlElementView &element, T &value, std::string &error, uint64_t *error_row)
static bool decode(const XmlElementView &element, std::optional< T > &value, std::string &error, uint64_t *error_row)
static bool decode(const XmlElementView &element, std::vector< T > &value, std::string &error, uint64_t *error_row)
Decode one XML child element into a described member.
std::string_view name
Definition XmlReader.hpp:66
One attribute or immediate child element borrowed from an XML element.
Definition XmlReader.hpp:64