From ef47113d2c0f01f5bacdaf0c9a5c2c548f49aa03 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Thu, 28 Dec 2023 22:37:44 +0100 Subject: [PATCH] Began implementing the XML writer in the new style --- include/rfl/xml/Writer.hpp | 72 +++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/include/rfl/xml/Writer.hpp b/include/rfl/xml/Writer.hpp index cb3e5528..2471ffb3 100644 --- a/include/rfl/xml/Writer.hpp +++ b/include/rfl/xml/Writer.hpp @@ -14,49 +14,73 @@ #include "../Ref.hpp" #include "../Result.hpp" #include "../always_false.hpp" -#include "OutputArray.hpp" -#include "OutputNull.hpp" -#include "OutputObject.hpp" -#include "OutputValue.hpp" namespace rfl { namespace xml { struct Writer { - using OutputArrayType = Ref; - using OutputObjectType = Ref; - using OutputVarType = Ref; + struct XMLOutputArray { + XMLOutputArray(const pugi::xml_node& _val) : val_(_val) {} + pugi::xml_node val_; + }; - Writer() {} + struct XMLOutputObject { + XMLOutputObject(const pugi::xml_node& _val) : val_(_val) {} + pugi::xml_node val_; + }; + + struct XMLOutputVar { + XMLOutputVar(const pugi::xml_node& _val) : val_(_val) {} + pugi::xml_node val_; + }; + + using OutputArrayType = XMLOutputArray; + using OutputObjectType = XMLOutputObject; + using OutputVarType = XMLOutputVar; + + Writer(const pugi::xml_node& _root) : root_(_root) {} ~Writer() = default; - void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept { - (*_arr)->push_back(_var); + OutputArrayType array_as_root(const size_t _size) const noexcept { + return OutputArrayType(root_); } - OutputVarType empty_var() const noexcept { return make_ref(); } - - template - OutputVarType from_basic_type( - const T& _var, const bool _is_attribute = false) const noexcept { - return make_ref>(_var, _is_attribute); + OutputObjectType object_as_root(const size_t _size) const noexcept { + return OutputObjectType(root_); } - OutputArrayType new_array() const noexcept { return make_ref(); } + OutputVarType null_as_root() const noexcept { return OutputVarType(root_); } - OutputObjectType new_object() const noexcept { - return make_ref(); + template + OutputVarType value_as_root(const T& _var) const noexcept { + const auto str = to_string(_var); + root_.append_child(pugi::node_pcdata).set_value(str.c_str()); + return OutputVarType(root_); } - bool is_empty(const OutputVarType& _var) const noexcept { - return _var->is_null(); + OutputArrayType add_array_to_array(const size_t _size, + OutputArrayType* _parent) const noexcept { + return OutputArrayType(_parent->val_); } - void set_field(const std::string& _name, const OutputVarType& _var, - OutputObjectType* _obj) const noexcept { - (*_obj)->push_back(_name, _var); + private: + template + std::string to_string(const T& _val) const noexcept { + if constexpr (std::is_same, std::string>()) { + return _val; + } else if constexpr (std::is_same, bool>()) { + return _val ? "true" : "false"; + } else if constexpr (std::is_floating_point>() || + std::is_integral>()) { + return std::to_string(_val); + } else { + static_assert(always_false_v, "Unsupported type"); + } } + + public: + pugi::xml_node root_; }; } // namespace xml