Made sure that to_named_tuple (and, by extension, rfl::replace) works properly

This commit is contained in:
Patrick Urbanke
2024-01-04 21:49:25 +01:00
parent 03f6f2ef44
commit b63c246f92
9 changed files with 100 additions and 18 deletions
+3 -2
View File
@@ -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 <internal::StringLiteral _name, class T>
struct Field {
/// The underlying type.
using Type = T;
using Type = internal::wrap_in_rfl_array_t<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 <class U>
Field(const Field<_name, U>& _field) : value_(_field.get()) {}
+2 -2
View File
@@ -14,7 +14,7 @@ namespace rfl {
/// Generates the struct T from a named tuple.
template <class T, class NamedTupleType>
T from_named_tuple(NamedTupleType&& _n) {
auto from_named_tuple(NamedTupleType&& _n) {
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
@@ -36,7 +36,7 @@ T from_named_tuple(NamedTupleType&& _n) {
/// Generates the struct T from a named tuple.
template <class T, class NamedTupleType>
T from_named_tuple(const NamedTupleType& _n) {
auto from_named_tuple(const NamedTupleType& _n) {
using RequiredType = std::remove_cvref_t<rfl::named_tuple_t<T>>;
if constexpr (!std::is_same<std::remove_cvref_t<NamedTupleType>,
RequiredType>()) {
+3
View File
@@ -1,6 +1,7 @@
#ifndef RFL_INTERNAL_ARRAY_HPP_
#define RFL_INTERNAL_ARRAY_HPP_
#include <array>
#include <type_traits>
#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;
+2 -2
View File
@@ -63,7 +63,7 @@ template <class T, class Pointers, class... Args>
auto move_from_pointers(Pointers& _ptrs, Args&&... _args) {
constexpr auto i = sizeof...(Args);
if constexpr (i == std::tuple_size_v<std::remove_cvref_t<Pointers>>) {
return T{std::move(_args)...};
return std::remove_cvref_t<T>{std::move(_args)...};
} else {
using FieldType = std::tuple_element_t<i, std::remove_cvref_t<Pointers>>;
@@ -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 <class T, class TupleType>
T move_from_tuple(TupleType&& _t) {
auto move_from_tuple(TupleType&& _t) {
auto ptr_tuple = tup_to_ptr_tuple(_t);
using TargetTupleType = tuple_t<std::remove_cvref_t<T>>;
+10 -1
View File
@@ -5,6 +5,7 @@
#include <type_traits>
#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<T>;
auto tup = bind_to_tuple(_t, [](auto& x) { return std::move(x); });
const auto fct = []<class T>(T& _v) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_array_v<Type>) {
return Array<Type>(_v);
} else {
return std::move(_v);
}
};
auto tup = bind_to_tuple(_t, fct);
return wrap_in_fields<FieldNames>(std::move(tup));
}
}
+40 -7
View File
@@ -1,24 +1,57 @@
#ifndef RFL_INTERNAL_TO_STD_ARRAY_HPP_
#define RFL_INTERNAL_TO_STD_ARRAY_HPP_
#include <cstdint>
#include <array>
#include <cstdint>
#include <type_traits>
namespace rfl::internal {
template <class T>
struct to_std_array {
using type = T;
struct StdArrayType {
using Type = T;
};
template <class T, std::size_t _n>
struct to_std_array<T[_n]> {
using type = std::array<typename to_std_array<std::remove_cvref_t<T>>::type, _n>;
template <class T, size_t _n>
struct StdArrayType<T[_n]> {
using Type =
std::array<typename StdArrayType<std::remove_cvref_t<T>>::Type, _n>;
using ValueType = std::remove_cvref_t<T>;
constexpr static size_t size = _n;
};
template <class T>
using to_std_array_t = to_std_array<T>::type;
using to_std_array_t = StdArrayType<T>::Type;
template <class T>
auto to_std_array(T&& _t) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_array_v<Type>) {
constexpr size_t n = StdArrayType<Type>::size;
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
return to_std_array_t<Type>({to_std_array(
std::forward<typename StdArrayType<Type>::ValueType>(_t[_i]))...});
};
return fct(std::make_index_sequence<n>());
} else {
return std::forward<T>(_t);
}
}
template <class T>
auto to_std_array(const T& _t) {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_array_v<Type>) {
constexpr size_t n = StdArrayType<Type>::size;
const auto fct = [&]<std::size_t... _i>(std::index_sequence<_i...>) {
return to_std_array_t<Type>({to_std_array(_t[_i])...});
};
return fct(std::make_index_sequence<n>());
} else {
return _t;
}
}
} // namespace rfl::internal
#endif
+28
View File
@@ -0,0 +1,28 @@
#include "test_c_array_class5.hpp"
#include <array>
#include <source_location>
#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
+5
View File
@@ -0,0 +1,5 @@
namespace test_c_array_class5 {
void test();
}
+7 -4
View File
@@ -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;
}