Made sure rfl::as works as intended

This commit is contained in:
Patrick Urbanke
2023-10-08 10:56:20 +02:00
parent 2346e78979
commit 266defd578
4 changed files with 110 additions and 26 deletions
+32 -26
View File
@@ -56,7 +56,8 @@ class NamedTuple {
/// Construct from a tuple containing fields.
NamedTuple(std::tuple<FieldTypes...>&& _tup)
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(_tup)) {
: NamedTuple(std::make_from_tuple<NamedTuple<FieldTypes...>>(
std::forward<std::tuple<FieldTypes...>>(_tup))) {
static_assert(no_duplicate_field_names(),
"Duplicate field names are not allowed");
}
@@ -129,12 +130,10 @@ class NamedTuple {
template <class... TupContent, class... Tail>
auto add(std::tuple<TupContent...>&& _tuple, Tail&&... _tail) {
if constexpr (sizeof...(Tail) > 0) {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
std::make_index_sequence<sizeof...(TupContent)>{})
.replace(_tail...);
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple))
.add(std::forward<Tail>(_tail)...);
} else {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
std::make_index_sequence<sizeof...(TupContent)>{});
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple));
}
}
@@ -143,12 +142,10 @@ class NamedTuple {
template <class... TupContent, class... Tail>
auto add(std::tuple<TupContent...>&& _tuple, Tail&&... _tail) const {
if constexpr (sizeof...(Tail) > 0) {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
std::make_index_sequence<sizeof...(TupContent)>{})
.replace(_tail...);
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple))
.add(std::forward<Tail>(_tail)...);
} else {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
std::make_index_sequence<sizeof...(TupContent)>{});
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple));
}
}
@@ -157,12 +154,9 @@ class NamedTuple {
template <class... TupContent, class... Tail>
auto add(const std::tuple<TupContent...>& _tuple, Tail&&... _tail) const {
if constexpr (sizeof...(Tail) > 0) {
return add_tuple(_tuple,
std::make_index_sequence<sizeof...(TupContent)>{})
.replace(_tail...);
return add_tuple(_tuple).add(std::forward<Tail>(_tail)...);
} else {
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple),
std::make_index_sequence<sizeof...(TupContent)>{});
return add_tuple(std::forward<std::tuple<TupContent...>>(_tuple));
}
}
@@ -170,14 +164,18 @@ class NamedTuple {
/// named tuples.
template <class... TupContent, class... Tail>
auto add(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
return add(
std::forward<std::tuple<TupContent...>>(_named_tuple.fields()),
std::forward<Tail>(_tail)...);
}
/// Template specialization for NamedTuple, so we can pass fields from other
/// named tuples.
template <class... TupContent, class... Tail>
auto add(NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) const {
return add(_named_tuple.fields(), std::forward<Tail>(_tail)...);
return add(
std::forward<std::tuple<TupContent...>>(_named_tuple.fields()),
std::forward<Tail>(_tail)...);
}
/// Template specialization for NamedTuple, so we can pass fields from other
@@ -346,17 +344,22 @@ class NamedTuple {
private:
/// Adds the elements of a tuple to a newly created named tuple,
/// and other elements to a newly created named tuple.
template <class Tuple, size_t... Is>
constexpr auto add_tuple(Tuple&& _tuple, const std::index_sequence<Is...>) {
return add(std::get<Is>(_tuple)...);
template <class... TupContent>
constexpr auto add_tuple(std::tuple<TupContent...>&& _tuple) {
const auto a = [this](auto&&... _fields) {
return add(std::forward<TupContent>(_fields)...);
};
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
}
/// Adds the elements of a tuple to a newly created named tuple,
/// and other elements to a newly created named tuple.
template <class Tuple, size_t... Is>
constexpr auto add_tuple(Tuple&& _tuple,
const std::index_sequence<Is...>) const {
return add(std::get<Is>(_tuple)...);
template <class... TupContent>
constexpr auto add_tuple(std::tuple<TupContent...>&& _tuple) const {
const auto a = [this](auto&&... _fields) {
return add(std::forward<TupContent>(_fields)...);
};
return std::apply(a, std::forward<std::tuple<TupContent...>>(_tuple));
}
/// Generates the fields.
@@ -480,10 +483,13 @@ class NamedTuple {
using FieldType = typename std::tuple_element<size, Fields>::type;
using T = std::decay_t<typename FieldType::Type>;
return retrieve_fields(
std::forward<std::tuple<OtherFieldTypes...>>(_other_fields),
std::forward<Args>(_args)...,
FieldType(std::get<index>(_other_fields).value_));
FieldType(
std::forward<T>(std::get<index>(_other_fields).value_)));
}
}
+36
View File
@@ -0,0 +1,36 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_as() {
std::cout << "test_as" << std::endl;
struct A {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
};
struct B {
rfl::Field<"f3", std::string> f3;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
struct C {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
const auto c = rfl::as<C>(std::move(a), std::move(b));
write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})");
}
+38
View File
@@ -0,0 +1,38 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_as_flatten() {
std::cout << "test_as_flatten" << std::endl;
struct A {
rfl::Field<"f1", std::string> f1;
rfl::Field<"f2", rfl::Box<std::string>> f2;
};
struct B {
rfl::Field<"f3", std::string> f3;
rfl::Field<"f4", rfl::Box<std::string>> f4;
};
struct C {
rfl::Flatten<A> a;
rfl::Flatten<B> b;
rfl::Field<"f5", int> f5;
};
auto a = A{.f1 = "Hello", .f2 = rfl::make_box<std::string>("World")};
auto b = B{.f3 = "Hello", .f4 = rfl::make_box<std::string>("World")};
const auto c =
rfl::as<C>(std::move(a), std::move(b), rfl::make_field<"f5">(5));
write_and_read(
c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})");
}
+4
View File
@@ -1,4 +1,6 @@
#include "test_array.hpp"
#include "test_as.hpp"
#include "test_as_flatten.hpp"
#include "test_box.hpp"
#include "test_custom_class1.hpp"
#include "test_custom_class2.hpp"
@@ -42,6 +44,8 @@ int main() {
test_replace();
test_replace_flatten();
test_as();
test_as_flatten();
return 0;
}