From 9aa882d390636df57077ca30acbacfd41e767dee Mon Sep 17 00:00:00 2001 From: Jose Diez Date: Fri, 27 Jan 2017 13:56:14 +0000 Subject: [PATCH] Problem: there is no way to create a deepcopy of a message instance Solution: add a dup method --- src/zproto_codec_c.gsl | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/zproto_codec_c.gsl b/src/zproto_codec_c.gsl index 144767b..c42f276 100644 --- a/src/zproto_codec_c.gsl +++ b/src/zproto_codec_c.gsl @@ -77,6 +77,10 @@ set_defaults () Destroy a $(class.name) instance + + Create a deep copy of a $(class.name) instance + + . if class.virtual ?= 1 Deserialize a $(class.name) from the specified message, popping @@ -285,6 +289,10 @@ $(CLASS.EXPORT_MACRO)$(class.name)_t * $(CLASS.EXPORT_MACRO)void $(class.name)_destroy ($(class.name)_t **self_p); +// Create a deep copy of a $(class.name) instance +$(CLASS.EXPORT_MACRO)$(class.name)_t * + $(class.name)_dup ($(class.name)_t *other); + .if class.virtual ?= 1 // Deserialize a $(class.name) from the specified message, popping // as many frames as needed. Returns 0 if OK, -1 if the read was @@ -655,6 +663,38 @@ $(class.name)_destroy ($(class.name)_t **self_p) } +// -------------------------------------------------------------------------- +// Create a deep copy of a $(class.name) instance + +$(class.name)_t * +$(class.name)_dup ($(class.name)_t *other) +{ + assert (other); + $(class.name)_t *copy = $(class.name)_new (); + + // Copy the routing and message id + $(class.name)_set_routing_id (copy, zframe_dup ($(class.name)_routing_id (other))); + $(class.name)_set_id (copy, $(class.name)_id (other)); + + // Copy the rest of the fields +.for class.field +. if type = "longstr" | type = "string" + $(class.name)_set_$(name) (copy, strdup ($(class.name)_$(name) (other))); +. elsif type = "uuid" | type = "hash" | type = "chunk" | type = "frame" | type = "msg" + $(class.name)_set_$(name) (copy, z$(type)_dup ($(class.name)_$(name) (other))); +. elsif type = "strings" + { + zlist_t *lcopy = zlist_dup ($(class.name)_$(name) (other)); + $(class.name)_set_$(name) (copy, &lcopy); + } +. elsif type = "number" + $(class.name)_set_$(name) (copy, $(class.name)_$(name) (other)); +. endif +.endfor + + return copy; +} + .macro calculate_frame_size size_t frame_size = 2 + 1; // Signature and message ID switch (self->id) {