MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
XmlAttributeBinding.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "XmlReader.hpp"
4#include "XmlTextCodec.hpp"
5
7
8namespace MayaFlux::IO {
9
10template <typename T>
11bool decode_xml_attribute(std::string_view name, std::string_view source,
12 T& value, std::string_view label, std::string& error)
13{
14 if constexpr (XmlTextDecodable<T>) {
16 if (XmlTextCodec<T>::decode(source, value, reason))
17 return true;
18 if (reason == XmlTextError::Cardinality) {
19 error = "<" + std::string(label) + "> needs "
20 + std::to_string(XmlTextCodec<T>::extent) + " numbers in "
21 + std::string(name);
22 } else if (reason == XmlTextError::Unsupported) {
23 error = "<" + std::string(label) + "> has an unsupported "
24 + std::string(name) + " " + std::string(source);
25 } else {
26 error = "<" + std::string(label) + "> has an invalid "
27 + std::string(name) + " attribute";
28 }
29 } else {
30 error = "<" + std::string(label) + "> has an unsupported attribute "
31 + std::string(name);
32 }
33 return false;
34}
35
36/** @brief Overlay XML text attributes onto a described value. */
37template <Reflect::Described T, typename Attributes>
38bool overlay_xml_attributes(T& target, Attributes&& attributes,
39 std::string_view label, std::string& error)
40{
42 [&](const auto& visit) {
43 return attributes([&](std::string_view name, std::string_view value) {
44 return visit(name, value);
45 });
46 },
47 [&](std::string_view name, std::string_view source, auto& value) {
48 return decode_xml_attribute(name, source, value, label, error);
49 },
50 [&](std::string_view name) {
51 error = "<" + std::string(label) + "> has an unsupported attribute "
52 + std::string(name);
53 return false;
54 });
55}
56
57/** @brief Overlay an XML element's attributes onto a described value. */
58template <Reflect::Described T>
59bool overlay_xml_attributes(T& target, const XmlElementView& element,
60 std::string& error)
61{
62 return overlay_xml_attributes(target,
63 [&](const auto& visit) {
64 return element.for_each_field([&](const XmlFieldView& field) {
65 return field.kind == XmlFieldKind::Element
66 || visit(field.name, field.value);
67 });
68 }, element.name(), error);
69}
70
71}
std::string_view name() const noexcept
Definition XmlReader.cpp:24
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 decode_xml_attribute(std::string_view name, std::string_view source, T &value, std::string_view label, std::string &error)
bool overlay_xml_attributes(T &target, Attributes &&attributes, std::string_view label, std::string &error)
Overlay XML text attributes onto a described value.
bool overlay_described_fields(T &target, Fields &&fields, Decode &&decode, Unknown &&unknown)
Apply present named fields to a described value.
std::string_view name
Definition XmlReader.hpp:66
std::string_view value
Definition XmlReader.hpp:67
One attribute or immediate child element borrowed from an XML element.
Definition XmlReader.hpp:64
Convert XML attribute text to one described member value.