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 REFPTR_H |
4 | #define REFPTR_H |
5 | |
6 | #include "PassRefPtr.h" |
7 | |
8 | template <typename T> |
9 | class RefPtr { |
10 | public: |
11 | RefPtr() : m_ptr(0) {} |
12 | RefPtr(const RefPtr<T> &other) |
13 | : m_ptr(other.m_ptr) |
14 | { |
15 | if (m_ptr) |
16 | m_ptr->ref(); |
17 | } |
18 | |
19 | RefPtr<T>& operator=(const RefPtr<T>& other) |
20 | { |
21 | if (other.m_ptr) |
22 | other.m_ptr->ref(); |
23 | if (m_ptr) |
24 | m_ptr->deref(); |
25 | m_ptr = other.m_ptr; |
26 | return *this; |
27 | } |
28 | |
29 | RefPtr(const PassRefPtr<T>& other) |
30 | : m_ptr(other.leakRef()) |
31 | { |
32 | } |
33 | |
34 | ~RefPtr() |
35 | { |
36 | if (m_ptr) |
37 | m_ptr->deref(); |
38 | } |
39 | |
40 | T* operator->() const { return m_ptr; } |
41 | T* get() const { return m_ptr; } |
42 | bool operator!() const { return !m_ptr; } |
43 | |
44 | PassRefPtr<T> release() |
45 | { |
46 | T* ptr = m_ptr; |
47 | m_ptr = 0; |
48 | return adoptRef(ptr); |
49 | } |
50 | |
51 | private: |
52 | T* m_ptr; |
53 | }; |
54 | |
55 | #endif // REFPTR_H |
56 |