aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjeffro256 <jeffro256@tutanota.com>2024-02-21 12:40:35 -0600
committerjeffro256 <jeffro256@tutanota.com>2024-02-24 11:24:47 -0600
commit2a2da7994356e56043d744cd4650540861a833f6 (patch)
tree67ef8d12aa771eb1827d9ae3cb8186379f625e26
parentMerge pull request #9126 (diff)
downloadmonero-2a2da7994356e56043d744cd4650540861a833f6.tar.xz
free function serialization DSL
Example usage for Seraphis types (in global or `sp` namespace): ``` BEGIN_SERIALIZE_OBJECT_FN(sp::SpCoinbaseEnoteCore) FIELD_F(onetime_address) VARINT_FIELD_F(amount) END_SERIALIZE() BEGIN_SERIALIZE_OBJECT_FN(sp::SpEnoteCore) FIELD_F(onetime_address) FIELD_F(amount_commitment) END_SERIALIZE() ```
-rw-r--r--src/serialization/serialization.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/serialization/serialization.h b/src/serialization/serialization.h
index 2911aecb5..d1f97e324 100644
--- a/src/serialization/serialization.h
+++ b/src/serialization/serialization.h
@@ -123,6 +123,17 @@ inline bool do_serialize(Archive &ar, bool &v)
template <bool W, template <bool> class Archive> \
bool member_do_serialize(Archive<W> &ar) {
+/*! \macro BEGIN_SERIALIZE_FN
+ *
+ * \brief Begins the environment of the DSL as a free function
+ *
+ * Inside, instead of FIELD() and VARINT_FIELD(), use FIELD_F() and
+ * VARINT_FIELD_F(). Otherwise, this macro is similar to BEGIN_SERIALIZE().
+ */
+#define BEGIN_SERIALIZE_FN(stype) \
+ template <bool W, template <bool> class Archive> \
+ bool do_serialize(Archive<W> &ar, stype &v) {
+
/*! \macro BEGIN_SERIALIZE_OBJECT
*
* \brief begins the environment of the DSL
@@ -139,6 +150,27 @@ inline bool do_serialize(Archive &ar, bool &v)
template <bool W, template <bool> class Archive> \
bool do_serialize_object(Archive<W> &ar){
+/*! \macro BEGIN_SERIALIZE_OBJECT_FN
+ *
+ * \brief Begins the environment of the DSL as a free function in object-style
+ *
+ * Inside, instead of FIELD() and VARINT_FIELD(), use FIELD_F() and
+ * 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) { \
+
/*! \macro PREPARE_CUSTOM_VECTOR_SERIALIZATION
*/
#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \
@@ -173,6 +205,12 @@ inline bool do_serialize(Archive &ar, bool &v)
if (!r || !ar.good()) return false; \
} while(0);
+/*! \macro FIELD_F(f)
+ *
+ * \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)
+
/*! \macro FIELDS(f)
*
* \brief does not add a tag to the serialized value
@@ -204,6 +242,12 @@ inline bool do_serialize(Archive &ar, bool &v)
if (!ar.good()) return false; \
} while(0);
+/*! \macro VARINT_FIELD_F(f)
+ *
+ * \brief tags and serializes the varint \a f (for use in a free function)
+ */
+#define VARINT_FIELD_F(f) VARINT_FIELD_N(#f, v.f)
+
/*! \macro MAGIC_FIELD(m)
*/
#define MAGIC_FIELD(m) \