diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2019-02-06 16:02:19 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2019-02-08 11:40:17 +0000 |
commit | fb2fb95f8b6d23715728b702ccdb9ec5587872a9 (patch) | |
tree | 9de18dd57f54c1a6275950075b0a47c3552f8516 | |
parent | 56a5103781a17a2ae0369bbd468d7103d8df6d6b (diff) |
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 <lars.knoll@qt.io>
-rw-r--r-- | src/qml/qml/ftw/qqmlrefcount_p.h | 21 |
1 files 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<class T> @@ -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<class T> QQmlRefPointer<T>::QQmlRefPointer() : o(nullptr) |