MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlRecordBinding.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::IO {
6
7template <typename Node>
9
10template <>
12 static std::string_view name(const XmlElementView& node) { return node.name(); }
13 static uint64_t row(const XmlElementView& node) { return node.row(); }
14
15 static std::optional<std::string_view> attribute(const XmlElementView& node,
16 std::string_view name)
17 {
18 return node.attribute(name);
19 }
20
21 template <typename Visitor>
22 static bool for_each_attribute(const XmlElementView& node, Visitor&& visit)
23 {
24 return node.for_each_field([&](const XmlFieldView& field) {
25 return field.kind == XmlFieldKind::Element || visit(field.name, field.value);
26 });
27 }
28
29 template <typename Visitor>
30 static bool for_each_child(const XmlElementView& node, Visitor&& visit)
31 {
32 return node.for_each_field([&](const XmlFieldView& field) {
33 return field.kind == XmlFieldKind::Attribute || visit(*field.element);
34 });
35 }
36};
37
38/** @brief Bind described attributes and configured XML child collections. */
39template <Reflect::Described Input, typename Node,
40 typename Access = XmlRecordAccess<Node>>
42public:
43 XmlRecordBinding& required(std::string name)
44 {
45 m_required.push_back(std::move(name));
46 return *this;
47 }
48
49 /**
50 * @brief Leave these attributes to other readers of the same element.
51 *
52 * Listed names are neither decoded nor rejected. Described members still
53 * decode strictly, so do not list one.
54 */
55 XmlRecordBinding& ignore(std::initializer_list<std::string_view> names)
56 {
57 for (const auto name : names)
58 m_ignored.emplace_back(name);
59 return *this;
60 }
61
62 template <auto Pointer>
63 XmlRecordBinding& repeated(std::string child_name,
64 std::string defaults_name = {},
65 std::string required_item_attribute = {},
66 size_t minimum = 0)
67 {
68 using Values = std::remove_cvref_t<decltype(std::declval<Input&>().*Pointer)>;
69 using Item = typename Values::value_type;
70 static_assert(std::same_as<Values, std::vector<Item>>);
71 static_assert(Reflect::Described<Item>);
72
73 ChildBinding binding;
74 binding.name = std::move(child_name);
75 binding.defaults_name = std::move(defaults_name);
76 binding.decode = [name = binding.name, defaults_name = binding.defaults_name,
77 required_item_attribute = std::move(required_item_attribute), minimum](
78 Input& input, const Node& node, std::string& error,
79 uint64_t* error_row) {
80 Item defaults {};
81 bool defaults_seen = false;
82 size_t count = 0;
83 auto& values = input.*Pointer;
84 const bool traversed = Access::for_each_child(node, [&](const Node& child) {
85 if (error_row)
86 *error_row = Access::row(child);
87
88 const auto child_name = Access::name(child);
89 if (!defaults_name.empty() && child_name == defaults_name) {
90 if (defaults_seen || count != 0) {
91 error = "<" + defaults_name + "> must appear once before the "
92 + name + " children";
93 return false;
94 }
95 defaults_seen = true;
96 if (!required_item_attribute.empty()
97 && Access::attribute(child, required_item_attribute)) {
98 error = "<" + defaults_name + "> has an unsupported attribute "
99 + required_item_attribute;
100 return false;
101 }
102 if (has_children(child)) {
103 error = "<" + defaults_name + "> may not have children";
104 return false;
105 }
106 return overlay(defaults, child, error);
107 }
108 if (child_name != name)
109 return true;
110 if (!required_item_attribute.empty()
111 && !Access::attribute(child, required_item_attribute)) {
112 error = "<" + label(child) + "> needs a "
113 + required_item_attribute + " attribute";
114 return false;
115 }
116 if (has_children(child)) {
117 error = "<" + name + "> may not have children";
118 return false;
119 }
120 Item item = defaults;
121 if (!overlay(item, child, error))
122 return false;
123 values.push_back(std::move(item));
124 ++count;
125 return true;
126 });
127 if (!traversed)
128 return false;
129 if (count < minimum) {
130 if (error_row)
131 *error_row = Access::row(node);
132 error = "<" + std::string(Access::name(node)) + "> needs at least "
133 + std::to_string(minimum) + " " + name + " children";
134 return false;
135 }
136 return true;
137 };
138 m_children.push_back(std::move(binding));
139 return *this;
140 }
141
142 [[nodiscard]] std::optional<Input> read(const Node& node,
143 std::string& error, uint64_t* error_row = nullptr) const
144 {
145 if (error_row)
146 *error_row = Access::row(node);
147 for (const auto& name : m_required) {
148 if (!Access::attribute(node, name)) {
149 error = "<" + label(node) + "> needs a " + name + " attribute";
150 return std::nullopt;
151 }
152 }
153
154 Input input {};
155 const bool overlaid = overlay_xml_attributes(input,
156 [&](const auto& visit) {
157 return Access::for_each_attribute(node,
158 [&](std::string_view name, std::string_view value) {
159 return std::ranges::find(m_ignored, name) != m_ignored.end()
160 || visit(name, value);
161 });
162 }, label(node), error);
163 if (!overlaid)
164 return std::nullopt;
165 const bool known_children = Access::for_each_child(node, [&](const Node& child) {
166 const auto child_name = Access::name(child);
167 const bool known = std::ranges::any_of(m_children, [&](const ChildBinding& binding) {
168 return child_name == binding.name
169 || (!binding.defaults_name.empty() && child_name == binding.defaults_name);
170 });
171 if (known)
172 return true;
173 if (error_row)
174 *error_row = Access::row(child);
175 error = "<" + std::string(child_name) + "> is not allowed in <"
176 + std::string(Access::name(node)) + ">";
177 return false;
178 });
179 if (!known_children)
180 return std::nullopt;
181 for (const ChildBinding& binding : m_children) {
182 if (!binding.decode(input, node, error, error_row))
183 return std::nullopt;
184 }
185 return input;
186 }
187
188private:
189 template <Reflect::Described T>
190 static bool overlay(T& target, const Node& node, std::string& error)
191 {
192 return overlay_xml_attributes(target,
193 [&](const auto& visit) {
194 return Access::for_each_attribute(node, visit);
195 }, label(node), error);
196 }
197
198 static std::string label(const Node& node)
199 {
200 const auto id = Access::attribute(node, "id");
201 return id ? std::string(*id) : std::string(Access::name(node));
202 }
203
204 static bool has_children(const Node& node)
205 {
206 bool found = false;
207 Access::for_each_child(node, [&](const Node&) {
208 found = true;
209 return false;
210 });
211 return found;
212 }
213
215 std::string name;
216 std::string defaults_name;
217 std::function<bool(Input&, const Node&, std::string&, uint64_t*)> decode;
218 };
219
220 std::vector<std::string> m_required;
221 std::vector<std::string> m_ignored;
222 std::vector<ChildBinding> m_children;
223};
224
225}
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
std::optional< std::string_view > attribute(std::string_view name) const noexcept
Definition XmlReader.cpp:30
Borrowed XML element with named attributes and immediate child elements.
Definition XmlReader.hpp:30
std::optional< Input > read(const Node &node, std::string &error, uint64_t *error_row=nullptr) const
static bool has_children(const Node &node)
std::vector< ChildBinding > m_children
XmlRecordBinding & repeated(std::string child_name, std::string defaults_name={}, std::string required_item_attribute={}, size_t minimum=0)
std::vector< std::string > m_required
XmlRecordBinding & required(std::string name)
std::vector< std::string > m_ignored
static bool overlay(T &target, const Node &node, std::string &error)
static std::string label(const Node &node)
XmlRecordBinding & ignore(std::initializer_list< std::string_view > names)
Leave these attributes to other readers of the same element.
Bind described attributes and configured XML child collections.
A type whose generated sidecar defines Generated::Description<T>::members().
Definition Describe.hpp:86
bool overlay_xml_attributes(T &target, Attributes &&attributes, std::string_view label, std::string &error)
Overlay XML text attributes onto a described value.
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).
Definition Analysis.cpp:506
std::string_view name
Definition XmlReader.hpp:66
std::string_view value
Definition XmlReader.hpp:67
std::optional< XmlElementView > element
Definition XmlReader.hpp:69
One attribute or immediate child element borrowed from an XML element.
Definition XmlReader.hpp:64
static bool for_each_child(const XmlElementView &node, Visitor &&visit)
static uint64_t row(const XmlElementView &node)
static std::string_view name(const XmlElementView &node)
static bool for_each_attribute(const XmlElementView &node, Visitor &&visit)
static std::optional< std::string_view > attribute(const XmlElementView &node, std::string_view name)
std::function< bool(Input &, const Node &, std::string &, uint64_t *)> decode