File: Reader.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (184 lines) | stat: -rw-r--r-- 6,012 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#ifndef RFL_XML_READER_HPP_
#define RFL_XML_READER_HPP_

#include <exception>
#include <optional>
#include <pugixml.hpp>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>

#include "../Result.hpp"
#include "../always_false.hpp"
#include "../parsing/is_view_reader.hpp"

namespace rfl {
namespace xml {

struct Reader {
  struct XMLInputArray {
    XMLInputArray(pugi::xml_node _node) : node_(_node) {}
    pugi::xml_node node_;
  };

  struct XMLInputObject {
    XMLInputObject(pugi::xml_node _node) : node_(_node) {}
    pugi::xml_node node_;
  };

  struct XMLInputVar {
    XMLInputVar() : node_or_attribute_(pugi::xml_node()) {}
    XMLInputVar(pugi::xml_attribute _attr) : node_or_attribute_(_attr) {}
    XMLInputVar(pugi::xml_node _node) : node_or_attribute_(_node) {}
    std::variant<pugi::xml_node, pugi::xml_attribute> node_or_attribute_;
  };

  using InputArrayType = XMLInputArray;
  using InputObjectType = XMLInputObject;
  using InputVarType = XMLInputVar;

  // TODO
  template <class T>
  static constexpr bool has_custom_constructor = false;

  rfl::Result<InputVarType> get_field_from_array(
      const size_t _idx, const InputArrayType& _arr) const noexcept {
    const auto name = _arr.node_.name();
    size_t i = 0;
    for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
      if (i == _idx) {
        return InputVarType(node);
      }
    }
    return error("Index " + std::to_string(_idx) + " of of bounds.");
  }

  rfl::Result<InputVarType> get_field_from_object(
      const std::string& _name, const InputObjectType _obj) const noexcept {
    const auto node = _obj.node_.child(_name.c_str());
    if (!node) {
      return error("Object contains no field named '" + _name + "'.");
    }
    return InputVarType(node);
  }

  bool is_empty(const InputVarType _var) const noexcept {
    const auto wrap = [](const auto& _node) { return !_node; };
    return std::visit(cast_as_node, _var.node_or_attribute_)
        .transform(wrap)
        .value_or(false);
  }

  template <class T>
  rfl::Result<T> to_basic_type(const InputVarType _var) const noexcept {
    const auto get_value = [](const auto& _n) -> std::string {
      using Type = std::remove_cvref_t<decltype(_n)>;
      if constexpr (std::is_same<Type, pugi::xml_node>()) {
        return std::string(_n.child_value());
      } else {
        return std::string(_n.value());
      }
    };

    if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
      return std::visit(get_value, _var.node_or_attribute_);

    } else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
      return std::visit(get_value, _var.node_or_attribute_) == "true";

    } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
      const auto str = std::visit(get_value, _var.node_or_attribute_);
      try {
        return static_cast<T>(std::stod(str));
      } catch (std::exception& e) {
        return error("Could not cast '" + std::string(str) +
                     "' to floating point value.");
      }

    } else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
      const auto str = std::visit(get_value, _var.node_or_attribute_);
      try {
        return static_cast<T>(std::stoi(str));
      } catch (std::exception& e) {
        return error("Could not cast '" + std::string(str) + "' to integer.");
      }

    } else {
      static_assert(rfl::always_false_v<T>, "Unsupported type.");
    }
  }

  rfl::Result<InputArrayType> to_array(const InputVarType _var) const noexcept {
    const auto wrap = [](const auto& _node) { return InputArrayType(_node); };
    return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
  }

  template <class ArrayReader>
  std::optional<Error> read_array(const ArrayReader& _array_reader,
                                  const InputArrayType& _arr) const noexcept {
    const auto name = _arr.node_.name();
    for (auto node = _arr.node_; node; node = node.next_sibling(name)) {
      const auto err = _array_reader.read(InputVarType(node));
      if (err) {
        return err;
      }
    }
    return std::nullopt;
  }

  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()) {
      _object_reader.read(std::string_view(child.name()), InputVarType(child));
    }

    for (auto attr = _obj.node_.first_attribute(); attr;
         attr = attr.next_attribute()) {
      _object_reader.read(std::string_view(attr.name()), InputVarType(attr));
    }

    if constexpr (parsing::is_view_reader_v<ObjectReader>) {
      _object_reader.read(std::string_view("xml_content"),
                          InputVarType(_obj.node_));
    }

    return std::nullopt;
  }

  rfl::Result<InputObjectType> to_object(
      const InputVarType _var) const noexcept {
    const auto wrap = [](const auto& _node) { return InputObjectType(_node); };
    return std::visit(cast_as_node, _var.node_or_attribute_).transform(wrap);
  }

  template <class T>
  rfl::Result<T> use_custom_constructor(
      const InputVarType _var) const noexcept {
    return error("TODO");
  }

 private:
  /// XML-only helper function. This is needed because XML distinguishes between
  /// nodes and attributes.
  static rfl::Result<pugi::xml_node> cast_as_node(
      const std::variant<pugi::xml_node, pugi::xml_attribute>&
          _node_or_attribute) {
    const auto cast = [](const auto& _n) -> Result<pugi::xml_node> {
      using Type = std::remove_cvref_t<decltype(_n)>;
      if constexpr (std::is_same<Type, pugi::xml_node>()) {
        return _n;
      } else {
        return error("Field '" + std::string(_n.name()) + "' is an attribute.");
      }
    };
    return std::visit(cast, _node_or_attribute);
  }
};

}  // namespace xml
}  // namespace rfl

#endif