| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | #ifndef REFCOUNTED_H |
| 4 | #define REFCOUNTED_H |
| 5 | |
| 6 | #include "PassRefPtr.h" |
| 7 | |
| 8 | template <typename Base> |
| 9 | class RefCounted { |
| 10 | public: |
| 11 | RefCounted() : m_refCount(1) {} |
| 12 | ~RefCounted() |
| 13 | { |
| 14 | deref(); |
| 15 | } |
| 16 | |
| 17 | void ref() |
| 18 | { |
| 19 | ++m_refCount; |
| 20 | } |
| 21 | |
| 22 | void deref() |
| 23 | { |
| 24 | if (!--m_refCount) |
| 25 | delete static_cast<Base*>(this); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | int m_refCount; |
| 30 | }; |
| 31 | |
| 32 | #endif // REFCOUNTED_H |
| 33 | |