mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-05 09:15:34 +08:00
T102 Implement move constructor and move assignment for CWorkerPointer
so the connection to the worker's aboutToStart signal remains valid.
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#define BLACKMISC_WORKER_H
|
||||
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include "blackmisc/connectionguard.h"
|
||||
#include "blackmisc/logcategorylist.h"
|
||||
#include "blackmisc/invoke.h"
|
||||
#include "blackmisc/stacktrace.h"
|
||||
@@ -329,7 +330,7 @@ namespace BlackMisc
|
||||
{
|
||||
if (!ptr || static_cast<const CContinuousWorker *>(ptr)->hasStarted()) { return; }
|
||||
m_strong.reset(ptr);
|
||||
QObject::connect(ptr, &CWorkerBase::aboutToStart, [this] { m_strong.release(); });
|
||||
connect();
|
||||
}
|
||||
|
||||
//! Construct a null pointer.
|
||||
@@ -338,6 +339,27 @@ namespace BlackMisc
|
||||
CWorkerPointer(std::nullptr_t) {}
|
||||
//! @}
|
||||
|
||||
//! Move constructor.
|
||||
CWorkerPointer(CWorkerPointer &&other) : m_strong(std::move(other.m_strong)), m_weak(other.m_weak), m_guard()
|
||||
{
|
||||
connect();
|
||||
}
|
||||
|
||||
//! Move assignment operator.
|
||||
CWorkerPointer &operator =(CWorkerPointer &&other)
|
||||
{
|
||||
m_strong = std::move(other.m_strong);
|
||||
m_weak = other.m_weak;
|
||||
connect();
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Not copyable.
|
||||
//! @{
|
||||
CWorkerPointer(const CWorkerPointer &) = delete;
|
||||
CWorkerPointer &operator =(const CWorkerPointer &) = delete;
|
||||
//! @}
|
||||
|
||||
//! Factory method.
|
||||
//! Arguments are forwarded to the constructor of T. Strictly more exception-safe than calling the constructor with new.
|
||||
template <typename... Ts>
|
||||
@@ -360,8 +382,15 @@ namespace BlackMisc
|
||||
bool isOwner() const { return m_strong; }
|
||||
|
||||
private:
|
||||
void connect()
|
||||
{
|
||||
if (!m_strong) { return; }
|
||||
m_guard = QObject::connect(m_strong.get(), &CWorkerBase::aboutToStart, [this] { m_strong.release(); });
|
||||
}
|
||||
|
||||
std::unique_ptr<T> m_strong;
|
||||
QPointer<T> m_weak;
|
||||
CConnectionGuard m_guard;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user