diff --git a/include/rfl/Array.hpp b/include/rfl/Array.hpp new file mode 100644 index 00000000..33529059 --- /dev/null +++ b/include/rfl/Array.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_ARRAY_HPP_ +#define RFL_ARRAY_HPP_ + +#include + +#include "internal/to_std_array.hpp" + +namespace rfl { + +template + requires std::is_array_v +struct Array : internal::to_std_array_t { + using Type = T; + using Base = internal::to_std_array_t; + using Base::Base; + + Array() = default; + Array(const Base &t) : Base(t) {} + Array(Base &&t) : Base(std::move(t)) {} + Array(const T &t) : Base(reinterpret_cast(t)) {} + Array(T &&t) : Base(reinterpret_cast(t)) {} +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 48b29899..8d833179 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -7,6 +7,7 @@ #include #include +#include "Array.hpp" #include "Literal.hpp" #include "default.hpp" #include "internal/StringLiteral.hpp" @@ -134,31 +135,14 @@ 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)); -} - -template -inline auto make_field(const T& _value) { - return Field<_name, T>(_value); + using T0 = std::remove_cvref_t; + if constexpr (std::is_array_v) { + return Field<_name, T0>(rfl::Array(std::forward(_value))); + } else { + return Field<_name, T0>(std::forward(_value)); + } } } // namespace rfl diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 7ae4c89f..3a4cbfec 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -43,20 +43,16 @@ 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_(get(std::make_tuple(std::move(_fields.value_)...))) { + : values_(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_(get(std::make_tuple(_fields.value_...))) { + : values_(std::make_tuple(_fields.value_...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index f69e8ef8..f10bebd8 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Array.hpp" #include "is_flatten_field.hpp" #include "is_named_tuple.hpp" #include "ptr_tuple_t.hpp" @@ -95,14 +96,18 @@ auto make_tuple_from_element(T (*arr)[n]) { } } -auto make_tuple_from_element(auto &e) { - return std::make_tuple(e); +template +auto make_tuple_from_element(Array* arr) { + return make_tuple_from_element(reinterpret_cast(arr)); } -auto flatten_c_arrays(const auto &tuple) { +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>>()); + }(std::make_index_sequence< + std::tuple_size_v>>()); } /// Creates a struct of type T from a tuple by moving the underlying @@ -113,7 +118,8 @@ T move_from_tuple(TupleType&& _t) { using TargetTupleType = tuple_t>; - auto pointers = flatten_c_arrays(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/named_tuple_t.hpp b/include/rfl/named_tuple_t.hpp index bd1d7a5a..2f1cc635 100644 --- a/include/rfl/named_tuple_t.hpp +++ b/include/rfl/named_tuple_t.hpp @@ -9,6 +9,7 @@ #include "internal/is_named_tuple.hpp" #include "internal/ptr_named_tuple_t.hpp" #include "to_named_tuple.hpp" +#include "wrap_in_rfl_array_t.hpp" namespace rfl { @@ -17,7 +18,9 @@ struct remove_ptr; template struct remove_ptr> { - using FieldType = Field<_name, std::remove_cvref_t>>; + using FieldType = + Field<_name, + wrap_in_rfl_array_t>>>; }; template diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 607356b7..eaecbc67 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -15,6 +15,7 @@ #include "Parser_reference_wrapper.hpp" #include "Parser_rename.hpp" #include "Parser_result.hpp" +#include "Parser_rfl_array.hpp" #include "Parser_rfl_variant.hpp" #include "Parser_shared_ptr.hpp" #include "Parser_tagged_union.hpp" diff --git a/include/rfl/parsing/Parser_c_array.hpp b/include/rfl/parsing/Parser_c_array.hpp index bce7e0c9..dd539aa6 100644 --- a/include/rfl/parsing/Parser_c_array.hpp +++ b/include/rfl/parsing/Parser_c_array.hpp @@ -3,6 +3,7 @@ #include +#include "../Array.hpp" #include "../Result.hpp" #include "../always_false.hpp" #include "../internal/to_std_array.hpp" @@ -25,7 +26,8 @@ struct Parser { using CArray = T[_size]; using StdArray = internal::to_std_array_t; - static auto read(const R& _r, const InputVarType& _var) noexcept { + static Result> read(const R& _r, + const InputVarType& _var) noexcept { return Parser::read(_r, _var); } diff --git a/include/rfl/parsing/Parser_rfl_array.hpp b/include/rfl/parsing/Parser_rfl_array.hpp new file mode 100644 index 00000000..d0e51a26 --- /dev/null +++ b/include/rfl/parsing/Parser_rfl_array.hpp @@ -0,0 +1,41 @@ +#ifndef RFL_PARSING_PARSER_RFL_ARRAY_HPP_ +#define RFL_PARSING_PARSER_RFL_ARRAY_HPP_ + +#include + +#include "../Array.hpp" +#include "../Result.hpp" +#include "../always_false.hpp" +#include "../internal/to_std_array.hpp" +#include "Parser_array.hpp" +#include "Parser_base.hpp" + +namespace rfl { +namespace parsing { + +template + requires AreReaderAndWriter> +struct Parser> { + public: + using InputArrayType = typename R::InputArrayType; + using InputVarType = typename R::InputVarType; + + using OutputArrayType = typename W::OutputArrayType; + using OutputVarType = typename W::OutputVarType; + + using RflArray = Array; + using StdArray = internal::to_std_array_t; + + static Result read(const R& _r, const InputVarType& _var) noexcept { + return Parser::read(_r, _var); + } + + static OutputVarType write(const W& _w, const RflArray& _arr) noexcept { + return Parser::write(_w, _arr); + } +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/wrap_in_rfl_array_t.hpp b/include/rfl/wrap_in_rfl_array_t.hpp new file mode 100644 index 00000000..24ea6254 --- /dev/null +++ b/include/rfl/wrap_in_rfl_array_t.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_WRAP_IN_RFL_ARRAY_T_ +#define RFL_WRAP_IN_RFL_ARRAY_T_ + +#include + +#include "Array.hpp" + +namespace rfl { + +template +struct wrap_in_rfl_array { + using type = T; +}; + +template + requires std::is_array_v +struct wrap_in_rfl_array { + using type = Array; +}; + +template +using wrap_in_rfl_array_t = typename wrap_in_rfl_array::type; + +} // namespace rfl + +#endif