From a16cf3015f12d124cf56be9c50f38e7fb345e574 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Dec 2020 14:02:59 +0100 Subject: [PATCH] Fix deserializing into existing objects `read_object()` was trying to create copies. --- src/common/communication/common.h | 12 ++++++++---- src/common/communication/vst3.h | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/common/communication/common.h b/src/common/communication/common.h index b6409b0e..e430864c 100644 --- a/src/common/communication/common.h +++ b/src/common/communication/common.h @@ -104,7 +104,7 @@ inline void write_object(Socket& socket, const T& object) { * @relates write_object */ template -inline T read_object(Socket& socket, std::vector& buffer, T& object) { +inline T& read_object(Socket& socket, std::vector& buffer, T& object) { // See the note above on the use of `uint64_t` instead of `size_t` std::array message_length; boost::asio::read(socket, boost::asio::buffer(message_length)); @@ -141,7 +141,9 @@ inline T read_object(Socket& socket, std::vector& buffer, T& object) { template inline T read_object(Socket& socket, std::vector& buffer) { T object; - return read_object(socket, buffer, object); + read_object(socket, buffer, object); + + return object; } /** @@ -151,7 +153,7 @@ inline T read_object(Socket& socket, std::vector& buffer) { * @overload */ template -inline T read_object(Socket& socket, T& object) { +inline T& read_object(Socket& socket, T& object) { std::vector buffer(64); return read_object(socket, buffer, object); } @@ -166,7 +168,9 @@ template inline T read_object(Socket& socket) { T object; std::vector buffer(64); - return read_object(socket, buffer, object); + read_object(socket, buffer, object); + + return object; } /** diff --git a/src/common/communication/vst3.h b/src/common/communication/vst3.h index d60c199f..c279a640 100644 --- a/src/common/communication/vst3.h +++ b/src/common/communication/vst3.h @@ -69,6 +69,8 @@ class Vst3MessageHandler : public AdHocSocketHandler { * another thread, then this will create a new socket connection and send * the event there instead. * + * @param object The request object to send. Often a marker struct to ask + * for a specific object to be returned. * @param logging A pair containing a logger instance and whether or not * this is for sending host -> plugin control messages. If set to false, * then this indicates that this `Vst3MessageHandler` is handling plugin @@ -94,13 +96,18 @@ class Vst3MessageHandler : public AdHocSocketHandler { * an existing object. * * TODO: We might also need overloads that reuse buffers + * TODO: Rename to `receive_into()` to make it more apparent what's + * happening + * + * @param response_object The object to deserialize into. * * @overload */ template - void send_message(const T& object, - typename T::Response& response_object, - std::optional> logging) { + typename T::Response& send_message( + const T& object, + typename T::Response& response_object, + std::optional> logging) { using TResponse = typename T::Response; if (logging) { @@ -126,6 +133,8 @@ class Vst3MessageHandler : public AdHocSocketHandler { auto [logger, is_host_vst] = *logging; logger.log_response(!is_host_vst, response_object); } + + return response_object; } /**