Skip to content

Commit

Permalink
Serialize19 use indexed tuple access
Browse files Browse the repository at this point in the history
* allow serialize tuples with multiples of same type
  • Loading branch information
arBmind committed Jun 18, 2024
1 parent 6b461f0 commit 4aeae14
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/serialize19.lib/serialize19/serialize.Tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
namespace serialize19 {

template<Archive A, class... Ts> void serialize(A& a, tuple19::Tuple<Ts...>& tuple) {
using meta19::nullptr_to;
using meta19::type;
(serialize(a, tuple.amendOf(type<Ts>)), ...);
[&]<size_t... Is>(std::index_sequence<Is...>*) {
(serialize(a, tuple.template amendAt<Is>()), ...);
}(nullptr_to<std::make_index_sequence<sizeof...(Ts)>>);
}

} // namespace serialize19
13 changes: 13 additions & 0 deletions src/serialize19.lib/serialize19/serialize.Tuple.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ TEST(serialize, Tuple) {

EXPECT_EQ(output, input);
}

TEST(serialize, Tuple_ints) {
using T = Tuple<int, int, int>;
auto input = T{23, 42, 13};

auto buffer = dynamicWrite(input);

auto reader = ReadArchive{buffer.slice()};
auto output = T{};
serialize(reader, output);

EXPECT_EQ(output, input);
}
7 changes: 6 additions & 1 deletion src/serialize19.lib/serialize19/serialize.std_tuple.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#pragma once
#include "serialize.h"

#include <meta19/Type.h>
#include <tuple>

namespace serialize19 {

template<Archive A, class... Ts> void serialize(A& a, std::tuple<Ts...>& tuple) {
(serialize(a, std::get<Ts>(tuple)), ...);
using meta19::nullptr_to;
using meta19::type;
[&]<size_t... Is>(std::index_sequence<Is...>*) {
(serialize(a, std::get<Is>(tuple)), ...);
}(nullptr_to<std::make_index_sequence<sizeof...(Ts)>>);
}

} // namespace serialize19
13 changes: 13 additions & 0 deletions src/serialize19.lib/serialize19/serialize.std_tuple.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ TEST(serialize, std_tuple) {

EXPECT_EQ(output, input);
}

TEST(serialize, std_tuple_ints) {
using T = std::tuple<int, int, int>;
auto input = T{23, 13, 42};

auto buffer = dynamicWrite(input);

auto reader = ReadArchive{buffer.slice()};
auto output = T{};
serialize(reader, output);

EXPECT_EQ(output, input);
}

0 comments on commit 4aeae14

Please sign in to comment.