diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 5ebe4374..ae190b30 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -12,6 +12,7 @@ #include "internal/Array.hpp" #include "internal/StringLiteral.hpp" #include "internal/to_std_array.hpp" +#include "internal/wrap_in_rfl_array_t.hpp" namespace rfl { @@ -19,7 +20,7 @@ namespace rfl { template struct Field { /// The underlying type. - using Type = T; + using Type = internal::wrap_in_rfl_array_t; /// The name of the field. using Name = rfl::Literal<_name>; @@ -30,7 +31,7 @@ struct Field { Field(Field<_name, T>&& _field) noexcept = default; - Field(const Field<_name, Type>& _field) = default; + Field(const Field<_name, T>& _field) = default; template Field(const Field<_name, U>& _field) : value_(_field.get()) {} diff --git a/include/rfl/from_named_tuple.hpp b/include/rfl/from_named_tuple.hpp index 8a114e59..63a29838 100644 --- a/include/rfl/from_named_tuple.hpp +++ b/include/rfl/from_named_tuple.hpp @@ -14,7 +14,7 @@ namespace rfl { /// Generates the struct T from a named tuple. template -T from_named_tuple(NamedTupleType&& _n) { +auto from_named_tuple(NamedTupleType&& _n) { using RequiredType = std::remove_cvref_t>; if constexpr (!std::is_same, RequiredType>()) { @@ -36,7 +36,7 @@ T from_named_tuple(NamedTupleType&& _n) { /// Generates the struct T from a named tuple. template -T from_named_tuple(const NamedTupleType& _n) { +auto from_named_tuple(const NamedTupleType& _n) { using RequiredType = std::remove_cvref_t>; if constexpr (!std::is_same, RequiredType>()) { diff --git a/include/rfl/internal/Array.hpp b/include/rfl/internal/Array.hpp index 5341b578..9f75a35d 100644 --- a/include/rfl/internal/Array.hpp +++ b/include/rfl/internal/Array.hpp @@ -1,6 +1,7 @@ #ifndef RFL_INTERNAL_ARRAY_HPP_ #define RFL_INTERNAL_ARRAY_HPP_ +#include #include #include "to_std_array.hpp" @@ -17,6 +18,8 @@ struct Array { Array() = default; Array(const StdArrayType &_arr) : arr_(_arr) {} Array(StdArrayType &&_arr) : arr_(std::move(_arr)) {} + Array(const T &_arr) : arr_(to_std_array(_arr)) {} + Array(T &&_arr) : arr_(to_std_array(_arr)) {} ~Array() = default; diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index 3ddcd4e4..02b4d217 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -63,7 +63,7 @@ template auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { constexpr auto i = sizeof...(Args); if constexpr (i == std::tuple_size_v>) { - return T{std::move(_args)...}; + return std::remove_cvref_t{std::move(_args)...}; } else { using FieldType = std::tuple_element_t>; @@ -116,7 +116,7 @@ auto flatten_c_arrays(const auto& _tup) { /// Creates a struct of type T from a tuple by moving the underlying /// fields. template -T move_from_tuple(TupleType&& _t) { +auto move_from_tuple(TupleType&& _t) { auto ptr_tuple = tup_to_ptr_tuple(_t); using TargetTupleType = tuple_t>; diff --git a/include/rfl/internal/move_to_field_tuple.hpp b/include/rfl/internal/move_to_field_tuple.hpp index 7841ae27..b7d10a40 100644 --- a/include/rfl/internal/move_to_field_tuple.hpp +++ b/include/rfl/internal/move_to_field_tuple.hpp @@ -5,6 +5,7 @@ #include #include "../field_names_t.hpp" +#include "Array.hpp" #include "bind_to_tuple.hpp" #include "has_fields.hpp" #include "is_named_tuple.hpp" @@ -22,7 +23,15 @@ auto move_to_field_tuple(OriginalStruct&& _t) { return bind_to_tuple(_t, [](auto& x) { return std::move(x); }); } else { using FieldNames = field_names_t; - auto tup = bind_to_tuple(_t, [](auto& x) { return std::move(x); }); + const auto fct = [](T& _v) { + using Type = std::remove_cvref_t; + if constexpr (std::is_array_v) { + return Array(_v); + } else { + return std::move(_v); + } + }; + auto tup = bind_to_tuple(_t, fct); return wrap_in_fields(std::move(tup)); } } diff --git a/include/rfl/internal/to_std_array.hpp b/include/rfl/internal/to_std_array.hpp index 715399b1..8703eb47 100644 --- a/include/rfl/internal/to_std_array.hpp +++ b/include/rfl/internal/to_std_array.hpp @@ -1,24 +1,57 @@ #ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_ #define RFL_INTERNAL_TO_STD_ARRAY_HPP_ -#include #include +#include +#include namespace rfl::internal { template -struct to_std_array { - using type = T; +struct StdArrayType { + using Type = T; }; -template -struct to_std_array { - using type = std::array>::type, _n>; +template +struct StdArrayType { + using Type = + std::array>::Type, _n>; + using ValueType = std::remove_cvref_t; + constexpr static size_t size = _n; }; template -using to_std_array_t = to_std_array::type; +using to_std_array_t = StdArrayType::Type; +template +auto to_std_array(T&& _t) { + using Type = std::remove_cvref_t; + if constexpr (std::is_array_v) { + constexpr size_t n = StdArrayType::size; + const auto fct = [&](std::index_sequence<_i...>) { + return to_std_array_t({to_std_array( + std::forward::ValueType>(_t[_i]))...}); + }; + return fct(std::make_index_sequence()); + } else { + return std::forward(_t); + } } +template +auto to_std_array(const T& _t) { + using Type = std::remove_cvref_t; + if constexpr (std::is_array_v) { + constexpr size_t n = StdArrayType::size; + const auto fct = [&](std::index_sequence<_i...>) { + return to_std_array_t({to_std_array(_t[_i])...}); + }; + return fct(std::make_index_sequence()); + } else { + return _t; + } +} + +} // namespace rfl::internal + #endif diff --git a/tests/json/test_c_array_class5.cpp b/tests/json/test_c_array_class5.cpp new file mode 100644 index 00000000..55fe7b29 --- /dev/null +++ b/tests/json/test_c_array_class5.cpp @@ -0,0 +1,28 @@ +#include "test_c_array_class5.hpp" + +#include +#include + +#include "rfl.hpp" +#include "rfl/json.hpp" +#include "write_and_read.hpp" + +namespace test_c_array_class5 { + +struct Test5 { + int a[3]; + int b[3]; + int c[2][2]; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + Test5 t1 = {.a = {1, 2, 3}, .b = {4, 5, 6}, .c = {{7, 8}, {9, 10}}}; + + const auto t2 = rfl::replace(t1, rfl::make_field<"b", int[3]>({1, 2, 3})); + + write_and_read(t2, R"({"a":[1,2,3],"b":[1,2,3],"c":[[7,8],[9,10]]})"); +} + +} // namespace test_c_array_class5 diff --git a/tests/json/test_c_array_class5.hpp b/tests/json/test_c_array_class5.hpp new file mode 100644 index 00000000..4b87e475 --- /dev/null +++ b/tests/json/test_c_array_class5.hpp @@ -0,0 +1,5 @@ +namespace test_c_array_class5 { + +void test(); + +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index b3471bc0..93e1bd30 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -12,6 +12,7 @@ #include "test_c_array_class2.hpp" #include "test_c_array_class3.hpp" #include "test_c_array_class4.hpp" +#include "test_c_array_class5.hpp" #include "test_custom_class1.hpp" #include "test_custom_class2.hpp" #include "test_custom_class3.hpp" @@ -116,6 +117,12 @@ int main() { test_custom_class3::test(); test_custom_class4::test(); + test_c_array_class1::test(); + test_c_array_class2::test(); + test_c_array_class3::test(); + test_c_array_class4::test(); + test_c_array_class5::test(); + test_replace::test(); test_replace2::test(); test_replace_flatten::test(); @@ -139,9 +146,5 @@ int main() { test_meta_fields::test(); - test_c_array_class1::test(); - test_c_array_class2::test(); - test_c_array_class3::test(); - test_c_array_class4::test(); return 0; }