From fe956b37790fc452a5c3ce76a8f2f1e151777a62 Mon Sep 17 00:00:00 2001 From: hazby2002 <1094501217@qq.com> Date: Thu, 28 Dec 2023 13:37:56 +0800 Subject: [PATCH] Implement read operation of Parser for c-arrays. --- include/rfl/Field.hpp | 18 ++++++++++++++++ include/rfl/NamedTuple.hpp | 8 ++++++-- include/rfl/Result.hpp | 11 +++++++++- include/rfl/internal/move_from_tuple.hpp | 26 ++++++++++++++++++++++-- include/rfl/internal/to_std_array.hpp | 24 ++++++++++++++++++++++ include/rfl/parsing/Parser_c_array.hpp | 8 ++++++-- 6 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 include/rfl/internal/to_std_array.hpp diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 179d9c1e..48b29899 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -10,6 +10,7 @@ #include "Literal.hpp" #include "default.hpp" #include "internal/StringLiteral.hpp" +#include "internal/to_std_array.hpp" namespace rfl { @@ -133,6 +134,23 @@ struct Field { Type value_; }; +/// Used to define a field in the NamedTuple. +template +struct Field<_name, T[_n]> : Field<_name, internal::to_std_array_t> { + /// The underlying type. + using Type = T[_n]; + + using StdArray = internal::to_std_array_t; + + using Field<_name, StdArray>::Field; + + Type& c_array() { return reinterpret_cast(this->value_); } + + const Type& c_array() const { + return reinterpret_cast(this->value_); + } +}; + template inline auto make_field(T&& _value) { return Field<_name, T>(std::forward(_value)); diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 3a4cbfec..7ae4c89f 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -43,16 +43,20 @@ class NamedTuple { "Duplicate field names are not allowed"); } + static auto get(auto &&tuple) { + return reinterpret_cast::type::Type...>&&>(tuple); + } + /// Construct from the fields. NamedTuple(FieldTypes&&... _fields) - : values_(std::make_tuple(std::move(_fields.value_)...)) { + : values_(get(std::make_tuple(std::move(_fields.value_)...))) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the fields. NamedTuple(const FieldTypes&... _fields) - : values_(std::make_tuple(_fields.value_...)) { + : values_(get(std::make_tuple(_fields.value_...))) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } diff --git a/include/rfl/Result.hpp b/include/rfl/Result.hpp index 096f517d..b3be13ef 100644 --- a/include/rfl/Result.hpp +++ b/include/rfl/Result.hpp @@ -10,6 +10,8 @@ #include #include +#include "internal/to_std_array.hpp" + namespace rfl { /// To be returned @@ -342,7 +344,14 @@ class Result { /// This is simply to get the compilation to pass. template -class Result : public Result> {}; +struct Result : public Result> { + using StdArray = internal::to_std_array_t; + using Base = Result; + using Base::Base; + + Result(const Base& other) : Base(other) {} + Result(Base&& other) : Base(std::move(other)) {} +}; } // namespace rfl diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index b374ea28..f69e8ef8 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -82,6 +82,29 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { } } +template +auto make_tuple_from_element(T (*arr)[n]) { + if constexpr (std::is_array_v) { + return [&](std::index_sequence) { + return std::tuple_cat(make_tuple_from_element(&(*arr)[i])...); + }(std::make_index_sequence()); + } else { + return [&](std::index_sequence) { + return std::make_tuple(&(*arr)[i]...); + }(std::make_index_sequence()); + } +} + +auto make_tuple_from_element(auto &e) { + return std::make_tuple(e); +} + +auto flatten_c_arrays(const auto &tuple) { + return [&](std::index_sequence) { + return std::tuple_cat(make_tuple_from_element(std::get(tuple))...); + }(std::make_index_sequence>>()); +} + /// Creates a struct of type T from a tuple by moving the underlying /// fields. template @@ -90,8 +113,7 @@ T move_from_tuple(TupleType&& _t) { using TargetTupleType = tuple_t>; - auto pointers = unflatten_ptr_tuple(ptr_tuple); - + auto pointers = flatten_c_arrays(unflatten_ptr_tuple(ptr_tuple)); return move_from_pointers(pointers); } diff --git a/include/rfl/internal/to_std_array.hpp b/include/rfl/internal/to_std_array.hpp new file mode 100644 index 00000000..715399b1 --- /dev/null +++ b/include/rfl/internal/to_std_array.hpp @@ -0,0 +1,24 @@ +#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_ +#define RFL_INTERNAL_TO_STD_ARRAY_HPP_ + +#include +#include + +namespace rfl::internal { + +template +struct to_std_array { + using type = T; +}; + +template +struct to_std_array { + using type = std::array>::type, _n>; +}; + +template +using to_std_array_t = to_std_array::type; + +} + +#endif diff --git a/include/rfl/parsing/Parser_c_array.hpp b/include/rfl/parsing/Parser_c_array.hpp index 0755e495..bce7e0c9 100644 --- a/include/rfl/parsing/Parser_c_array.hpp +++ b/include/rfl/parsing/Parser_c_array.hpp @@ -5,6 +5,8 @@ #include "../Result.hpp" #include "../always_false.hpp" +#include "../internal/to_std_array.hpp" +#include "Parser_array.hpp" #include "Parser_base.hpp" namespace rfl { @@ -21,8 +23,10 @@ struct Parser { using OutputVarType = typename W::OutputVarType; using CArray = T[_size]; - static Result read(const R& _r, const InputVarType& _var) noexcept { - static_assert(always_false_v, "Unimplemented"); + using StdArray = internal::to_std_array_t; + + static auto read(const R& _r, const InputVarType& _var) noexcept { + return Parser::read(_r, _var); } static OutputVarType write(const W& _w, const CArray& _arr) noexcept {