Optimizationg for the reader and writer; #68

This commit is contained in:
Patrick Urbanke
2024-03-16 09:58:26 +01:00
parent 54785ec893
commit 58d89e717c
27 changed files with 738 additions and 556 deletions
+11 -34
View File
@@ -5,6 +5,7 @@
#include <exception>
#include <map>
#include <memory>
#include <optional>
#include <pugixml.hpp>
#include <sstream>
#include <stdexcept>
@@ -17,6 +18,7 @@
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../parsing/is_view_reader.hpp"
namespace rfl {
namespace xml {
@@ -119,50 +121,25 @@ struct Reader {
return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
}
template <size_t size, class FunctionType>
std::array<std::optional<InputVarType>, size> to_fields_array(
const FunctionType _fct, const InputObjectType _obj) const noexcept {
std::array<std::optional<InputVarType>, size> f_arr;
template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader,
const InputObjectType& _obj) const noexcept {
for (auto child = _obj.node_.first_child(); child;
child = child.next_sibling()) {
const auto name = child.name();
const auto ix = _fct(std::string_view(name));
// XML allows for duplicate node names, but we always want the first
// sibling.
if (ix != -1 && !f_arr[ix]) {
f_arr[ix] = InputVarType(child);
}
_object_reader.read(std::string_view(child.name()), InputVarType(child));
}
for (auto attr = _obj.node_.first_attribute(); attr;
attr = attr.next_attribute()) {
const auto name = attr.name();
const auto ix = _fct(std::string_view(name));
// XML allows for duplicate node names, but we always want the first
// sibling.
if (ix != -1 && !f_arr[ix]) {
f_arr[ix] = InputVarType(attr);
}
_object_reader.read(std::string_view(attr.name()), InputVarType(attr));
}
const auto ix = _fct(std::string_view("xml_content"));
if (ix != -1) {
f_arr[ix] = InputVarType(_obj.node_);
if constexpr (parsing::is_view_reader_v<ObjectReader>) {
_object_reader.read(std::string_view("xml_content"),
InputVarType(_obj.node_));
}
return f_arr;
}
rfl::Result<std::vector<std::pair<std::string, InputVarType>>> to_map(
const InputObjectType _obj) const noexcept {
std::vector<std::pair<std::string, InputVarType>> m;
for (auto child = _obj.node_.first_child(); child;
child = child.next_sibling()) {
auto p = std::make_pair(std::string(child.name()), InputVarType(child));
m.emplace_back(std::move(p));
}
return m;
return std::nullopt;
}
rfl::Result<InputObjectType> to_object(