draconisplusplus/include/rfl/xml/read.hpp

55 lines
1.4 KiB
C++
Raw Normal View History

2024-06-05 19:04:53 -04:00
#ifndef RFL_XML_READ_HPP_
#define RFL_XML_READ_HPP_
#include <istream>
#include <pugixml.hpp>
#include <string>
#include "../Processors.hpp"
#include "../internal/get_type_name.hpp"
#include "../internal/remove_namespaces.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
namespace xml {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from a XML var.
template <class T, class... Ps>
auto read(const InputVarType& _var) {
const auto r = Reader();
return Parser<T, Processors<Ps...>>::read(r, _var);
}
/// Parses an object from XML using reflection.
template <class T, class... Ps>
Result<T> read(const std::string& _xml_str) {
pugi::xml_document doc;
2024-06-08 15:53:06 -04:00
const auto result = doc.load_string(_xml_str.c_str());
2024-06-08 14:10:59 -04:00
if (!result) {
2024-06-08 15:53:06 -04:00
return Error(
"XML string could not be parsed: " +
std::string(result.description())
);
2024-06-08 14:10:59 -04:00
}
const auto var = InputVarType(doc.first_child());
return read<T, Ps...>(var);
}
/// Parses an object from a stringstream.
template <class T, class... Ps>
auto read(std::istream& _stream) {
2024-06-08 15:53:06 -04:00
const auto xml_str = std::string(
std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>()
);
2024-06-08 14:10:59 -04:00
return read<T, Ps...>(xml_str);
}
} // namespace xml
} // namespace rfl
2024-06-05 19:04:53 -04:00
#endif