From 57349c587bafb1fc739e4e79538bd89bab326a6f Mon Sep 17 00:00:00 2001 From: Bruno Iljazovic Date: Thu, 2 Nov 2023 13:21:56 +0100 Subject: [PATCH] [mqtt-client] add move constructor Summary: Resolves T12913 Reviewers: ivica Reviewed By: ivica Subscribers: korina Maniphest Tasks: T12913 Differential Revision: https://repo.mireo.local/D26376 --- include/async_mqtt5/mqtt_client.hpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/include/async_mqtt5/mqtt_client.hpp b/include/async_mqtt5/mqtt_client.hpp index 6f39b83..1c926b4 100644 --- a/include/async_mqtt5/mqtt_client.hpp +++ b/include/async_mqtt5/mqtt_client.hpp @@ -85,8 +85,19 @@ public: mqtt_client(context.get_executor(), cnf, std::move(tls_context)) {} - /// Copy constructor. - mqtt_client(const mqtt_client& other) = delete; + /// Move-construct an mqtt_client from another. + /** Moved-from client can only be destructed. */ + mqtt_client(mqtt_client&& other) noexcept = default; + + /// Move-assign an mqtt_client from another. + /** Cancels this client first. + * Moved-from client can only be destructed. + */ + mqtt_client& operator=(mqtt_client&& other) noexcept { + cancel(); + _svc_ptr = std::move(other._svc_ptr); + return *this; + } /** * \brief Destructor. @@ -94,7 +105,7 @@ public: * \details Automatically calls \ref mqtt_client::cancel. */ ~mqtt_client() { - cancel(); + if (_svc_ptr) cancel(); } /**