diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2019-10-15 10:39:33 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2019-10-22 09:19:58 +0000 |
commit | 9188076ae9c7beff8e693d953c1e26f413680951 (patch) | |
tree | 03881a460280fae4f55216569b60961949a71054 /src | |
parent | ae5daa8ebfbfc3e72e1ad80cc5525b0dc0eac64d (diff) |
Core/IOBluetooth - minor refactoring in RAII classes
To prepare a switch from QScoped/QShared specializations in ObjC-RAII classes.
Since StrongReference will become a parent for ObjCStrongReference, add
'take'-like functionality we have for free from QSharedPointer (used when
we relegate the ownership and pass it to IOBluetooth)
Change-Id: Iab7758e6c51c339bf7afc49007d921380d38ba51
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/bluetooth/darwin/btraii.mm | 21 | ||||
-rw-r--r-- | src/bluetooth/darwin/btraii_p.h | 2 |
2 files changed, 16 insertions, 7 deletions
diff --git a/src/bluetooth/darwin/btraii.mm b/src/bluetooth/darwin/btraii.mm index a1bf2a8d..486c3c14 100644 --- a/src/bluetooth/darwin/btraii.mm +++ b/src/bluetooth/darwin/btraii.mm @@ -52,13 +52,14 @@ namespace DarwinBluetooth { StrongReference::StrongReference(void *object, RetainPolicy policy) : objCInstance(object) { - if (policy == RetainPolicy::doInitialRetain) + if (objCInstance && policy == RetainPolicy::doInitialRetain) objCInstance = [getAs<NSObject>() retain]; } StrongReference::StrongReference(const StrongReference &other) { - objCInstance = [other.getAs<NSObject>() retain]; + if ((objCInstance = other.getAs<NSObject>())) + objCInstance = [other.getAs<NSObject>() retain]; } StrongReference::StrongReference(StrongReference &&other) @@ -84,9 +85,18 @@ StrongReference &StrongReference::operator = (const StrongReference &other) noex StrongReference &StrongReference::operator = (StrongReference &&other) noexcept { swap(other); + return *this; } +void *StrongReference::release() +{ + void *released = objCInstance; + objCInstance = nullptr; + + return released; +} + void StrongReference::reset() { [getAs<NSObject>() release]; @@ -98,11 +108,8 @@ void StrongReference::reset(void *obj, RetainPolicy policy) [getAs<NSObject>() release]; objCInstance = obj; - if (policy == RetainPolicy::doInitialRetain) { - auto newInstance = static_cast<NSObject *>(obj); - Q_ASSERT(newInstance); - objCInstance = [newInstance retain]; - } + if (objCInstance && policy == RetainPolicy::doInitialRetain) + objCInstance = [getAs<NSObject>() retain]; } } // namespace DarwinBluetooth diff --git a/src/bluetooth/darwin/btraii_p.h b/src/bluetooth/darwin/btraii_p.h index 6053d63b..c7a159cb 100644 --- a/src/bluetooth/darwin/btraii_p.h +++ b/src/bluetooth/darwin/btraii_p.h @@ -98,6 +98,8 @@ public: std::swap(objCInstance, other.objCInstance); } + void *release(); + void reset(); void reset(void *newInstance, RetainPolicy policy); |