mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTARRAY_HPP_
|
||||
#define RFL_XML_OUTPUTARRAY_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputArray : public OutputVar {
|
||||
public:
|
||||
OutputArray() {}
|
||||
|
||||
~OutputArray() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
for (auto& v : vars_) {
|
||||
v->insert(_key, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const Ref<OutputVar>& _var) { vars_.push_back(_var); }
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<Ref<OutputVar>> vars_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,32 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTNULL_HPP_
|
||||
#define RFL_XML_OUTPUTNULL_HPP_
|
||||
|
||||
#include <optional>
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputNull : public OutputVar {
|
||||
public:
|
||||
OutputNull() {}
|
||||
|
||||
~OutputNull() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
_parent->append_child(_key.c_str());
|
||||
}
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return true; }
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,44 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTOBJECT_HPP_
|
||||
#define RFL_XML_OUTPUTOBJECT_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputObject : public OutputVar {
|
||||
public:
|
||||
OutputObject() {}
|
||||
|
||||
~OutputObject() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
auto node_child = _parent->append_child(_key.c_str());
|
||||
for (auto& v : vars_) {
|
||||
v.second->insert(v.first, &node_child);
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
/// Adds a new element to the vector.
|
||||
void push_back(const std::string& _name, const Ref<OutputVar>& _var) {
|
||||
vars_.push_back(std::make_pair(_name, _var));
|
||||
}
|
||||
|
||||
private:
|
||||
/// The underlying variables.
|
||||
std::vector<std::pair<std::string, Ref<OutputVar>>> vars_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTVALUE_HPP_
|
||||
#define RFL_XML_OUTPUTVALUE_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../always_false.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
template <class T>
|
||||
class OutputValue : public OutputVar {
|
||||
public:
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
public:
|
||||
OutputValue(T _val, bool _is_attribute)
|
||||
: is_attribute_(_is_attribute), val_(_val) {}
|
||||
|
||||
~OutputValue() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::string& _key, pugi::xml_node* _parent) final {
|
||||
const auto str = to_string();
|
||||
if (is_attribute_) {
|
||||
_parent->append_attribute(_key.c_str()) = str.c_str();
|
||||
} else if (_key == XML_CONTENT) {
|
||||
_parent->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
} else {
|
||||
auto node_child = _parent->append_child(_key.c_str());
|
||||
node_child.append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
private:
|
||||
/// Transforms the underlying value into a string.
|
||||
std::string to_string() {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
/// Whether you want this encoded as an attribute.
|
||||
bool is_attribute_;
|
||||
|
||||
/// The underlying value.
|
||||
T val_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef RFL_XML_OUTPUTVAR_HPP_
|
||||
#define RFL_XML_OUTPUTVAR_HPP_
|
||||
|
||||
#include <pugixml.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
class OutputVar {
|
||||
public:
|
||||
virtual ~OutputVar() = default;
|
||||
|
||||
/// Inserts the value into the _parent.
|
||||
virtual void insert(const std::string& _key, pugi::xml_node* _parent) = 0;
|
||||
|
||||
/// Whether this is null.
|
||||
virtual bool is_null() const = 0;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -3,13 +3,11 @@
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "is_required_for_xml.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../parsing/NamedTupleParser.hpp"
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "is_required_for_xml.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
+138
-23
@@ -14,49 +14,164 @@
|
||||
#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>;
|
||||
static constexpr const char* XML_CONTENT = "xml_content";
|
||||
|
||||
Writer() {}
|
||||
struct XMLOutputArray {
|
||||
XMLOutputArray(const std::string& _name, const Ref<pugi::xml_node>& _node)
|
||||
: name_(_name), node_(_node) {}
|
||||
std::string name_;
|
||||
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)
|
||||
: root_(_root), root_name_(_root_name) {}
|
||||
|
||||
~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 {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputArrayType(root_name_, node_child);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept { return make_ref<OutputNull>(); }
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
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);
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(root_->append_child(root_name_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept { return make_ref<OutputArray>(); }
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return make_ref<OutputObject>();
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return *_parent;
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return _var->is_null();
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return OutputArrayType(_name, _parent->node_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
(*_obj)->push_back(_name, _var);
|
||||
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_.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
auto node_child =
|
||||
Ref<pugi::xml_node>::make(_parent->node_->append_child(_name.c_str()));
|
||||
return OutputObjectType(node_child);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
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_.c_str()));
|
||||
node_child->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(
|
||||
const std::string& _name, const T& _var, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
const auto str = to_string(_var);
|
||||
if (_is_attribute) {
|
||||
_parent->node_->append_attribute(_name.c_str()) = str.c_str();
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
_parent->node_->append_child(pugi::node_pcdata).set_value(str.c_str());
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_name.c_str()));
|
||||
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 {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_parent->name_.c_str()));
|
||||
return OutputVarType(node_child);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(
|
||||
const std::string& _name, OutputObjectType* _parent,
|
||||
const bool _is_attribute = false) const noexcept {
|
||||
if (_is_attribute) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else if (_name == XML_CONTENT) {
|
||||
return OutputVarType(_parent->node_);
|
||||
} else {
|
||||
auto node_child = Ref<pugi::xml_node>::make(
|
||||
_parent->node_->append_child(_name.c_str()));
|
||||
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::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:
|
||||
Ref<pugi::xml_node> root_;
|
||||
|
||||
std::string root_name_;
|
||||
};
|
||||
|
||||
} // namespace xml
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef RFL_XML_IS_REQUIRED_FOR_XML_HPP_
|
||||
#define RFL_XML_IS_REQUIRED_FOR_XML_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../parsing/is_required.hpp"
|
||||
#include "../parsing/is_vector_like.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace xml {
|
||||
|
||||
// Empty vectors are simply not represented in XML. Therefore, XML requires more
|
||||
// lax rules when things are required.
|
||||
template <class T>
|
||||
consteval bool is_required_for_xml() {
|
||||
using Type = std::decay_t<std::remove_pointer_t<T>>;
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
return is_required_for_xml<typename T::ReflectionType>();
|
||||
} else {
|
||||
return parsing::is_required<Type>() && !parsing::is_vector_like_v<Type>;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace xml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../internal/StringLiteral.hpp"
|
||||
#include "../internal/get_type_name.hpp"
|
||||
#include "../internal/remove_namespaces.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -29,6 +30,8 @@ consteval auto get_root_name() {
|
||||
template <internal::StringLiteral _root = internal::StringLiteral(""), class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream,
|
||||
const std::string& _indent = " ") {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
|
||||
constexpr auto root_name = get_root_name<_root, T>();
|
||||
|
||||
static_assert(root_name.string_view().find("<") == std::string_view::npos &&
|
||||
@@ -38,18 +41,17 @@ std::ostream& write(const T& _obj, std::ostream& _stream,
|
||||
"explicit root name to rfl::xml::write(...) like this: "
|
||||
"rfl::xml::write<\"root_name\">(...).");
|
||||
|
||||
auto w = Writer();
|
||||
const auto xml_obj = Parser<T>::write(w, _obj);
|
||||
const auto doc = rfl::Ref<pugi::xml_document>::make();
|
||||
|
||||
pugi::xml_document doc;
|
||||
|
||||
auto declaration_node = doc.append_child(pugi::node_declaration);
|
||||
auto declaration_node = doc->append_child(pugi::node_declaration);
|
||||
declaration_node.append_attribute("version") = "1.0";
|
||||
declaration_node.append_attribute("encoding") = "UTF-8";
|
||||
|
||||
xml_obj->insert(root_name.str(), &doc);
|
||||
auto w = Writer(doc, root_name.str());
|
||||
|
||||
doc.save(_stream, _indent.c_str());
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
|
||||
doc->save(_stream, _indent.c_str());
|
||||
|
||||
return _stream;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user