2024-06-05 19:04:53 -04:00
|
|
|
#ifndef RFL_XML_WRITER_HPP_
|
|
|
|
#define RFL_XML_WRITER_HPP_
|
|
|
|
|
|
|
|
#include <exception>
|
|
|
|
#include <map>
|
|
|
|
#include <pugixml.hpp>
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../Ref.hpp"
|
|
|
|
#include "../Result.hpp"
|
|
|
|
#include "../always_false.hpp"
|
|
|
|
|
|
|
|
namespace rfl {
|
2024-06-08 14:10:59 -04:00
|
|
|
namespace xml {
|
|
|
|
|
|
|
|
struct Writer {
|
|
|
|
static constexpr const char* XML_CONTENT = "xml_content";
|
|
|
|
|
|
|
|
struct XMLOutputArray {
|
2024-06-16 00:13:15 -04:00
|
|
|
XMLOutputArray(const std::string_view& _name, const Ref<pugi::xml_node>& _node)
|
|
|
|
: name_(_name), node_(_node) {}
|
2024-06-08 15:53:06 -04:00
|
|
|
std::string_view name_;
|
2024-06-08 14:10:59 -04:00
|
|
|
Ref<pugi::xml_node> node_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct XMLOutputObject {
|
|
|
|
XMLOutputObject(const Ref<pugi::xml_node>& _node) : node_(_node) {}
|
|
|
|
Ref<pugi::xml_node> node_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct XMLOutputVar {
|
|
|
|
XMLOutputVar(const Ref<pugi::xml_node>& _node) : node_(_node) {}
|
|
|
|
Ref<pugi::xml_node> node_;
|
|
|
|
};
|
|
|
|
|
|
|
|
using OutputArrayType = XMLOutputArray;
|
|
|
|
using OutputObjectType = XMLOutputObject;
|
|
|
|
using OutputVarType = XMLOutputVar;
|
|
|
|
|
|
|
|
Writer(const Ref<pugi::xml_node>& _root, const std::string& _root_name)
|
2024-06-16 00:13:15 -04:00
|
|
|
: root_(_root), root_name_(_root_name) {}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
~Writer() = default;
|
|
|
|
|
|
|
|
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputArrayType(root_name_, node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputObjectType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputVarType null_as_root() const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
OutputVarType value_as_root(const T& _var) const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
const auto str = to_string(_var);
|
|
|
|
auto node_child = Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
2024-06-08 14:10:59 -04:00
|
|
|
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
OutputArrayType add_array_to_array(const size_t _size, OutputArrayType* _parent)
|
|
|
|
const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
return *_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputArrayType add_array_to_object(
|
2024-06-16 00:13:15 -04:00
|
|
|
const std::string_view& _name,
|
|
|
|
const size_t _size,
|
|
|
|
OutputObjectType* _parent
|
2024-06-08 15:53:06 -04:00
|
|
|
) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputArrayType(_name, _parent->node_);
|
|
|
|
}
|
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
OutputObjectType add_object_to_array(const size_t _size, OutputArrayType* _parent)
|
|
|
|
const noexcept {
|
|
|
|
auto node_child =
|
|
|
|
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputObjectType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputObjectType add_object_to_object(
|
2024-06-16 00:13:15 -04:00
|
|
|
const std::string_view& _name,
|
|
|
|
const size_t _size,
|
|
|
|
OutputObjectType* _parent
|
2024-06-08 15:53:06 -04:00
|
|
|
) const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputObjectType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2024-06-16 00:13:15 -04:00
|
|
|
OutputVarType add_value_to_array(const T& _var, OutputArrayType* _parent) const noexcept {
|
|
|
|
const auto str = to_string(_var);
|
|
|
|
auto node_child =
|
|
|
|
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
OutputVarType add_value_to_object(
|
2024-06-16 00:13:15 -04:00
|
|
|
const std::string_view& _name,
|
|
|
|
const T& _var,
|
|
|
|
OutputObjectType* _parent,
|
|
|
|
const bool _is_attribute = false
|
2024-06-08 15:53:06 -04:00
|
|
|
) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
const auto str = to_string(_var);
|
|
|
|
if (_is_attribute) {
|
|
|
|
_parent->node_->append_attribute(_name.data()) = str.c_str();
|
|
|
|
return OutputVarType(_parent->node_);
|
|
|
|
} else if (_name == XML_CONTENT) {
|
2024-06-16 00:13:15 -04:00
|
|
|
_parent->node_->append_child(pugi::node_pcdata).set_value(str.c_str());
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputVarType(_parent->node_);
|
|
|
|
} else {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child =
|
|
|
|
Ref<pugi::xml_node>::make(_parent->node_->append_child(_parent->name_.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputVarType add_null_to_object(
|
2024-06-16 00:13:15 -04:00
|
|
|
const std::string_view& _name,
|
|
|
|
OutputObjectType* _parent,
|
|
|
|
const bool _is_attribute = false
|
2024-06-08 15:53:06 -04:00
|
|
|
) const noexcept {
|
2024-06-08 14:10:59 -04:00
|
|
|
if (_is_attribute) {
|
|
|
|
return OutputVarType(_parent->node_);
|
|
|
|
} else if (_name == XML_CONTENT) {
|
|
|
|
return OutputVarType(_parent->node_);
|
|
|
|
} else {
|
2024-06-16 00:13:15 -04:00
|
|
|
auto node_child = Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.data()));
|
2024-06-08 14:10:59 -04:00
|
|
|
return OutputVarType(node_child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void end_array(OutputArrayType* _arr) const noexcept {}
|
|
|
|
|
|
|
|
void end_object(OutputObjectType* _obj) const noexcept {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <class T>
|
|
|
|
std::string to_string(const T& _val) const noexcept {
|
|
|
|
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
|
|
|
|
return _val;
|
|
|
|
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
|
|
|
|
return _val ? "true" : "false";
|
|
|
|
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
|
|
|
|
std::is_integral<std::remove_cvref_t<T>>()) {
|
|
|
|
return std::to_string(_val);
|
|
|
|
} else {
|
|
|
|
static_assert(always_false_v<T>, "Unsupported type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
Ref<pugi::xml_node> root_;
|
|
|
|
|
|
|
|
std::string root_name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace xml
|
|
|
|
} // namespace rfl
|
|
|
|
|
|
|
|
#endif // XML_PARSER_HPP_
|