Skip to content

Commit

Permalink
Some more adaptations
Browse files Browse the repository at this point in the history
  • Loading branch information
liuzicheng1987 committed Jan 3, 2024
1 parent 7a5b53b commit e01ff0b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 2 additions & 2 deletions include/rfl/parsing/Parser_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ struct Parser<R, W, std::array<T, _size>> {
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) {
Parser<R, W, std::remove_cvref_t<T>>::write(_w, *it, new_parent);
for (const auto& e : _arr) {
Parser<R, W, std::remove_cvref_t<T>>::write(_w, e, new_parent);
}
_w.end_array(&arr);
}
Expand Down
17 changes: 11 additions & 6 deletions include/rfl/parsing/Parser_c_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include "../Result.hpp"
#include "../always_false.hpp"
#include "../internal/to_std_array.hpp"
#include "Parent.hpp"
#include "Parser_array.hpp"
#include "Parser_base.hpp"

namespace rfl {
namespace parsing {

template <class R, class W, class T, size_t _size>
requires AreReaderAndWriter<R, W, T[_size]>
requires AreReaderAndWriter<R, W, T[_size]>
struct Parser<R, W, T[_size]> {
public:
using InputArrayType = typename R::InputArrayType;
Expand All @@ -23,20 +24,24 @@ struct Parser<R, W, T[_size]> {
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;

using ParentType = Parent<W>;
using CArray = T[_size];
using StdArray = internal::to_std_array_t<T[_size]>;

static Result<Array<T[_size]>> read(const R& _r,
const InputVarType& _var) noexcept {
using StdArray = internal::to_std_array_t<T[_size]>;
return Parser<R, W, StdArray>::read(_r, _var);
}

static OutputVarType write(const W& _w, const CArray& _arr) noexcept {
auto arr = _w.new_array();
template <class P>
static void write(const W& _w, const CArray& _arr,
const P& _parent) noexcept {
auto arr = ParentType::add_array(_w, _size, _parent);
const auto new_parent = typename ParentType::Array{&arr};
for (const auto& e : _arr) {
_w.add(Parser<R, W, std::remove_cvref_t<T>>::write(_w, e), &arr);
Parser<R, W, std::remove_cvref_t<T>>::write(_w, e, new_parent);
}
return OutputVarType(arr);
_w.end_array(&arr);
}
};

Expand Down
9 changes: 6 additions & 3 deletions include/rfl/parsing/Parser_result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ 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::remove_cvref_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::remove_cvref_t<T>>::write(_w, _t, _parent);
return Nothing{};
};

const auto write_err = [&](const auto& _err) -> Nothing {
Expand Down
11 changes: 6 additions & 5 deletions include/rfl/parsing/Parser_rfl_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace rfl {
namespace parsing {

template <class R, class W, class T>
requires AreReaderAndWriter<R, W, Array<T>>
requires AreReaderAndWriter<R, W, Array<T>>
struct Parser<R, W, Array<T>> {
public:
using InputArrayType = typename R::InputArrayType;
Expand All @@ -23,15 +23,16 @@ struct Parser<R, W, Array<T>> {
using OutputArrayType = typename W::OutputArrayType;
using OutputVarType = typename W::OutputVarType;

using RflArray = Array<T>;
using StdArray = internal::to_std_array_t<T>;

static Result<RflArray> read(const R& _r, const InputVarType& _var) noexcept {
static Result<Array<T>> read(const R& _r, const InputVarType& _var) noexcept {
return Parser<R, W, StdArray>::read(_r, _var);
}

static OutputVarType write(const W& _w, const RflArray& _arr) noexcept {
return Parser<R, W, StdArray>::write(_w, _arr);
template <class P>
static void write(const W& _w, const Array<T>& _arr,
const P& _parent) noexcept {
Parser<R, W, StdArray>::write(_w, _arr, _parent);
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/rfl/parsing/Parser_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ struct Parser<R, W, std::variant<FieldTypes...>> {
} else {
const auto handle = [&](const auto& _v) {
using Type = std::remove_cvref_t<decltype(_v)>;
return Parser<R, W, Type>::write(_w, _v);
Parser<R, W, Type>::write(_w, _v, _parent);
};
return std::visit(handle, _variant);
}
Expand Down

0 comments on commit e01ff0b

Please sign in to comment.