From 57f98a095e55faec178f454a1bc3fd5193c854e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20S=C3=B8rvig?= Date: Fri, 7 Mar 2025 10:27:04 +0100 Subject: [PATCH] wasm: implement move for QWasmEventHandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement move by copying the emscripten::val member variables, and clearing the moved-from ones. This ensures that the moved-from val is in a known state ("undefined"). Also move the non-default constructor declaration up to the top of the class. Change-Id: If9574cc91a53d4029d49b29b89819d98319acaa3 Reviewed-by: Even Oscar Andersen Reviewed-by: Morten Johan Sørvig --- .../wasm/qwasmsuspendresumecontrol.cpp | 25 +++++++++++++++++++ .../wasm/qwasmsuspendresumecontrol_p.h | 7 +++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp b/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp index bf1d30bf23c..7b71e73816b 100644 --- a/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp +++ b/src/corelib/platform/wasm/qwasmsuspendresumecontrol.cpp @@ -180,12 +180,37 @@ QWasmEventHandler::QWasmEventHandler(emscripten::val element, const std::string QWasmEventHandler::~QWasmEventHandler() { + // Do nothing if this instance is default-constructed, or was moved from. + if (m_element.isUndefined()) + return; + QWasmSuspendResumeControl *suspendResume = QWasmSuspendResumeControl::get(); Q_ASSERT(suspendResume); m_element.call("removeEventListener", m_name, suspendResume->jsEventHandlerAt(m_eventHandlerIndex)); suspendResume->removeEventHandler(m_eventHandlerIndex); } +QWasmEventHandler::QWasmEventHandler(QWasmEventHandler&& other) noexcept +:m_element(std::move(other.m_element)) +,m_name(std::move(other.m_name)) +,m_eventHandlerIndex(other.m_eventHandlerIndex) +{ + other.m_element = emscripten::val(); + other.m_name = emscripten::val(); + other.m_eventHandlerIndex = 0; +} + +QWasmEventHandler& QWasmEventHandler::operator=(QWasmEventHandler&& other) noexcept +{ + m_element = std::move(other.m_element); + other.m_element = emscripten::val(); + m_name = std::move(other.m_name); + other.m_name = emscripten::val(); + m_eventHandlerIndex = other.m_eventHandlerIndex; + other.m_eventHandlerIndex = 0; + return *this; +} + // // The QWasmTimer class creates a native single-shot timer. The event handler is provided in the // constructor and can be reused: each call setTimeout() sets a new timeout, though with the diff --git a/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h b/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h index c500c40791a..8b660a91719 100644 --- a/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h +++ b/src/corelib/platform/wasm/qwasmsuspendresumecontrol_p.h @@ -51,12 +51,13 @@ class Q_CORE_EXPORT QWasmEventHandler { public: QWasmEventHandler() = default; + QWasmEventHandler(emscripten::val element, const std::string &name, + std::function fn); ~QWasmEventHandler(); QWasmEventHandler(QWasmEventHandler const&) = delete; QWasmEventHandler& operator=(QWasmEventHandler const&) = delete; - QWasmEventHandler(emscripten::val element, const std::string &name, - std::function fn); - + QWasmEventHandler(QWasmEventHandler&& other) noexcept; + QWasmEventHandler& operator=(QWasmEventHandler&& other) noexcept; private: emscripten::val m_element; emscripten::val m_name;