mirror of
https://github.com/monero-project/monero.git
synced 2024-10-01 11:49:47 -04:00
Merge pull request #9257
d6609b8
serialization: do any N-tuple (jeffro256)
This commit is contained in:
commit
eb35250895
@ -1,4 +1,4 @@
|
|||||||
// Copyright (c) 2014-2023, The Monero Project
|
// Copyright (c) 2014-2024, The Monero Project
|
||||||
//
|
//
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
//
|
//
|
||||||
@ -29,7 +29,7 @@
|
|||||||
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <memory>
|
#include <tuple>
|
||||||
#include "serialization.h"
|
#include "serialization.h"
|
||||||
|
|
||||||
namespace serialization
|
namespace serialization
|
||||||
@ -51,119 +51,71 @@ namespace serialization
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template <bool> class Archive, class E0, class E1, class E2>
|
template <size_t I, bool BackwardsCompat, bool W, template <bool> class Archive, typename... Ts>
|
||||||
inline bool do_serialize(Archive<false>& ar, std::tuple<E0,E1,E2>& p)
|
bool do_serialize_tuple_nth(Archive<W>& ar, std::tuple<Ts...>& v)
|
||||||
{
|
{
|
||||||
size_t cnt;
|
static constexpr const size_t tuple_size = std::tuple_size<std::tuple<Ts...>>();
|
||||||
ar.begin_array(cnt);
|
static_assert(I <= tuple_size, "bad call");
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
if (cnt != 3)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<0>(p)))
|
if constexpr (I == 0)
|
||||||
return false;
|
{
|
||||||
if (!ar.good())
|
// If BackwardsCompat is true, we serialize the size of 3-tuples and 4-tuples
|
||||||
return false;
|
if constexpr (BackwardsCompat && (tuple_size == 3 || tuple_size == 4))
|
||||||
ar.delimit_array();
|
{
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<1>(p)))
|
size_t cnt = tuple_size;
|
||||||
return false;
|
ar.begin_array(cnt);
|
||||||
if (!ar.good())
|
if (cnt != tuple_size)
|
||||||
return false;
|
return false;
|
||||||
ar.delimit_array();
|
}
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<2>(p)))
|
else
|
||||||
return false;
|
{
|
||||||
if (!ar.good())
|
ar.begin_array();
|
||||||
return false;
|
}
|
||||||
|
}
|
||||||
|
else if constexpr (I < tuple_size)
|
||||||
|
{
|
||||||
|
ar.delimit_array();
|
||||||
|
}
|
||||||
|
|
||||||
ar.end_array();
|
if constexpr (I == tuple_size)
|
||||||
return true;
|
{
|
||||||
|
ar.end_array();
|
||||||
|
return ar.good();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!::serialization::detail::serialize_tuple_element(ar, std::get<I>(v))
|
||||||
|
|| !ar.good())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return do_serialize_tuple_nth<I + 1, BackwardsCompat>(ar, v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template <bool> class Archive, class E0, class E1, class E2>
|
template <bool BackwardsCompat, bool W, template <bool> class Archive, typename... Ts>
|
||||||
inline bool do_serialize(Archive<true>& ar, std::tuple<E0,E1,E2>& p)
|
bool do_serialize_tuple(Archive<W>& ar, std::tuple<Ts...>& v)
|
||||||
{
|
{
|
||||||
ar.begin_array(3);
|
return do_serialize_tuple_nth<0, BackwardsCompat>(ar, v);
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<0>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<1>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<2>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.end_array();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template <bool> class Archive, class E0, class E1, class E2, class E3>
|
template <bool W, template <bool> class Archive, typename... Ts>
|
||||||
inline bool do_serialize(Archive<false>& ar, std::tuple<E0,E1,E2,E3>& p)
|
bool do_serialize(Archive<W>& ar, std::tuple<Ts...>& v)
|
||||||
{
|
{
|
||||||
size_t cnt;
|
return do_serialize_tuple<true>(ar, v);
|
||||||
ar.begin_array(cnt);
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
if (cnt != 4)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<0>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<1>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<2>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if (!::serialization::detail::serialize_tuple_element(ar, std::get<3>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ar.end_array();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <template <bool> class Archive, class E0, class E1, class E2, class E3>
|
#define TUPLE_COMPACT_FIELDS(v) \
|
||||||
inline bool do_serialize(Archive<true>& ar, std::tuple<E0,E1,E2,E3>& p)
|
do { \
|
||||||
{
|
if (!do_serialize_tuple<false>(ar, v) || !ar.good()) \
|
||||||
ar.begin_array(4);
|
return false; \
|
||||||
if (!ar.good())
|
} while (0);
|
||||||
return false;
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<0>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<1>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<2>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.delimit_array();
|
|
||||||
if(!::serialization::detail::serialize_tuple_element(ar, std::get<3>(p)))
|
|
||||||
return false;
|
|
||||||
if (!ar.good())
|
|
||||||
return false;
|
|
||||||
ar.end_array();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#define TUPLE_COMPACT_FIELD_N(t, v) \
|
||||||
|
do { \
|
||||||
|
ar.tag(t); \
|
||||||
|
TUPLE_COMPACT_FIELDS(v); \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
#define TUPLE_COMPACT_FIELD(f) TUPLE_COMPACT_FIELD_N(#f, f)
|
||||||
|
|
||||||
|
#define TUPLE_COMPACT_FIELD_F(f) TUPLE_COMPACT_FIELD_N(#f, v.f)
|
||||||
|
@ -61,6 +61,11 @@ struct Struct
|
|||||||
int32_t a;
|
int32_t a;
|
||||||
int32_t b;
|
int32_t b;
|
||||||
char blob[8];
|
char blob[8];
|
||||||
|
|
||||||
|
bool operator==(const Struct &other) const
|
||||||
|
{
|
||||||
|
return a == other.a && b == other.b && 0 == memcmp(blob, other.blob, sizeof(blob));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
@ -1212,3 +1217,90 @@ TEST(Serialization, adl_free_function)
|
|||||||
const std::string expected = "{\"custom_fieldname\": " + std::to_string(msg.size()) + '"' + epee::string_tools::buff_to_hex_nodelimer(msg) + "\"}";
|
const std::string expected = "{\"custom_fieldname\": " + std::to_string(msg.size()) + '"' + epee::string_tools::buff_to_hex_nodelimer(msg) + "\"}";
|
||||||
EXPECT_EQ(expected, ss.str());
|
EXPECT_EQ(expected, ss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
using Tuple3 = std::tuple<uint16_t, std::string, uint64_t>;
|
||||||
|
using Tuple4 = std::tuple<int32_t, std::string, uint64_t, Struct>;
|
||||||
|
|
||||||
|
TEST(Serialization, tuple_3_4_backwards_compatibility)
|
||||||
|
{
|
||||||
|
std::string serialized;
|
||||||
|
|
||||||
|
////////////////////////////////////////
|
||||||
|
|
||||||
|
Tuple3 t3{1876, "Hullabaloo", 1963};
|
||||||
|
EXPECT_TRUE(::serialization::dump_binary(t3, serialized));
|
||||||
|
|
||||||
|
EXPECT_EQ("0354070a48756c6c6162616c6f6fab0f",
|
||||||
|
epee::string_tools::buff_to_hex_nodelimer(serialized));
|
||||||
|
|
||||||
|
Tuple3 t3_recovered;
|
||||||
|
EXPECT_TRUE(::serialization::parse_binary(serialized, t3_recovered));
|
||||||
|
EXPECT_EQ(t3, t3_recovered);
|
||||||
|
|
||||||
|
/////////////////////////////////////////
|
||||||
|
|
||||||
|
Tuple4 t4{1999, "Caneck Caneck", (uint64_t)-1, {20229, 242, {1, 1, 2, 3, 5, 8, 13, 21}}};
|
||||||
|
EXPECT_TRUE(::serialization::dump_binary(t4, serialized));
|
||||||
|
|
||||||
|
EXPECT_EQ("04cf0700000d43616e65636b2043616e65636bffffffffffffffffff01054f0000f20000000101020305080d15",
|
||||||
|
epee::string_tools::buff_to_hex_nodelimer(serialized));
|
||||||
|
|
||||||
|
Tuple4 t4_recovered;
|
||||||
|
EXPECT_TRUE(::serialization::parse_binary(serialized, t4_recovered));
|
||||||
|
EXPECT_EQ(t4, t4_recovered);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Tupler
|
||||||
|
{
|
||||||
|
std::tuple<> t0;
|
||||||
|
std::tuple<int8_t> t1;
|
||||||
|
std::tuple<uint8_t, int16_t> t2;
|
||||||
|
Tuple3 t3_backcompat;
|
||||||
|
Tuple3 t3_compact;
|
||||||
|
Tuple4 t4_backcompat;
|
||||||
|
Tuple4 t4_compact;
|
||||||
|
std::tuple<uint32_t, std::string, bool, int64_t, Struct> t5;
|
||||||
|
|
||||||
|
BEGIN_SERIALIZE_OBJECT()
|
||||||
|
FIELD(t0)
|
||||||
|
FIELD(t1)
|
||||||
|
FIELD(t2)
|
||||||
|
FIELD(t3_backcompat)
|
||||||
|
TUPLE_COMPACT_FIELD(t3_compact)
|
||||||
|
FIELD(t4_backcompat)
|
||||||
|
TUPLE_COMPACT_FIELD(t4_compact)
|
||||||
|
TUPLE_COMPACT_FIELD(t5)
|
||||||
|
END_SERIALIZE()
|
||||||
|
};
|
||||||
|
|
||||||
|
bool operator==(const Tupler &a, const Tupler &b)
|
||||||
|
{
|
||||||
|
return a.t0 == b.t0 && a.t1 == b.t1 && a.t2 == b.t2 && a.t3_backcompat == b.t3_backcompat &&
|
||||||
|
a.t3_compact == b.t3_compact && a.t4_backcompat == b.t4_backcompat && a.t5 == b.t5;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Serialization, tuple_many_tuples)
|
||||||
|
{
|
||||||
|
Tupler tupler{
|
||||||
|
{},
|
||||||
|
{69},
|
||||||
|
{42, 420},
|
||||||
|
{1876, "Hullabaloo", 1963},
|
||||||
|
{1876, "Hullabaloo", 1963},
|
||||||
|
{1999, "Caneck Caneck", (uint64_t)-1, {20229, 242, {1, 1, 2, 3, 5, 8, 13, 21}}},
|
||||||
|
{1999, "Caneck Caneck", (uint64_t)-1, {20229, 242, {1, 1, 2, 3, 5, 8, 13, 21}}},
|
||||||
|
{72982, "He is now rising from affluence to poverty.", false, 256,
|
||||||
|
{
|
||||||
|
13, 37, { 1, 1, 1, 2, 3, 7, 11, 26 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::string serialized;
|
||||||
|
EXPECT_TRUE(::serialization::dump_binary(tupler, serialized));
|
||||||
|
|
||||||
|
Tupler tupler_recovered;
|
||||||
|
EXPECT_TRUE(::serialization::parse_binary(serialized, tupler_recovered));
|
||||||
|
|
||||||
|
EXPECT_EQ(tupler, tupler_recovered);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user