From fb2fb95f8b6d23715728b702ccdb9ec5587872a9 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 6 Feb 2019 16:02:19 +0100 Subject: Make QQmlRefCount usable for immutable types refcounting should still be possible if the object is const. You can safely take and release references to const objects, after all. operator delete also works for const pointers. Therefore, make the refCount member mutable and declare addref() and release() as const. Additionally, drop the unused destroy() method and make the dtor protected for marginally more protection from accidental manual deletion. Plus, refcounted types should really not be copied or moved. Change-Id: I28fdc96c60b6ed5a056782568717c16b76234883 Reviewed-by: Lars Knoll --- src/qml/qml/ftw/qqmlrefcount_p.h | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/qml/qml/ftw/qqmlrefcount_p.h b/src/qml/qml/ftw/qqmlrefcount_p.h index d32a08e0f5..140f129b21 100644 --- a/src/qml/qml/ftw/qqmlrefcount_p.h +++ b/src/qml/qml/ftw/qqmlrefcount_p.h @@ -60,18 +60,18 @@ QT_BEGIN_NAMESPACE class Q_QML_PRIVATE_EXPORT QQmlRefCount { + Q_DISABLE_COPY_MOVE(QQmlRefCount) public: inline QQmlRefCount(); - inline virtual ~QQmlRefCount(); - inline void addref(); - inline void release(); + inline void addref() const; + inline void release() const; inline int count() const; protected: - inline virtual void destroy(); + inline virtual ~QQmlRefCount(); private: - QAtomicInt refCount; + mutable QAtomicInt refCount; }; template @@ -116,17 +116,17 @@ QQmlRefCount::~QQmlRefCount() Q_ASSERT(refCount.load() == 0); } -void QQmlRefCount::addref() +void QQmlRefCount::addref() const { Q_ASSERT(refCount.load() > 0); refCount.ref(); } -void QQmlRefCount::release() +void QQmlRefCount::release() const { Q_ASSERT(refCount.load() > 0); if (!refCount.deref()) - destroy(); + delete this; } int QQmlRefCount::count() const @@ -134,11 +134,6 @@ int QQmlRefCount::count() const return refCount.load(); } -void QQmlRefCount::destroy() -{ - delete this; -} - template QQmlRefPointer::QQmlRefPointer() : o(nullptr) -- cgit v1.2.3