7template <
typename Node>
16 std::string_view
name)
21 template <
typename Visitor>
29 template <
typename Visitor>
40 typename Access = XmlRecordAccess<Node>>
57 for (
const auto name : names)
62 template <auto Po
inter>
64 std::string defaults_name = {},
65 std::string required_item_attribute = {},
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>>);
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) {
81 bool defaults_seen =
false;
83 auto& values = input.*Pointer;
84 const bool traversed = Access::for_each_child(node, [&](
const Node& child) {
86 *error_row = Access::row(child);
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 "
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;
103 error =
"<" + defaults_name +
"> may not have children";
106 return overlay(defaults, child, error);
108 if (child_name != name)
110 if (!required_item_attribute.empty()
111 && !Access::attribute(child, required_item_attribute)) {
113 + required_item_attribute +
" attribute";
117 error =
"<" + name +
"> may not have children";
120 Item item = defaults;
121 if (!
overlay(item, child, error))
123 values.push_back(std::move(item));
129 if (count < minimum) {
131 *error_row = Access::row(node);
132 error =
"<" + std::string(Access::name(node)) +
"> needs at least "
133 + std::to_string(minimum) +
" " + name +
" children";
142 [[nodiscard]] std::optional<Input>
read(
const Node& node,
143 std::string& error, uint64_t* error_row =
nullptr)
const
146 *error_row = Access::row(node);
148 if (!Access::attribute(node, name)) {
149 error =
"<" +
label(node) +
"> needs a " + name +
" attribute";
156 [&](
const auto& visit) {
157 return Access::for_each_attribute(node,
158 [&](std::string_view name, std::string_view value) {
160 || visit(name, value);
162 },
label(node), error);
165 const bool known_children = Access::for_each_child(node, [&](
const Node& child) {
166 const auto child_name = Access::name(child);
168 return child_name == binding.
name
174 *error_row = Access::row(child);
175 error =
"<" + std::string(child_name) +
"> is not allowed in <"
176 + std::string(Access::name(node)) +
">";
182 if (!binding.decode(input, node, error, error_row))
189 template <Reflect::Described T>
190 static bool overlay(
T& target,
const Node& node, std::string& error)
193 [&](
const auto& visit) {
194 return Access::for_each_attribute(node, visit);
195 },
label(node), error);
198 static std::string
label(
const Node& node)
200 const auto id = Access::attribute(node,
"id");
201 return id ? std::string(*
id) : std::string(Access::name(node));
207 Access::for_each_child(node, [&](
const Node&) {
217 std::function<bool(Input&,
const Node&, std::string&, uint64_t*)>
decode;
std::string_view name() const noexcept
uint64_t row() const noexcept
bool for_each_field(Fn &&visitor) const
Visit attributes and immediate child elements in document order.
std::optional< std::string_view > attribute(std::string_view name) const noexcept
Borrowed XML element with named attributes and immediate child elements.
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().
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).
std::optional< XmlElementView > element
One attribute or immediate child element borrowed from an XML element.
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
std::string defaults_name