Added XML support; #21

This commit is contained in:
Patrick Urbanke
2023-12-20 21:07:36 +01:00
parent 2281849dde
commit c5347eee28
124 changed files with 4652 additions and 1437 deletions
+49
View File
@@ -0,0 +1,49 @@
#ifndef RFL_XML_READ_HPP_
#define RFL_XML_READ_HPP_
#include <istream>
#include <pugixml.hpp>
#include <string>
#include "rfl/internal/get_type_name.hpp"
#include "rfl/internal/remove_namespaces.hpp"
#include "rfl/xml/Parser.hpp"
#include "rfl/xml/Reader.hpp"
namespace rfl {
namespace xml {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from a XML var.
template <class T>
Result<T> read(const InputVarType& _var) {
const auto r = Reader();
return Parser<T>::read(r, _var);
}
/// Parses an object from XML using reflection.
template <class T>
Result<T> read(const std::string& _xml_str) {
pugi::xml_document doc;
const auto result = doc.load_string(_xml_str.c_str());
if (!result) {
return Error("XML string could not be parsed: " +
std::string(result.description()));
}
const auto var = InputVarType(doc.first_child());
return read<T>(var);
}
/// Parses an object from a stringstream.
template <class T>
Result<T> read(std::istream& _stream) {
const auto xml_str = std::string(std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>());
return read<T>(xml_str);
}
} // namespace xml
} // namespace rfl
#endif