Files
Dr. Patrick Urbanke (劉自成) ec6ab3baf5 Improved support for string_views for TOML and XML (#445)
* Improved support for string_views for TOML and XML

* Upgrade to pugixml 1.15 in order to be able to use string views
2025-06-08 21:00:21 +02:00

54 lines
1.5 KiB
C++

#ifndef RFL_XML_READ_HPP_
#define RFL_XML_READ_HPP_
#include <istream>
#include <pugixml.hpp>
#include <string>
#include <string_view>
#include "../Processors.hpp"
#include "../internal/get_type_name.hpp"
#include "../internal/remove_namespaces.hpp"
#include "Parser.hpp"
#include "Reader.hpp"
namespace rfl ::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();
using ProcessorsType = Processors<Ps...>;
static_assert(!ProcessorsType::no_field_names_,
"The NoFieldNames processor is not supported for BSON, XML, "
"TOML, or YAML.");
return Parser<T, ProcessorsType>::read(r, _var);
}
/// Parses an object from XML using reflection.
template <class T, class... Ps>
Result<T> read(const std::string_view _xml_str) {
pugi::xml_document doc;
const auto result = doc.load_buffer(_xml_str.data(), _xml_str.size());
if (!result) {
return error("XML string could not be parsed: " +
std::string(result.description()));
}
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) {
const auto xml_str = std::string(std::istreambuf_iterator<char>(_stream),
std::istreambuf_iterator<char>());
return read<T, Ps...>(xml_str);
}
} // namespace rfl::xml
#endif