serialization: support passing extra args to fields in DSL

This PR is upstreaming changes in the Seraphis lib here: https://github.com/UkoeHB/monero/pull/39. The changes to the serialization header allow clean passing
of extra arguments to field serialization in the DSL. This is used mainly to pass implied sizes of containers during deserialization to make the format more
compact. For example, if my object has two containers A & B which must be the same size, I can serialize only the size of container A. Then, during
deserialization, when I deserialize A, I can then use A's size to deserialize B.

Depends on #9286.
This commit is contained in:
jeffro256 2024-04-11 11:42:11 -05:00
parent b2c59c498c
commit 59cddbb9ca
No known key found for this signature in database
GPG Key ID: 6F79797A6E392442

View File

@ -50,6 +50,8 @@
#include <boost/type_traits/integral_constant.hpp>
#include <boost/mpl/bool.hpp>
#include "common/va_args.h"
/*! \struct is_blob_type / is_blob_forced
*
* \brief descriptors for dispatching serialize: whether to take byte-wise copy/store to type
@ -93,6 +95,15 @@ inline bool do_serialize(Archive &ar, bool &v)
ar.serialize_blob(&v, sizeof(v));
return true;
}
template <class Archive, class T, typename... Args>
inline auto do_serialize(Archive &ar, T &v, Args&&... args)
-> decltype(do_serialize_object(ar, v, args...), true)
{
ar.begin_object();
const bool r = do_serialize_object(ar, v, args...);
ar.end_object();
return r && ar.good();
}
/* the following add a trait to a set and define the serialization DSL*/
@ -180,18 +191,9 @@ inline bool do_serialize(Archive &ar, bool &v)
* VARINT_FIELD_F(). Otherwise, this macro is similar to
* BEGIN_SERIALIZE_OBJECT(), as you should list only field serializations.
*/
#define BEGIN_SERIALIZE_OBJECT_FN(stype) \
#define BEGIN_SERIALIZE_OBJECT_FN(stype, ...) \
template <bool W, template <bool> class Archive> \
bool do_serialize_object(Archive<W> &ar, stype &v); \
template <bool W, template <bool> class Archive> \
bool do_serialize(Archive<W> &ar, stype &v) { \
ar.begin_object(); \
bool r = do_serialize_object(ar, v); \
ar.end_object(); \
return r; \
} \
template <bool W, template <bool> class Archive> \
bool do_serialize_object(Archive<W> &ar, stype &v) { \
bool do_serialize_object(Archive<W> &ar, stype &v VA_ARGS_COMMAPREFIX(__VA_ARGS__)) {
/*! \macro PREPARE_CUSTOM_VECTOR_SERIALIZATION
*/
@ -209,10 +211,10 @@ inline bool do_serialize(Archive &ar, bool &v)
*
* \brief serializes a field \a f tagged \a t
*/
#define FIELD_N(t, f) \
#define FIELD_N(t, f, ...) \
do { \
ar.tag(t); \
bool r = do_serialize(ar, f); \
bool r = do_serialize(ar, f VA_ARGS_COMMAPREFIX(__VA_ARGS__)); \
if (!r || !ar.good()) return false; \
} while(0);
@ -231,7 +233,7 @@ inline bool do_serialize(Archive &ar, bool &v)
*
* \brief tags the field with the variable name and then serializes it (for use in a free function)
*/
#define FIELD_F(f) FIELD_N(#f, v.f)
#define FIELD_F(f, ...) FIELD_N(#f, v.f VA_ARGS_COMMAPREFIX(__VA_ARGS__))
/*! \macro FIELDS(f)
*