Skip to content

Commit

Permalink
serialization: support passing extra args to fields in DSL
Browse files Browse the repository at this point in the history
This PR is upstreaming changes in the Seraphis lib here: UkoeHB#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 monero-project#9286.
  • Loading branch information
jeffro256 committed Apr 11, 2024
1 parent bc05e72 commit f5b77ee
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/serialization/serialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
* \brief a descriptor for dispatching serialize
Expand Down Expand Up @@ -88,6 +90,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*/

Expand Down Expand Up @@ -158,18 +169,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) \
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) { \
#define BEGIN_SERIALIZE_OBJECT_FN(stype, ...) \
template <bool W, template <bool> class Archive> \
bool do_serialize_object(Archive<W> &ar, stype &v VA_ARGS_COMMAPREFIX(__VA_ARGS__)) {

/*! \macro PREPARE_CUSTOM_VECTOR_SERIALIZATION
*/
Expand All @@ -187,10 +189,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);

Expand All @@ -209,7 +211,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)
*
Expand Down

0 comments on commit f5b77ee

Please sign in to comment.