blob: 14e60ca2358a6fab1397ddd6773a5566ff6211fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef REFPTR_H
#define REFPTR_H
#include "PassRefPtr.h"
template <typename T>
class RefPtr {
public:
RefPtr() : m_ptr(0) {}
RefPtr(const RefPtr<T> &other)
: m_ptr(other.m_ptr)
{
if (m_ptr)
m_ptr->ref();
}
RefPtr<T>& operator=(const RefPtr<T>& other)
{
if (other.m_ptr)
other.m_ptr->ref();
if (m_ptr)
m_ptr->deref();
m_ptr = other.m_ptr;
return *this;
}
RefPtr(const PassRefPtr<T>& other)
: m_ptr(other.leakRef())
{
}
~RefPtr()
{
if (m_ptr)
m_ptr->deref();
}
T* operator->() const { return m_ptr; }
T* get() const { return m_ptr; }
bool operator!() const { return !m_ptr; }
PassRefPtr<T> release()
{
T* ptr = m_ptr;
m_ptr = 0;
return adoptRef(ptr);
}
private:
T* m_ptr;
};
#endif // REFPTR_H
|