Add proper copy and move semantics to proxy handle

This commit is contained in:
Robbert van der Helm
2021-07-10 00:49:11 +02:00
parent 2ba2cf1ab7
commit dbb1b09256
2 changed files with 36 additions and 5 deletions
+28 -3
View File
@@ -155,14 +155,39 @@ WineXdndProxy::WineXdndProxy()
*/ */
static std::atomic_size_t instance_reference_count = 0; static std::atomic_size_t instance_reference_count = 0;
WineXdndProxy::Handle::Handle(WineXdndProxy& proxy) : proxy(proxy) {} WineXdndProxy::Handle::Handle(WineXdndProxy* proxy) : proxy(proxy) {}
WineXdndProxy::Handle::~Handle() noexcept { WineXdndProxy::Handle::~Handle() noexcept {
if (instance_reference_count.fetch_sub(1) == 1) { if (instance_reference_count.fetch_sub(1) == 1) {
delete &proxy; delete proxy;
} }
} }
WineXdndProxy::Handle::Handle(const Handle& o) noexcept : proxy(o.proxy) {
instance_reference_count += 1;
}
WineXdndProxy::Handle& WineXdndProxy::Handle::operator=(
const Handle& o) noexcept {
if (&o != this) {
instance_reference_count += 1;
proxy = o.proxy;
}
return *this;
}
WineXdndProxy::Handle::Handle(Handle&& o) noexcept : proxy(std::move(o.proxy)) {
instance_reference_count += 1;
}
WineXdndProxy::Handle& WineXdndProxy::Handle::operator=(Handle&& o) noexcept {
if (&o != this) {
instance_reference_count += 1;
proxy = std::move(o.proxy);
}
return *this;
}
WineXdndProxy::Handle WineXdndProxy::init_proxy() { WineXdndProxy::Handle WineXdndProxy::init_proxy() {
// We're doing a bit of a hybrid between a COM-style reference counted smart // We're doing a bit of a hybrid between a COM-style reference counted smart
// pointer and a singleton here because we need to ensure that there's only // pointer and a singleton here because we need to ensure that there's only
@@ -176,5 +201,5 @@ WineXdndProxy::Handle WineXdndProxy::init_proxy() {
instance = new WineXdndProxy{}; instance = new WineXdndProxy{};
} }
return Handle(*instance); return Handle(instance);
} }
+8 -2
View File
@@ -50,7 +50,7 @@ class WineXdndProxy {
* Before calling this, the reference count should be increased by one * Before calling this, the reference count should be increased by one
* in `WineXdndProxy::init_proxy()`. * in `WineXdndProxy::init_proxy()`.
*/ */
Handle(WineXdndProxy& proxy); Handle(WineXdndProxy* proxy);
public: public:
/** /**
@@ -59,8 +59,14 @@ class WineXdndProxy {
*/ */
~Handle() noexcept; ~Handle() noexcept;
Handle(const Handle&) noexcept;
Handle& operator=(const Handle&) noexcept;
Handle(Handle&&) noexcept;
Handle& operator=(Handle&&) noexcept;
private: private:
WineXdndProxy& proxy; WineXdndProxy* proxy;
friend WineXdndProxy; friend WineXdndProxy;
}; };