Skip to content

Commit

Permalink
Began implementing the XML writer in the new style
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Dec 28, 2023
1 parent b546501 commit ef47113
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions include/rfl/xml/Writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OutputArray>;
using OutputObjectType = Ref<OutputObject>;
using OutputVarType = Ref<OutputVar>;
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<OutputNull>(); }

template <class T>
OutputVarType from_basic_type(
const T& _var, const bool _is_attribute = false) const noexcept {
return make_ref<OutputValue<T>>(_var, _is_attribute);
OutputObjectType object_as_root(const size_t _size) const noexcept {
return OutputObjectType(root_);
}

OutputArrayType new_array() const noexcept { return make_ref<OutputArray>(); }
OutputVarType null_as_root() const noexcept { return OutputVarType(root_); }

OutputObjectType new_object() const noexcept {
return make_ref<OutputObject>();
template <class T>
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());

Check failure on line 58 in include/rfl/xml/Writer.hpp

View workflow job for this annotation

GitHub Actions / linux-gcc

no matching function for call to ‘pugi::xml_node::append_child(pugi::xml_node_type) const’

Check failure on line 58 in include/rfl/xml/Writer.hpp

View workflow job for this annotation

GitHub Actions / linux-clang

no matching member function for call to 'append_child'
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 <class T>
std::string to_string(const T& _val) const noexcept {
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
return _val;
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
return _val ? "true" : "false";
} else if constexpr (std::is_floating_point<std::decay_t<T>>() ||
std::is_integral<std::decay_t<T>>()) {
return std::to_string(_val);
} else {
static_assert(always_false_v<T>, "Unsupported type");
}
}

public:
pugi::xml_node root_;
};

} // namespace xml
Expand Down

0 comments on commit ef47113

Please sign in to comment.