mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 01:06:19 +08:00
This commit is contained in:
@@ -1,59 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTARRAY_HPP_
|
||||
#define FLEXBUF_OUTPUTARRAY_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputArray : public OutputVar {
|
||||
public:
|
||||
OutputArray() {}
|
||||
|
||||
~OutputArray() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
/// We have to catch the edge case of an empty vector,
|
||||
/// because flexbuf ignores empty vectors/maps.
|
||||
if (vars_.size() == 0) {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto start =
|
||||
_key ? _fbb->StartVector(_key->c_str()) : _fbb->StartVector();
|
||||
|
||||
for (const auto& var : vars_) {
|
||||
var->insert(std::nullopt, _fbb);
|
||||
}
|
||||
|
||||
_fbb->EndVector(start, false, false);
|
||||
};
|
||||
|
||||
/// 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 flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,38 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTNULL_HPP_
|
||||
#define FLEXBUF_OUTPUTNULL_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputNull : public OutputVar {
|
||||
public:
|
||||
OutputNull() {}
|
||||
|
||||
~OutputNull() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return true; }
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,61 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
#define FLEXBUF_OUTPUTOBJECT_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputObject : public OutputVar {
|
||||
public:
|
||||
OutputObject() {}
|
||||
|
||||
~OutputObject() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
/// We have to catch the edge case of an empty map,
|
||||
/// because flexbuf ignores empty vectors/maps.
|
||||
if (vars_.size() == 0) {
|
||||
if (_key) {
|
||||
_fbb->Null(_key->c_str());
|
||||
} else {
|
||||
_fbb->Null();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const auto start = _key ? _fbb->StartMap(_key->c_str()) : _fbb->StartMap();
|
||||
|
||||
for (const auto& [name, elem] : vars_) {
|
||||
elem->insert(name, _fbb);
|
||||
}
|
||||
|
||||
_fbb->EndMap(start);
|
||||
};
|
||||
|
||||
/// 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 flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,67 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTVALUE_HPP_
|
||||
#define FLEXBUF_OUTPUTVALUE_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
class OutputValue : public OutputVar {
|
||||
public:
|
||||
OutputValue(T _val) : val_(_val) {}
|
||||
|
||||
~OutputValue() = default;
|
||||
|
||||
/// Inserts all elements into the builder.
|
||||
void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) final {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
if (_key) {
|
||||
_fbb->String(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->String(val_);
|
||||
}
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
if (_key) {
|
||||
_fbb->Bool(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Bool(val_);
|
||||
}
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Double(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Double(val_);
|
||||
}
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
if (_key) {
|
||||
_fbb->Int(_key->c_str(), val_);
|
||||
} else {
|
||||
_fbb->Int(val_);
|
||||
}
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
};
|
||||
|
||||
/// Whether this is null.
|
||||
bool is_null() const final { return false; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
T val_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,27 +0,0 @@
|
||||
#ifndef FLEXBUF_OUTPUTVAR_HPP_
|
||||
#define FLEXBUF_OUTPUTVAR_HPP_
|
||||
|
||||
#include <flatbuffers/flexbuffers.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
class OutputVar {
|
||||
public:
|
||||
virtual ~OutputVar() = default;
|
||||
|
||||
/// Inserts the value into the buffer.
|
||||
virtual void insert(const std::optional<std::string>& _key,
|
||||
flexbuffers::Builder* _fbb) = 0;
|
||||
|
||||
/// Whether this is null.
|
||||
virtual bool is_null() const = 0;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef FLEXBUF_PARSER_HPP_
|
||||
#define FLEXBUF_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
#include "../parsing/Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
@@ -105,10 +105,6 @@ struct Reader {
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
// Necessary, because we write empty vectors as null.
|
||||
if (_var.IsNull()) {
|
||||
return flexbuffers::Vector::EmptyVector();
|
||||
}
|
||||
if (!_var.IsVector()) {
|
||||
return rfl::Error("Could not cast to Vector.");
|
||||
}
|
||||
@@ -133,10 +129,6 @@ struct Reader {
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
// Necessary, because we write empty maps as null.
|
||||
if (_var.IsNull()) {
|
||||
return flexbuffers::Map::EmptyMap();
|
||||
}
|
||||
if (!_var.IsMap()) {
|
||||
return rfl::Error("Could not cast to Map!");
|
||||
}
|
||||
|
||||
+130
-24
@@ -16,51 +16,157 @@
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../Result.hpp"
|
||||
#include "OutputArray.hpp"
|
||||
#include "OutputNull.hpp"
|
||||
#include "OutputObject.hpp"
|
||||
#include "OutputValue.hpp"
|
||||
#include "OutputVar.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace flexbuf {
|
||||
|
||||
struct Writer {
|
||||
using OutputArrayType = Ref<OutputArray>;
|
||||
using OutputObjectType = Ref<OutputObject>;
|
||||
using OutputVarType = Ref<OutputVar>;
|
||||
struct OutputArray {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
Writer() {}
|
||||
struct OutputObject {
|
||||
size_t start_;
|
||||
};
|
||||
|
||||
struct OutputVar {};
|
||||
|
||||
using OutputArrayType = OutputArray;
|
||||
using OutputObjectType = OutputObject;
|
||||
using OutputVarType = OutputVar;
|
||||
|
||||
Writer(const Ref<flexbuffers::Builder>& _fbb) : fbb_(_fbb) {}
|
||||
|
||||
~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 new_array();
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept { return Ref<OutputNull>::make(); }
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
return Ref<OutputValue<T>>::make(_var);
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
fbb_->Null();
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
fbb_->Null(_name.c_str());
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
fbb_->EndVector(_arr->start_, false, false);
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
fbb_->EndMap(_obj->start_);
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string& _name,
|
||||
const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
fbb_->String(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
fbb_->Bool(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
fbb_->Double(_name.c_str(), _var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
fbb_->Int(_name.c_str(), _var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
fbb_->String(_var);
|
||||
} else if constexpr (std::is_same<std::decay_t<T>, bool>()) {
|
||||
fbb_->Bool(_var);
|
||||
} else if constexpr (std::is_floating_point<std::decay_t<T>>()) {
|
||||
fbb_->Double(_var);
|
||||
} else if constexpr (std::is_integral<std::decay_t<T>>()) {
|
||||
fbb_->Int(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<T>, "Unsupported type");
|
||||
}
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string& _name) const noexcept {
|
||||
const auto start = fbb_->StartVector(_name.c_str());
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return Ref<OutputArray>::make();
|
||||
const auto start = fbb_->StartVector();
|
||||
return OutputArrayType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string& _name) const noexcept {
|
||||
const auto start = fbb_->StartMap(_name.c_str());
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return Ref<OutputObject>::make();
|
||||
const auto start = fbb_->StartMap();
|
||||
return OutputObjectType{start};
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return _var->is_null();
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
(*_obj)->push_back(_name, _var);
|
||||
}
|
||||
private:
|
||||
Ref<flexbuffers::Builder> fbb_;
|
||||
};
|
||||
|
||||
} // namespace flexbuf
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include "../Ref.hpp"
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -15,12 +17,12 @@ namespace flexbuf {
|
||||
|
||||
template <class T>
|
||||
std::vector<uint8_t> to_buffer(const T& _obj) {
|
||||
auto w = Writer();
|
||||
const auto flexbuf_obj = Parser<T>::write(w, _obj);
|
||||
flexbuffers::Builder fbb;
|
||||
flexbuf_obj->insert(std::nullopt, &fbb);
|
||||
fbb.Finish();
|
||||
return fbb.GetBuffer();
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto fbb = Ref<flexbuffers::Builder>::make();
|
||||
auto w = Writer(fbb);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
fbb->Finish();
|
||||
return fbb->GetBuffer();
|
||||
}
|
||||
|
||||
/// Writes an object to flexbuf.
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <yyjson.h>
|
||||
|
||||
#include <array>
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
@@ -42,24 +43,10 @@ struct Reader {
|
||||
using InputObjectType = YYJSONInputObject;
|
||||
using InputVarType = YYJSONInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_json_obj<
|
||||
T,
|
||||
std::enable_if_t<std::is_invocable_r<
|
||||
T, decltype(T::from_json_obj), typename Reader::InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
struct has_from_json_obj<T, std::enable_if_t<std::is_invocable_r<
|
||||
rfl::Result<T>, decltype(T::from_json_obj),
|
||||
typename Reader::InputVarType>::value>>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = has_from_json_obj<T>::value;
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_json_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType _obj) const noexcept {
|
||||
|
||||
+92
-23
@@ -18,7 +18,8 @@
|
||||
namespace rfl {
|
||||
namespace json {
|
||||
|
||||
struct Writer {
|
||||
class Writer {
|
||||
public:
|
||||
struct YYJSONOutputArray {
|
||||
YYJSONOutputArray(yyjson_mut_val* _val) : val_(_val) {}
|
||||
yyjson_mut_val* val_;
|
||||
@@ -47,14 +48,99 @@ struct Writer {
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept {
|
||||
yyjson_mut_arr_add_val(_arr->val_, _var.val_);
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputVarType empty_var() const noexcept {
|
||||
return OutputVarType(yyjson_mut_null(doc_));
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_doc_set_root(doc_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_doc_set_root(doc_, val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto arr = yyjson_mut_arr(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
arr);
|
||||
return OutputArrayType(arr);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto obj = yyjson_mut_obj(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
obj);
|
||||
return OutputObjectType(obj);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_arr_add_val(_parent->val_, val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto val = from_basic_type(_var);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
val.val_);
|
||||
return OutputVarType(val);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_arr_add_val(_parent->val_, null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
const auto null = yyjson_mut_null(doc_);
|
||||
yyjson_mut_obj_add(_parent->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
null);
|
||||
return OutputVarType(null);
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType from_basic_type(const T& _var) const noexcept {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>()) {
|
||||
@@ -70,24 +156,7 @@ struct Writer {
|
||||
}
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
return OutputArrayType(yyjson_mut_arr(doc_));
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
return OutputObjectType(yyjson_mut_obj(doc_));
|
||||
}
|
||||
|
||||
bool is_empty(const OutputVarType& _var) const noexcept {
|
||||
return yyjson_mut_is_null(_var.val_);
|
||||
}
|
||||
|
||||
void set_field(const std::string& _name, const OutputVarType& _var,
|
||||
OutputObjectType* _obj) const noexcept {
|
||||
yyjson_mut_obj_add(_obj->val_, yyjson_mut_strcpy(doc_, _name.c_str()),
|
||||
_var.val_);
|
||||
}
|
||||
|
||||
public:
|
||||
yyjson_mut_doc* doc_;
|
||||
};
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -15,9 +16,9 @@ namespace json {
|
||||
/// Returns a JSON string.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
const auto json_str = std::string(json_c_str);
|
||||
free((void*)json_c_str);
|
||||
@@ -28,9 +29,9 @@ std::string write(const T& _obj) {
|
||||
/// Writes a JSON into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
auto w = Writer(yyjson_mut_doc_new(NULL));
|
||||
const auto json_obj = Parser<T>::write(w, _obj);
|
||||
yyjson_mut_doc_set_root(w.doc_, json_obj.val_);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
const char* json_c_str = yyjson_mut_write(w.doc_, 0, NULL);
|
||||
_stream << json_c_str;
|
||||
free((void*)json_c_str);
|
||||
|
||||
@@ -14,31 +14,33 @@ namespace parsing {
|
||||
template <class ReaderType, class WriterType, class OriginalClass,
|
||||
class HelperStruct>
|
||||
struct CustomParser {
|
||||
static Result<OriginalClass> read(const ReaderType& _r,
|
||||
const auto& _var) noexcept {
|
||||
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
|
||||
try {
|
||||
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
|
||||
return _h.to_class();
|
||||
} else {
|
||||
auto ptr_field_tuple = internal::to_ptr_field_tuple(_h);
|
||||
const auto class_from_ptrs = [](auto&... _ptrs) {
|
||||
return OriginalClass(std::move(*_ptrs.value_)...);
|
||||
};
|
||||
return std::apply(class_from_ptrs, ptr_field_tuple);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::read(_r, _var)
|
||||
.and_then(to_class);
|
||||
}
|
||||
static Result<OriginalClass> read(const ReaderType& _r,
|
||||
const auto& _var) noexcept {
|
||||
const auto to_class = [](auto&& _h) -> Result<OriginalClass> {
|
||||
try {
|
||||
if constexpr (internal::has_to_class_method_v<HelperStruct>) {
|
||||
return _h.to_class();
|
||||
} else {
|
||||
auto ptr_field_tuple = internal::to_ptr_field_tuple(_h);
|
||||
const auto class_from_ptrs = [](auto&... _ptrs) {
|
||||
return OriginalClass(std::move(*_ptrs.value_)...);
|
||||
};
|
||||
return std::apply(class_from_ptrs, ptr_field_tuple);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
};
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::read(_r, _var)
|
||||
.and_then(to_class);
|
||||
}
|
||||
|
||||
static auto write(const WriterType& _w, const OriginalClass& _p) noexcept {
|
||||
return Parser<ReaderType, WriterType, HelperStruct>::write(
|
||||
_w, HelperStruct::from_class(_p));
|
||||
}
|
||||
template <class P>
|
||||
static auto write(const WriterType& _w, const OriginalClass& _p,
|
||||
const P& _parent) noexcept {
|
||||
Parser<ReaderType, WriterType, HelperStruct>::write(
|
||||
_w, HelperStruct::from_class(_p), _parent);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -51,8 +51,9 @@ struct FieldVariantParser {
|
||||
return _r.to_object(_var).transform(to_map).and_then(to_result);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::variant<FieldTypes...>& _v) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::variant<FieldTypes...>& _v,
|
||||
const P& _parent) noexcept {
|
||||
static_assert(
|
||||
internal::no_duplicate_field_names<std::tuple<FieldTypes...>>(),
|
||||
"Externally tagged variants cannot have duplicate field "
|
||||
@@ -61,10 +62,10 @@ struct FieldVariantParser {
|
||||
const auto handle = [&](const auto& _field) {
|
||||
const auto named_tuple = make_named_tuple(internal::to_ptr_field(_field));
|
||||
using NamedTupleType = std::decay_t<decltype(named_tuple)>;
|
||||
return Parser<R, W, NamedTupleType>::write(_w, named_tuple);
|
||||
Parser<R, W, NamedTupleType>::write(_w, named_tuple, _parent);
|
||||
};
|
||||
|
||||
return std::visit(handle, _v);
|
||||
std::visit(handle, _v);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -43,7 +43,7 @@ concept IsReader = requires(R r, std::string name,
|
||||
/// floating point, std::string)
|
||||
{ r.template to_basic_type<T>(var) } -> std::same_as<rfl::Result<T>>;
|
||||
|
||||
/// fct is a function that turns the field name into the field index of the
|
||||
/// _fct is a function that turns the field name into the field index of the
|
||||
/// struct. It returns -1, if the fields does not exist on the struct. This
|
||||
/// returns an std::array that can be used to build up the struct.
|
||||
{
|
||||
|
||||
@@ -9,39 +9,88 @@ namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class W, class T>
|
||||
concept IsWriter = requires(W w, T t, std::string name,
|
||||
concept IsWriter = requires(W w, T t, std::string name, std::string basic_value,
|
||||
typename W::OutputArrayType arr,
|
||||
typename W::OutputObjectType obj,
|
||||
typename W::OutputVarType var) {
|
||||
/// Any Writer needs to define the following:
|
||||
///
|
||||
/// 1) An OutputArrayType, which must be an array-like data structure.
|
||||
/// 2) An OutputObjectType, which must contain key-value pairs.
|
||||
/// 3) An OutputVarType, which must be able to represent either
|
||||
/// OutputArrayType, OutputObjectType or a basic type (bool, integral,
|
||||
/// floating point, std::string).
|
||||
typename W::OutputVarType var, size_t size) {
|
||||
/// Sets an empty array as the root element of the document.
|
||||
/// Some serialization formats require you to pass the expected size in
|
||||
/// advance. If you are not working with such a format, you can ignore the
|
||||
/// parameter `size`. Returns the new array for further modification.
|
||||
{ w.array_as_root(size) } -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Appends var to the end of arr, thus mutating it.
|
||||
{ w.add(var, &arr) } -> std::same_as<void>;
|
||||
/// Sets an empty object as the root element of the document.
|
||||
/// Some serialization formats require you to pass the expected size in
|
||||
/// advance. If you are not working with such a format, you can ignore the
|
||||
/// parameter `size`.
|
||||
/// Returns the new object for further modification.
|
||||
{ w.object_as_root(size) } -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Returns an empty var (usually the NULL type).
|
||||
{ w.empty_var() } -> std::same_as<typename W::OutputVarType>;
|
||||
/// Sets a null as the root element of the document. Returns OutputVarType
|
||||
/// containing the null value.
|
||||
{ w.null_as_root() } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a var from a basic type (std::string, bool, floating point or
|
||||
/// integral).
|
||||
{ w.from_basic_type(t) } -> std::same_as<typename W::OutputVarType>;
|
||||
/// Sets a basic value (bool, numeric, string) as the root element of the
|
||||
/// document. Returns an OutputVarType containing the new value.
|
||||
{ w.value_as_root(basic_value) } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Generates a new, empty array.
|
||||
{ w.new_array() } -> std::same_as<typename W::OutputArrayType>;
|
||||
/// Adds an empty array to an existing array. Returns the new
|
||||
/// array for further modification.
|
||||
{
|
||||
w.add_array_to_array(size, &arr)
|
||||
} -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Generates a new, empty object.
|
||||
{ w.new_object() } -> std::same_as<typename W::OutputObjectType>;
|
||||
/// Adds an empty object to an existing array. Returns the new
|
||||
/// object for further modification.
|
||||
{
|
||||
w.add_object_to_array(size, &arr)
|
||||
} -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Determines whether the var is empty (the NULL type).
|
||||
{ w.is_empty(var) } -> std::same_as<bool>;
|
||||
/// Adds an empty array to an existing object. The key or name of the field is
|
||||
/// signified by `name`. Returns the new array for further modification.
|
||||
{
|
||||
w.add_array_to_object(name, size, &obj)
|
||||
} -> std::same_as<typename W::OutputArrayType>;
|
||||
|
||||
/// Adds a new field to obj, thus mutating it.
|
||||
{ w.set_field(name, var, &obj) } -> std::same_as<void>;
|
||||
/// Adds an empty object to an existing object. The key or name of the field
|
||||
/// is signified by `name`. Returns the new object for further modification.
|
||||
{
|
||||
w.add_object_to_object(name, size, &obj)
|
||||
} -> std::same_as<typename W::OutputObjectType>;
|
||||
|
||||
/// Adds a basic value (bool, numeric, string) to an array. Returns an
|
||||
/// OutputVarType containing the new value.
|
||||
{
|
||||
w.add_value_to_array(basic_value, &arr)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Adds a basic value (bool, numeric, string) to an existing object. The key
|
||||
/// or name of the field is signified by `name`. Returns an
|
||||
/// OutputVarType containing the new value.
|
||||
{
|
||||
w.add_value_to_object(name, basic_value, &obj)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Adds a null value to an array. Returns an
|
||||
/// OutputVarType containing the null value.
|
||||
{ w.add_null_to_array(&arr) } -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Adds a null value to an existing object. The key
|
||||
/// or name of the field is signified by `name`. Returns an
|
||||
/// OutputVarType containing the null value.
|
||||
{
|
||||
w.add_null_to_object(name, &obj)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
/// Signifies to the writer that we do not want to add any further elements to
|
||||
/// this array. Some serialization formats require this. If you are working
|
||||
/// with a serialization format that doesn't, just leave the function empty.
|
||||
{ w.end_array(&arr) } -> std::same_as<void>;
|
||||
|
||||
/// Signifies to the writer that we do not want to add any further elements to
|
||||
/// this object. Some serialization formats require this. If you are working
|
||||
/// with a serialization format that doesn't, just leave the function empty.
|
||||
{ w.end_object(&obj) } -> std::same_as<void>;
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -25,34 +26,42 @@ struct MapParser {
|
||||
using KeyType = std::decay_t<typename MapType::value_type::first_type>;
|
||||
using ValueType = std::decay_t<typename MapType::value_type::second_type>;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<MapType> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_map = [&](const auto& _obj) { return make_map(_r, _obj); };
|
||||
return _r.to_object(_var).and_then(to_map);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const MapType& _m) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
template <class P>
|
||||
static void write(const W& _w, const MapType& _m, const P& _parent) noexcept {
|
||||
auto obj = ParentType::add_object(_w, _m.size(), _parent);
|
||||
for (const auto& [k, v] : _m) {
|
||||
auto parsed_val = Parser<R, W, std::decay_t<ValueType>>::write(_w, v);
|
||||
if constexpr (internal::has_reflection_type_v<KeyType>) {
|
||||
using ReflT = typename KeyType::ReflectionType;
|
||||
|
||||
if constexpr (std::is_integral_v<ReflT> ||
|
||||
std::is_floating_point_v<ReflT>) {
|
||||
_w.set_field(std::to_string(k.reflection()), std::move(parsed_val),
|
||||
&obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k.reflection()), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
} else {
|
||||
_w.set_field(k.reflection(), std::move(parsed_val), &obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{k.reflection(), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
}
|
||||
|
||||
} else if constexpr (std::is_integral_v<KeyType> ||
|
||||
std::is_floating_point_v<KeyType>) {
|
||||
_w.set_field(std::to_string(k), std::move(parsed_val), &obj);
|
||||
const auto new_parent =
|
||||
typename ParentType::Object{std::to_string(k), &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
} else {
|
||||
_w.set_field(k, std::move(parsed_val), &obj);
|
||||
const auto new_parent = typename ParentType::Object{k, &obj};
|
||||
Parser<R, W, std::decay_t<ValueType>>::write(_w, v, new_parent);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -9,10 +9,13 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "../internal/Memoization.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/strings/replace_all.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_empty.hpp"
|
||||
#include "is_required.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -27,8 +30,9 @@ struct NamedTupleParser {
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
public:
|
||||
/// Generates a NamedTuple from a JSON Object.
|
||||
static Result<NamedTuple<FieldTypes...>> read(
|
||||
const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto& indices = field_indices();
|
||||
@@ -45,12 +49,12 @@ struct NamedTupleParser {
|
||||
return _r.to_object(_var).transform(to_fields_array).and_then(build);
|
||||
}
|
||||
|
||||
/// Transforms a NamedTuple into a JSON object.
|
||||
static OutputVarType write(const W& _w,
|
||||
const NamedTuple<FieldTypes...>& _tup) noexcept {
|
||||
auto obj = _w.new_object();
|
||||
template <class P>
|
||||
static void write(const W& _w, const NamedTuple<FieldTypes...>& _tup,
|
||||
const P& _parent) noexcept {
|
||||
auto obj = ParentType::add_object(_w, _tup.size(), _parent);
|
||||
build_object_recursively(_w, _tup, &obj);
|
||||
return OutputVarType(obj);
|
||||
_w.end_object(&obj);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -160,14 +164,24 @@ struct NamedTupleParser {
|
||||
using FieldType =
|
||||
typename std::tuple_element<_i, std::tuple<FieldTypes...>>::type;
|
||||
using ValueType = std::decay_t<typename FieldType::Type>;
|
||||
auto value = Parser<R, W, ValueType>::write(_w, rfl::get<_i>(_tup));
|
||||
const auto& value = rfl::get<_i>(_tup);
|
||||
const auto name = FieldType::name_.str();
|
||||
const auto new_parent = typename ParentType::Object{name, _ptr};
|
||||
if constexpr (!is_required<ValueType, _ignore_empty_containers>()) {
|
||||
if (!_w.is_empty(value)) {
|
||||
_w.set_field(name, value, _ptr);
|
||||
if (!is_empty(value)) {
|
||||
if constexpr (internal::is_attribute_v<ValueType>) {
|
||||
Parser<R, W, ValueType>::write(_w, value,
|
||||
new_parent.as_attribute());
|
||||
} else {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_w.set_field(name, value, _ptr);
|
||||
if constexpr (internal::is_attribute_v<ValueType>) {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent.as_attribute());
|
||||
} else {
|
||||
Parser<R, W, ValueType>::write(_w, value, new_parent);
|
||||
}
|
||||
}
|
||||
return build_object_recursively<_i + 1>(_w, _tup, _ptr);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
#ifndef RFL_PARSING_PARENT_HPP_
|
||||
#define RFL_PARSING_PARENT_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../always_false.hpp"
|
||||
#include "supports_attributes.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class W>
|
||||
struct Parent {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputObjectType = typename W::OutputObjectType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
struct Array {
|
||||
OutputArrayType* arr_;
|
||||
};
|
||||
|
||||
struct Object {
|
||||
std::string name_;
|
||||
OutputObjectType* obj_;
|
||||
bool is_attribute_ = false;
|
||||
Object as_attribute() const { return Object{name_, obj_, true}; }
|
||||
};
|
||||
|
||||
struct Root {};
|
||||
|
||||
template <class ParentType>
|
||||
static OutputArrayType add_array(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_array_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
return _w.add_array_to_object(_parent.name_, _size, _parent.obj_);
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.array_as_root(_size);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType>
|
||||
static OutputObjectType add_object(const W& _w, const size_t _size,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_object_to_array(_size, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
return _w.add_object_to_object(_parent.name_, _size, _parent.obj_);
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.object_as_root(_size);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType>
|
||||
static OutputVarType add_null(const W& _w, const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_null_to_array(_parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
return _w.add_null_to_object(_parent.name_, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
return _w.add_null_to_object(_parent.name_, _parent.obj_);
|
||||
}
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.null_as_root();
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class ParentType, class T>
|
||||
static OutputVarType add_value(const W& _w, const T& _var,
|
||||
const ParentType& _parent) {
|
||||
using Type = std::decay_t<ParentType>;
|
||||
if constexpr (std::is_same<Type, Array>()) {
|
||||
return _w.add_value_to_array(_var, _parent.arr_);
|
||||
} else if constexpr (std::is_same<Type, Object>()) {
|
||||
if constexpr (supports_attributes<std::decay_t<W>>) {
|
||||
return _w.add_value_to_object(_parent.name_, _var, _parent.obj_,
|
||||
_parent.is_attribute_);
|
||||
} else {
|
||||
return _w.add_value_to_object(_parent.name_, _var, _parent.obj_);
|
||||
}
|
||||
} else if constexpr (std::is_same<Type, Root>()) {
|
||||
return _w.value_as_root(_var);
|
||||
} else {
|
||||
static_assert(always_false_v<Type>, "Unsupported option.");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -21,6 +22,8 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::array<T, _size>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_vec = [&](const auto& _arr) { return _r.to_vec(_arr); };
|
||||
@@ -43,13 +46,15 @@ struct Parser<R, W, std::array<T, _size>> {
|
||||
.and_then(extract);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::array<T, _size>& _arr) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::array<T, _size>& _arr,
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, _size, _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (auto it = _arr.begin(); it != _arr.end(); ++it) {
|
||||
_w.add(Parser<R, W, std::decay_t<T>>::write(_w, *it), &arr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *it, new_parent);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -21,8 +21,10 @@ struct Parser<R, W, Box<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_box);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const Box<T>& _box) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_box);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Box<T>& _box,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_box, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -10,12 +10,11 @@
|
||||
#include "../internal/enums/StringConverter.hpp"
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_attribute.hpp"
|
||||
#include "../internal/is_basic_type.hpp"
|
||||
#include "../internal/to_ptr_named_tuple.hpp"
|
||||
#include "AreReaderAndWriter.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "supports_attributes.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -27,6 +26,8 @@ struct Parser {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
if constexpr (R::template has_custom_constructor<T>) {
|
||||
@@ -68,28 +69,26 @@ struct Parser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the variable to a JSON type.
|
||||
static auto write(const W& _w, const T& _var) noexcept {
|
||||
if constexpr (supports_attributes<W> && internal::is_attribute_v<T>) {
|
||||
return _w.from_basic_type(resolve_reflection_type(_var), true);
|
||||
} else if constexpr (internal::has_reflection_type_v<T>) {
|
||||
template <class P>
|
||||
static void write(const W& _w, const T& _var, const P& _parent) noexcept {
|
||||
if constexpr (internal::has_reflection_type_v<T>) {
|
||||
using ReflectionType = std::decay_t<typename T::ReflectionType>;
|
||||
if constexpr (internal::has_reflection_method_v<T>) {
|
||||
return Parser<R, W, ReflectionType>::write(_w, _var.reflection());
|
||||
Parser<R, W, ReflectionType>::write(_w, _var.reflection(), _parent);
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return Parser<R, W, ReflectionType>::write(_w, r);
|
||||
Parser<R, W, ReflectionType>::write(_w, r, _parent);
|
||||
}
|
||||
} else if constexpr (std::is_class_v<T> && std::is_aggregate_v<T>) {
|
||||
const auto ptr_named_tuple = internal::to_ptr_named_tuple(_var);
|
||||
using PtrNamedTupleType = std::decay_t<decltype(ptr_named_tuple)>;
|
||||
return Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple);
|
||||
Parser<R, W, PtrNamedTupleType>::write(_w, ptr_named_tuple, _parent);
|
||||
} else if constexpr (std::is_enum_v<T>) {
|
||||
using StringConverter = internal::enums::StringConverter<T>;
|
||||
const auto str = StringConverter::enum_to_string(_var);
|
||||
return _w.from_basic_type(str);
|
||||
ParentType::add_value(_w, str, _parent);
|
||||
} else if constexpr (internal::is_basic_type_v<T>) {
|
||||
return _w.from_basic_type(_var);
|
||||
ParentType::add_value(_w, _var, _parent);
|
||||
} else {
|
||||
static_assert(always_false_v<T>,
|
||||
"Unsupported type. Please refer to the sections on custom "
|
||||
@@ -97,20 +96,6 @@ struct Parser {
|
||||
"support for your own classes.");
|
||||
}
|
||||
}
|
||||
|
||||
template <class V>
|
||||
static const auto resolve_reflection_type(const V& _var) {
|
||||
if constexpr (internal::has_reflection_type_v<V>) {
|
||||
if constexpr (internal::has_reflection_method_v<V>) {
|
||||
return resolve_reflection_type(_var.reflection());
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return resolve_reflection_type(r);
|
||||
}
|
||||
} else {
|
||||
return _var;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace parsing
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,6 +18,8 @@ struct Parser<R, W, std::optional<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::optional<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
@@ -26,11 +29,14 @@ struct Parser<R, W, std::optional<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_opt);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const std::optional<T>& _o) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::optional<T>& _o,
|
||||
const P& _parent) noexcept {
|
||||
if (!_o) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_o);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_o, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -29,12 +29,12 @@ struct Parser<R, W, std::pair<FirstType, SecondType>> {
|
||||
.transform(to_pair);
|
||||
}
|
||||
|
||||
/// Transform a std::pair into an array
|
||||
static OutputVarType write(
|
||||
const W& _w, const std::pair<FirstType, SecondType>& _p) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::pair<FirstType, SecondType>& _p,
|
||||
const P& _parent) noexcept {
|
||||
const auto tup = std::make_tuple(&_p.first, &_p.second);
|
||||
return Parser<R, W, std::tuple<const FirstType*, const SecondType*>>::write(
|
||||
_w, tup);
|
||||
Parser<R, W, std::tuple<const FirstType*, const SecondType*>>::write(
|
||||
_w, tup, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -16,6 +17,8 @@ struct Parser<R, W, T*> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<T*> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
static_assert(always_false_v<T>,
|
||||
@@ -27,12 +30,13 @@ struct Parser<R, W, T*> {
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w, const T* _ptr) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const T* _ptr, const P& _parent) noexcept {
|
||||
if (!_ptr) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_ptr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ptr, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -17,15 +17,15 @@ struct Parser<R, W, Ref<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
static Result<Ref<T>> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
const auto to_ref = [&](auto&& _t) { return Ref<T>::make(std::move(_t)); };
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ref);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w, const Ref<T>& _ref) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_ref);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Ref<T>& _ref,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_ref, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -28,9 +28,10 @@ struct Parser<R, W, std::reference_wrapper<T>> {
|
||||
return Error("Unsupported.");
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::reference_wrapper<T> _ref) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _ref.get());
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::reference_wrapper<T> _ref,
|
||||
const P& _p) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _ref.get(), _p);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -26,9 +26,10 @@ struct Parser<R, W, Rename<_name, T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_rename);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const Rename<_name, T>& _rename) noexcept {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _rename.value());
|
||||
template <class P>
|
||||
static void write(const W& _w, const Rename<_name, T>& _rename,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _rename.value(), _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -39,17 +39,21 @@ struct Parser<R, W, Result<T>> {
|
||||
Parser<R, W, VariantType>::read(_r, _var).transform(to_res));
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const Result<T>& _r) noexcept {
|
||||
const auto write_t = [&](const auto& _t) -> OutputVarType {
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, _t);
|
||||
template <class P>
|
||||
static void write(const W& _w, const Result<T>& _r,
|
||||
const P& _parent) noexcept {
|
||||
const auto write_t = [&](const auto& _t) -> Nothing {
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, _t, _parent);
|
||||
return Nothing{};
|
||||
};
|
||||
|
||||
const auto write_err = [&](const auto& _err) -> OutputVarType {
|
||||
return Parser<R, W, ErrorType>::write(
|
||||
_w, ErrorType(make_field<"error">(_err.what())));
|
||||
const auto write_err = [&](const auto& _err) -> Nothing {
|
||||
Parser<R, W, ErrorType>::write(
|
||||
_w, ErrorType(make_field<"error">(_err.what())), _parent);
|
||||
return Nothing{};
|
||||
};
|
||||
|
||||
return *_r.transform(write_t).or_else(write_err);
|
||||
_r.transform(write_t).or_else(write_err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -30,10 +30,11 @@ struct Parser<R, W, rfl::Variant<FieldTypes...>> {
|
||||
to_rfl_variant);
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const rfl::Variant<FieldTypes...>& _variant) noexcept {
|
||||
return Parser<R, W, std::variant<FieldTypes...>>::write(_w,
|
||||
_variant.variant());
|
||||
template <class P>
|
||||
static void write(const W& _w, const rfl::Variant<FieldTypes...>& _variant,
|
||||
const P& _parent) noexcept {
|
||||
Parser<R, W, std::variant<FieldTypes...>>::write(_w, _variant.variant(),
|
||||
_parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,7 +18,8 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
/// Expresses the variables as type T.
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::shared_ptr<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(*_var)) {
|
||||
@@ -29,13 +31,14 @@ struct Parser<R, W, std::shared_ptr<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
}
|
||||
|
||||
/// Expresses the variable a a JSON.
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::shared_ptr<T>& _s) noexcept {
|
||||
template <class P>
|
||||
static OutputVarType write(const W& _w, const std::shared_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_s);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -38,13 +38,15 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
return _r.to_object(_var).and_then(get_disc).and_then(to_result);
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const TaggedUnion<_discriminator, AlternativeTypes...>&
|
||||
_tagged_union) noexcept {
|
||||
const auto handle = [&](const auto& _val) -> OutputVarType {
|
||||
return write_wrapped(_w, _val);
|
||||
template <class P>
|
||||
static void write(
|
||||
const W& _w,
|
||||
const TaggedUnion<_discriminator, AlternativeTypes...>& _tagged_union,
|
||||
const P& _parent) noexcept {
|
||||
const auto handle = [&](const auto& _val) {
|
||||
write_wrapped(_w, _val, _parent);
|
||||
};
|
||||
return std::visit(handle, _tagged_union.variant_);
|
||||
std::visit(handle, _tagged_union.variant_);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -109,20 +111,21 @@ struct Parser<R, W, TaggedUnion<_discriminator, AlternativeTypes...>> {
|
||||
}
|
||||
|
||||
/// Writes a wrapped version of the original object, which contains the tag.
|
||||
template <class T>
|
||||
static OutputVarType write_wrapped(const W& _w, const T& _val) noexcept {
|
||||
template <class T, class P>
|
||||
static void write_wrapped(const W& _w, const T& _val,
|
||||
const P& _parent) noexcept {
|
||||
const auto tag = internal::make_tag<T>();
|
||||
using TagType = std::decay_t<decltype(tag)>;
|
||||
if constexpr (internal::has_fields<std::decay_t<T>>()) {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperWithFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
} else {
|
||||
using WrapperType =
|
||||
TaggedUnionWrapperNoFields<T, TagType, _discriminator>;
|
||||
const auto wrapper = WrapperType{.tag = tag, .fields = &_val};
|
||||
return Parser<R, W, WrapperType>::write(_w, wrapper);
|
||||
Parser<R, W, WrapperType>::write(_w, wrapper, _parent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
|
||||
namespace rfl {
|
||||
@@ -17,6 +18,8 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
using InputVarType = typename R::InputVarType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::unique_ptr<T>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
if (_r.is_empty(_var)) {
|
||||
@@ -28,12 +31,14 @@ struct Parser<R, W, std::unique_ptr<T>> {
|
||||
return Parser<R, W, std::decay_t<T>>::read(_r, _var).transform(to_ptr);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::unique_ptr<T>& _s) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::unique_ptr<T>& _s,
|
||||
const P& _parent) noexcept {
|
||||
if (!_s) {
|
||||
return _w.empty_var();
|
||||
ParentType::add_null(_w, _parent);
|
||||
return;
|
||||
}
|
||||
return Parser<R, W, std::decay_t<T>>::write(_w, *_s);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, *_s, _parent);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -49,14 +49,15 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
|
||||
}
|
||||
}
|
||||
|
||||
static OutputVarType write(
|
||||
const W& _w, const std::variant<FieldTypes...>& _variant) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::variant<FieldTypes...>& _variant,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (internal::all_fields<std::tuple<FieldTypes...>>()) {
|
||||
return FieldVariantParser<R, W, FieldTypes...>::write(_w, _variant);
|
||||
FieldVariantParser<R, W, FieldTypes...>::write(_w, _variant, _parent);
|
||||
} else {
|
||||
const auto handle = [&](const auto& _v) {
|
||||
using Type = std::decay_t<decltype(_v)>;
|
||||
return Parser<R, W, Type>::write(_w, _v);
|
||||
Parser<R, W, Type>::write(_w, _v, _parent);
|
||||
};
|
||||
return std::visit(handle, _variant);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "Parent.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
@@ -20,6 +21,8 @@ struct TupleParser {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
static Result<std::tuple<Ts...>> read(const R& _r,
|
||||
const InputVarType& _var) noexcept {
|
||||
const auto to_vec = [&](auto _arr) { return _r.to_vec(_arr); };
|
||||
@@ -42,11 +45,13 @@ struct TupleParser {
|
||||
.and_then(extract);
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w,
|
||||
const std::tuple<Ts...>& _tup) noexcept {
|
||||
auto arr = _w.new_array();
|
||||
to_array<0>(_w, _tup, &arr);
|
||||
return arr;
|
||||
template <class P>
|
||||
static void write(const W& _w, const std::tuple<Ts...>& _tup,
|
||||
const P& _parent) noexcept {
|
||||
auto arr = ParentType::add_array(_w, sizeof...(Ts), _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
to_array<0>(_w, _tup, new_parent);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -89,17 +94,14 @@ struct TupleParser {
|
||||
return Parser<R, W, NewFieldType>::read(_r, _vec[_i]);
|
||||
}
|
||||
|
||||
template <int _i>
|
||||
template <int _i, class P>
|
||||
static void to_array(const W& _w, const std::tuple<Ts...>& _tup,
|
||||
OutputArrayType* _ptr) noexcept {
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (_i < sizeof...(Ts)) {
|
||||
using NewFieldType = std::decay_t<
|
||||
typename std::tuple_element<_i, std::tuple<Ts...>>::type>;
|
||||
|
||||
const auto val =
|
||||
Parser<R, W, NewFieldType>::write(_w, std::get<_i>(_tup));
|
||||
_w.add(val, _ptr);
|
||||
to_array<_i + 1>(_w, _tup, _ptr);
|
||||
Parser<R, W, NewFieldType>::write(_w, std::get<_i>(_tup), _parent);
|
||||
to_array<_i + 1>(_w, _tup, _parent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef RFL_PARSING_VECTORPARSER_HPP_
|
||||
#define RFL_PARSING_VECTORPARSER_HPP_
|
||||
|
||||
#include <iterator>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
@@ -8,6 +9,7 @@
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
#include "MapParser.hpp"
|
||||
#include "Parent.hpp"
|
||||
#include "Parser_base.hpp"
|
||||
#include "is_forward_list.hpp"
|
||||
#include "is_map_like.hpp"
|
||||
@@ -31,6 +33,8 @@ struct VectorParser {
|
||||
using OutputArrayType = typename W::OutputArrayType;
|
||||
using OutputVarType = typename W::OutputVarType;
|
||||
|
||||
using ParentType = Parent<W>;
|
||||
|
||||
using T = typename VecType::value_type;
|
||||
|
||||
static Result<VecType> read(const R& _r, const InputVarType& _var) noexcept {
|
||||
@@ -44,15 +48,19 @@ struct VectorParser {
|
||||
}
|
||||
}
|
||||
|
||||
static OutputVarType write(const W& _w, const VecType& _vec) noexcept {
|
||||
template <class P>
|
||||
static void write(const W& _w, const VecType& _vec,
|
||||
const P& _parent) noexcept {
|
||||
if constexpr (treat_as_map()) {
|
||||
return MapParser<R, W, VecType>::write(_w, _vec);
|
||||
MapParser<R, W, VecType>::write(_w, _vec, _parent);
|
||||
} else {
|
||||
auto arr = _w.new_array();
|
||||
auto arr = ParentType::add_array(
|
||||
_w, std::distance(_vec.begin(), _vec.end()), _parent);
|
||||
const auto new_parent = typename ParentType::Array{&arr};
|
||||
for (const auto& v : _vec) {
|
||||
_w.add(Parser<R, W, std::decay_t<T>>::write(_w, v), &arr);
|
||||
Parser<R, W, std::decay_t<T>>::write(_w, v, new_parent);
|
||||
}
|
||||
return OutputVarType(arr);
|
||||
_w.end_array(&arr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef RFL_PARSING_IS_EMPTY_HPP_
|
||||
#define RFL_PARSING_IS_EMPTY_HPP_
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
#include "../internal/has_reflection_method_v.hpp"
|
||||
#include "../internal/has_reflection_type_v.hpp"
|
||||
#include "../internal/is_rename.hpp"
|
||||
#include "is_map_like.hpp"
|
||||
#include "is_vector_like.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace parsing {
|
||||
|
||||
template <class T>
|
||||
static const bool is_empty(const T& _var) {
|
||||
using Type = std::decay_t<T>;
|
||||
if constexpr (std::is_pointer_v<Type>) {
|
||||
return !_var || is_empty(*_var);
|
||||
} else if constexpr (internal::has_reflection_type_v<Type>) {
|
||||
if constexpr (internal::has_reflection_method_v<Type>) {
|
||||
return is_empty(_var.reflection());
|
||||
} else {
|
||||
const auto& [r] = _var;
|
||||
return is_empty(r);
|
||||
}
|
||||
} else if constexpr (internal::is_rename_v<Type>) {
|
||||
return is_empty(_var.value());
|
||||
} else if constexpr (is_map_like_v<Type> || is_vector_like_v<Type>) {
|
||||
return _var.begin() == _var.end();
|
||||
} else {
|
||||
return !_var;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace parsing
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RFL_PARSING_SUPPORTSATTRIBUTES_HPP_
|
||||
|
||||
#include <concepts>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
|
||||
@@ -10,9 +11,15 @@ namespace parsing {
|
||||
|
||||
/// Determines whether a writer supports attributes.
|
||||
template <class W>
|
||||
concept supports_attributes = requires(W w, int val, bool _is_attribute) {
|
||||
concept supports_attributes = requires(W w, std::string name,
|
||||
typename W::OutputObjectType obj,
|
||||
bool is_attribute) {
|
||||
{
|
||||
w.from_basic_type(val, _is_attribute)
|
||||
w.add_value_to_object(name, name, &obj, is_attribute)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
|
||||
{
|
||||
w.add_null_to_object(name, &obj, is_attribute)
|
||||
} -> std::same_as<typename W::OutputVarType>;
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
#ifndef FLEXBUF_XML_HPP_
|
||||
#define FLEXBUF_XML_HPP_
|
||||
#ifndef RFL_XML_HPP_
|
||||
#define RFL_XML_HPP_
|
||||
|
||||
#include "xml/Parser.hpp"
|
||||
#include "xml/Reader.hpp"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef RFL_YAML_HPP_
|
||||
#define RFL_YAML_HPP_
|
||||
|
||||
#include "yaml/Parser.hpp"
|
||||
#include "yaml/Reader.hpp"
|
||||
#include "yaml/Writer.hpp"
|
||||
#include "yaml/load.hpp"
|
||||
#include "yaml/read.hpp"
|
||||
#include "yaml/save.hpp"
|
||||
#include "yaml/write.hpp"
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RFL_YAML_PARSER_HPP_
|
||||
#define RFL_YAML_PARSER_HPP_
|
||||
|
||||
#include "../parsing/Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
#include "Writer.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
using Parser = parsing::Parser<Reader, Writer, T>;
|
||||
|
||||
}
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,151 @@
|
||||
#ifndef RFL_YAML_READER_HPP_
|
||||
#define RFL_YAML_READER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <array>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../always_false.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
struct Reader {
|
||||
struct YAMLInputArray {
|
||||
YAMLInputArray(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputObject {
|
||||
YAMLInputObject(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
struct YAMLInputVar {
|
||||
YAMLInputVar(const YAML::Node& _node) : node_(_node) {}
|
||||
YAML::Node node_;
|
||||
};
|
||||
|
||||
using InputArrayType = YAMLInputArray;
|
||||
using InputObjectType = YAMLInputObject;
|
||||
using InputVarType = YAMLInputVar;
|
||||
|
||||
template <class T, class = void>
|
||||
struct has_from_json_obj : std::false_type {};
|
||||
|
||||
template <class T>
|
||||
static constexpr bool has_custom_constructor = (requires(InputVarType var) {
|
||||
T::from_yaml_obj(var);
|
||||
});
|
||||
|
||||
rfl::Result<InputVarType> get_field(
|
||||
const std::string& _name, const InputObjectType& _obj) const noexcept {
|
||||
auto var = InputVarType(_obj.node_[_name]);
|
||||
if (!var.node_) {
|
||||
return rfl::Error("Object contains no field named '" + _name + "'.");
|
||||
}
|
||||
return var;
|
||||
}
|
||||
|
||||
bool is_empty(const InputVarType& _var) const noexcept {
|
||||
return !_var.node_ && true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> to_basic_type(const InputVarType& _var) const noexcept {
|
||||
try {
|
||||
if constexpr (std::is_same<std::decay_t<T>, std::string>() ||
|
||||
std::is_same<std::decay_t<T>, bool>() ||
|
||||
std::is_floating_point<std::decay_t<T>>() ||
|
||||
std::is_integral<std::decay_t<T>>()) {
|
||||
return _var.node_.as<std::decay_t<T>>();
|
||||
} else {
|
||||
static_assert(rfl::always_false_v<T>, "Unsupported type.");
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
rfl::Result<InputArrayType> to_array(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsSequence()) {
|
||||
return rfl::Error("Could not cast to sequence!");
|
||||
}
|
||||
return InputArrayType(_var.node_);
|
||||
}
|
||||
|
||||
template <size_t size, class FunctionType>
|
||||
std::array<std::optional<InputVarType>, size> to_fields_array(
|
||||
const FunctionType& _fct, const InputObjectType& _obj) const noexcept {
|
||||
std::array<std::optional<InputVarType>, size> f_arr;
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
const auto k = p.first.as<std::string>();
|
||||
const auto ix = _fct(std::string_view(k));
|
||||
if (ix != -1) {
|
||||
f_arr[ix] = InputVarType(p.second);
|
||||
}
|
||||
} catch (std::exception& e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return f_arr;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string, InputVarType>> to_map(
|
||||
const InputObjectType& _obj) const noexcept {
|
||||
std::vector<std::pair<std::string, InputVarType>> m;
|
||||
for (const auto& p : _obj.node_) {
|
||||
try {
|
||||
auto k = p.first.as<std::string>();
|
||||
m.emplace_back(std::make_pair(k, p.second));
|
||||
} catch (std::exception& e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
rfl::Result<InputObjectType> to_object(
|
||||
const InputVarType& _var) const noexcept {
|
||||
if (!_var.node_.IsMap()) {
|
||||
return rfl::Error("Could not cast to map!");
|
||||
}
|
||||
return InputObjectType(_var.node_);
|
||||
}
|
||||
|
||||
std::vector<InputVarType> to_vec(const InputArrayType& _arr) const noexcept {
|
||||
std::vector<InputVarType> vec;
|
||||
for (size_t i = 0; i < _arr.node_.size(); ++i) {
|
||||
vec.push_back(InputVarType(_arr.node_[i]));
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
rfl::Result<T> use_custom_constructor(
|
||||
const InputVarType _var) const noexcept {
|
||||
try {
|
||||
return T::from_yaml_obj(_var);
|
||||
} catch (std::exception& e) {
|
||||
return rfl::Error(e.what());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,147 @@
|
||||
#ifndef RFL_YAML_WRITER_HPP_
|
||||
#define RFL_YAML_WRITER_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#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 {
|
||||
namespace yaml {
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
struct YAMLArray {};
|
||||
|
||||
struct YAMLObject {};
|
||||
|
||||
struct YAMLVar {};
|
||||
|
||||
using OutputArrayType = YAMLArray;
|
||||
using OutputObjectType = YAMLObject;
|
||||
using OutputVarType = YAMLVar;
|
||||
|
||||
Writer(const Ref<YAML::Emitter>& _out) : out_(_out) {}
|
||||
|
||||
~Writer() = default;
|
||||
|
||||
OutputArrayType array_as_root(const size_t _size) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputObjectType object_as_root(const size_t _size) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputVarType null_as_root() const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType value_as_root(const T& _var) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_array(const size_t _size,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return new_array();
|
||||
}
|
||||
|
||||
OutputArrayType add_array_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_array(_name);
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_array(
|
||||
const size_t _size, OutputArrayType* _parent) const noexcept {
|
||||
return new_object();
|
||||
}
|
||||
|
||||
OutputObjectType add_object_to_object(
|
||||
const std::string& _name, const size_t _size,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return new_object(_name);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_array(const T& _var,
|
||||
OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(_var);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType add_value_to_object(const std::string& _name, const T& _var,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, _var);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_array(OutputArrayType* _parent) const noexcept {
|
||||
return insert_value(YAML::Null);
|
||||
}
|
||||
|
||||
OutputVarType add_null_to_object(const std::string& _name,
|
||||
OutputObjectType* _parent) const noexcept {
|
||||
return insert_value(_name, YAML::Null);
|
||||
}
|
||||
|
||||
void end_array(OutputArrayType* _arr) const noexcept {
|
||||
(*out_) << YAML::EndSeq;
|
||||
}
|
||||
|
||||
void end_object(OutputObjectType* _obj) const noexcept {
|
||||
(*out_) << YAML::EndMap;
|
||||
}
|
||||
|
||||
private:
|
||||
template <class T>
|
||||
OutputVarType insert_value(const std::string& _name,
|
||||
const T& _var) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
template <class T>
|
||||
OutputVarType insert_value(const T& _var) const noexcept {
|
||||
(*out_) << _var;
|
||||
return OutputVarType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array(const std::string& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
OutputArrayType new_array() const noexcept {
|
||||
(*out_) << YAML::BeginSeq;
|
||||
return OutputArrayType{};
|
||||
}
|
||||
|
||||
OutputObjectType new_object(const std::string& _name) const noexcept {
|
||||
(*out_) << YAML::Key << _name << YAML::Value << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
OutputObjectType new_object() const noexcept {
|
||||
(*out_) << YAML::BeginMap;
|
||||
return OutputObjectType{};
|
||||
}
|
||||
|
||||
public:
|
||||
const Ref<YAML::Emitter> out_;
|
||||
};
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // JSON_PARSER_HPP_
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef RFL_YAML_LOAD_HPP_
|
||||
#define RFL_YAML_LOAD_HPP_
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/load_string.hpp"
|
||||
#include "read.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
Result<T> load(const std::string& _fname) {
|
||||
const auto read_string = [](const auto& _str) { return read<T>(_str); };
|
||||
return rfl::io::load_string(_fname).and_then(read_string);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef RFL_YAML_READ_HPP_
|
||||
#define RFL_YAML_READ_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <istream>
|
||||
#include <string>
|
||||
|
||||
#include "Parser.hpp"
|
||||
#include "Reader.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
using InputVarType = typename Reader::InputVarType;
|
||||
|
||||
/// Parses an object from a YAML var.
|
||||
template <class T>
|
||||
Result<T> read(const InputVarType& _var) {
|
||||
const auto r = Reader();
|
||||
return Parser<T>::read(r, _var);
|
||||
}
|
||||
|
||||
/// Parses an object from YAML using reflection.
|
||||
template <class T>
|
||||
Result<T> read(const std::string& _yaml_str) {
|
||||
try {
|
||||
const auto var = InputVarType(YAML::Load(_yaml_str));
|
||||
return read<T>(var);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses an object from a stringstream.
|
||||
template <class T>
|
||||
Result<T> read(std::istream& _stream) {
|
||||
const auto yaml_str = std::string(std::istreambuf_iterator<char>(_stream),
|
||||
std::istreambuf_iterator<char>());
|
||||
return read<T>(yaml_str);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef RFL_YAML_SAVE_HPP_
|
||||
#define RFL_YAML_SAVE_HPP_
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "../Result.hpp"
|
||||
#include "../io/save_string.hpp"
|
||||
#include "write.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
template <class T>
|
||||
Result<Nothing> save(const std::string& _fname, const T& _obj) {
|
||||
const auto write_func = [](const auto& _obj, auto& _stream) -> auto& {
|
||||
return write(_obj, _stream);
|
||||
};
|
||||
return rfl::io::save_string(_fname, _obj, write_func);
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef RFL_YAML_WRITE_HPP_
|
||||
#define RFL_YAML_WRITE_HPP_
|
||||
|
||||
#include <yaml-cpp/yaml.h>
|
||||
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include "../parsing/Parent.hpp"
|
||||
#include "Parser.hpp"
|
||||
|
||||
namespace rfl {
|
||||
namespace yaml {
|
||||
|
||||
/// Writes a XML into an ostream.
|
||||
template <class T>
|
||||
std::ostream& write(const T& _obj, std::ostream& _stream) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto out = Ref<YAML::Emitter>::make();
|
||||
auto w = Writer(out);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
_stream << out->c_str();
|
||||
return _stream;
|
||||
}
|
||||
|
||||
/// Returns a XML string.
|
||||
template <class T>
|
||||
std::string write(const T& _obj) {
|
||||
using ParentType = parsing::Parent<Writer>;
|
||||
const auto out = Ref<YAML::Emitter>::make();
|
||||
auto w = Writer(out);
|
||||
Parser<T>::write(w, _obj, typename ParentType::Root{});
|
||||
return out->c_str();
|
||||
}
|
||||
|
||||
} // namespace yaml
|
||||
} // namespace rfl
|
||||
|
||||
#endif // XML_PARSER_HPP_
|
||||
Reference in New Issue
Block a user