Give some TLC to QAppleRefCounted

- add conditional noexcept to move special member functions
- use qExchange() in the move ctor implementation (turns a copy into a move)
- separate the default ctor from the ctor that acquires a resource, then
- overload the latter for rvalue payloads

Change-Id: I6816143a94fe6a74cf0d02569b83a752a8da3089
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Marc Mutz 2019-08-03 09:06:50 +03:00
parent 9c891bead8
commit 3ed21726ec

View File

@ -86,8 +86,14 @@ template <typename T, typename U, U (*RetainFunction)(U), void (*ReleaseFunction
class QAppleRefCounted
{
public:
QAppleRefCounted(const T &t = T()) : value(t) {}
QAppleRefCounted(QAppleRefCounted &&other) : value(other.value) { other.value = T(); }
QAppleRefCounted() : value() {}
QAppleRefCounted(const T &t) : value(t) {}
QAppleRefCounted(T &&t) noexcept(std::is_nothrow_move_constructible<T>::value)
: value(std::move(t)) {}
QAppleRefCounted(QAppleRefCounted &&other)
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
: value(qExchange(other.value, T())) {}
QAppleRefCounted(const QAppleRefCounted &other) : value(other.value) { if (value) RetainFunction(value); }
~QAppleRefCounted() { if (value) ReleaseFunction(value); }
operator T() const { return value; }
@ -96,6 +102,8 @@ public:
QAppleRefCounted &operator=(const QAppleRefCounted &other)
{ QAppleRefCounted copy(other); swap(copy); return *this; }
QAppleRefCounted &operator=(QAppleRefCounted &&other)
noexcept(std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
{ QAppleRefCounted moved(std::move(other)); swap(moved); return *this; }
T *operator&() { return &value; }
protected: