Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qqmlrefcount_p.h
Go to the documentation of this file.
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
// Qt-Security score:significant
4
5
#
ifndef
QQMLREFCOUNT_P_H
6
#
define
QQMLREFCOUNT_P_H
7
8
//
9
// W A R N I N G
10
// -------------
11
//
12
// This file is not part of the Qt API. It exists purely as an
13
// implementation detail. This header file may change from version to
14
// version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#
include
<
QtCore
/
qglobal
.
h
>
20
#
include
<
QtCore
/
qatomic
.
h
>
21
#
include
<
private
/
qv4global_p
.
h
>
22
23
QT_BEGIN_NAMESPACE
24
25
template
<
typename
T>
26
class
QQmlRefCounted;
27
28
class
QQmlRefCount
29
{
30
Q_DISABLE_COPY_MOVE
(
QQmlRefCount
)
31
public
:
32
inline
QQmlRefCount
();
33
inline
void
addref
()
const
;
34
inline
int
count
()
const
;
35
36
private
:
37
inline
~QQmlRefCount();
38
template
<
typename
T>
friend
class
QQmlRefCounted
;
39
40
private
:
41
mutable
QAtomicInt refCount;
42
};
43
44
template
<
typename
T>
45
class
QQmlRefCounted
:
public
QQmlRefCount
46
{
47
public
:
48
inline
void
release
()
const
;
49
protected
:
50
inline
~
QQmlRefCounted
();
51
};
52
53
template
<
class
T>
54
class
QQmlRefPointer
55
{
56
public
:
57
enum
Mode
{
58
AddRef
,
59
Adopt
60
};
61
Q_NODISCARD_CTOR
inline
QQmlRefPointer
()
noexcept
;
62
Q_NODISCARD_CTOR
inline
QQmlRefPointer
(
T
*,
Mode
m
=
AddRef
);
63
Q_NODISCARD_CTOR
inline
QQmlRefPointer
(
const
QQmlRefPointer
&);
64
Q_NODISCARD_CTOR
inline
QQmlRefPointer
(
QQmlRefPointer
&&)
noexcept
;
65
inline
~
QQmlRefPointer
();
66
67
void
swap
(
QQmlRefPointer
&other)
noexcept
{ qt_ptr_swap(o, other.o); }
68
69
inline
QQmlRefPointer
<T> &
operator
=(
const
QQmlRefPointer
<T> &o);
70
inline
QQmlRefPointer
<T> &
operator
=(
QQmlRefPointer
<T> &&o)
noexcept
;
71
72
inline
bool
isNull
()
const
{
return
!o; }
73
74
inline
T*
operator
->()
const
{
return
o; }
75
inline
T&
operator
*()
const
{
return
*o; }
76
explicit
inline
operator
bool
()
const
{
return
o !=
nullptr
; }
77
inline
T*
data
()
const
{
return
o; }
78
79
inline
QQmlRefPointer
<T> &
adopt
(T *);
80
81
inline
T*
take
() { T *res = o; o =
nullptr
;
return
res; }
82
83
friend
bool
operator
==(
const
QQmlRefPointer
&a,
const
QQmlRefPointer
&b)
noexcept
84
{
85
return
a.o == b.o;
86
}
87
88
friend
bool
operator
!=(
const
QQmlRefPointer
&a,
const
QQmlRefPointer
&b)
noexcept
89
{
90
return
!(a == b);
91
}
92
93
friend
size_t
qHash
(
const
QQmlRefPointer
&v, size_t seed = 0)
noexcept
94
{
95
return
qHash(v.o, seed);
96
}
97
98
void
reset
(T *t =
nullptr
)
99
{
100
if
(t == o)
101
return
;
102
if
(o)
103
o->release();
104
if
(t)
105
t->addref();
106
o = t;
107
}
108
109
private
:
110
T *o;
111
};
112
113
namespace
QQml
{
114
/*!
115
\internal
116
Creates a QQmlRefPointer which takes ownership of a newly constructed T.
117
T must derive from QQmlRefCounted<T> (as we rely on an initial refcount of _1_).
118
T will be constructed by forwarding \a args to its constructor.
119
*/
120
template
<
typename
T,
typename
...Args>
121
QQmlRefPointer
<T>
makeRefPointer
(Args&&... args)
122
{
123
static_assert
(std::is_base_of_v<QQmlRefCount, T>);
124
return
QQmlRefPointer<T>(
new
T(std::forward<Args>(args)...), QQmlRefPointer<T>::Adopt);
125
}
126
}
127
128
template
<
typename
T>
129
Q_DECLARE_TYPEINFO_BODY
(
QQmlRefPointer
<T>,
Q_RELOCATABLE_TYPE
);
130
131
QQmlRefCount
::QQmlRefCount()
132
: refCount(1)
133
{
134
}
135
136
QQmlRefCount
::~QQmlRefCount()
137
{
138
Q_ASSERT(refCount.loadRelaxed() == 0);
139
}
140
141
void
QQmlRefCount
::
addref
()
const
142
{
143
Q_ASSERT(refCount.loadRelaxed() > 0);
144
refCount.ref();
145
}
146
147
template
<
typename
T>
148
void
QQmlRefCounted
<T>::
release
()
const
149
{
150
static_assert
(std::is_base_of_v<QQmlRefCounted, T>,
151
"QQmlRefCounted<T> must be a base of T (CRTP)"
);
152
Q_ASSERT(refCount.loadRelaxed() > 0);
153
if
(!refCount.deref())
154
delete
static_cast
<
const
T *>(
this
);
155
}
156
157
template
<
typename
T>
158
QQmlRefCounted
<T>::~
QQmlRefCounted
()
159
{
160
static_assert
(std::is_final_v<T> || std::has_virtual_destructor_v<T>,
161
"T must either be marked final or have a virtual dtor, "
162
"lest release() runs into UB."
);
163
}
164
165
int
QQmlRefCount
::
count
()
const
166
{
167
return
refCount.loadRelaxed();
168
}
169
170
template
<
class
T>
171
QQmlRefPointer
<T>::QQmlRefPointer()
noexcept
172
: o(
nullptr
)
173
{
174
}
175
176
template
<
class
T>
177
QQmlRefPointer
<T>::QQmlRefPointer(T *o,
Mode
m)
178
: o(o)
179
{
180
if
(m ==
AddRef
&& o)
181
o->addref();
182
}
183
184
template
<
class
T>
185
QQmlRefPointer
<T>::QQmlRefPointer(
const
QQmlRefPointer
<T> &other)
186
: o(other.o)
187
{
188
if
(o) o->addref();
189
}
190
191
template
<
class
T>
192
QQmlRefPointer
<T>::QQmlRefPointer(
QQmlRefPointer
<T> &&other)
noexcept
193
: o(other
.
take
(
)
)
194
{
195
}
196
197
template
<
class
T>
198
QQmlRefPointer
<T>::~
QQmlRefPointer
()
199
{
200
if
(o) o->release();
201
}
202
203
template
<
class
T>
204
QQmlRefPointer
<T> &
QQmlRefPointer
<T>::
operator
=(
const
QQmlRefPointer
<T> &other)
205
{
206
if
(o == other.o)
207
return
*
this
;
208
if
(other.o)
209
other.o->addref();
210
if
(o)
211
o->release();
212
o = other.o;
213
return
*
this
;
214
}
215
216
template
<
class
T>
217
QQmlRefPointer
<T> &
QQmlRefPointer
<T>::
operator
=(
QQmlRefPointer
<T> &&other)
noexcept
218
{
219
QQmlRefPointer
<T> m(std::move(other));
220
swap
(
m
)
;
221
return
*
this
;
222
}
223
224
/*!
225
Takes ownership of \a other. take() does *not* add a reference, as it assumes ownership
226
of the callers reference of other.
227
*/
228
template
<
class
T>
229
QQmlRefPointer
<T> &
QQmlRefPointer
<T>::
adopt
(T *other)
230
{
231
if
(o) o->release();
232
o = other;
233
return
*
this
;
234
}
235
236
QT_END_NAMESPACE
237
238
#
endif
// QQMLREFCOUNT_P_H
QQmlRefCount
Definition
qqmlrefcount_p.h:29
QQmlRefCount::QQmlRefCounted
friend class QQmlRefCounted
Definition
qqmlrefcount_p.h:38
QQmlRefCount::addref
void addref() const
Definition
qqmlrefcount_p.h:141
QQmlRefCount::count
int count() const
Definition
qqmlrefcount_p.h:165
QQmlRefCounted::~QQmlRefCounted
~QQmlRefCounted()
Definition
qqmlrefcount_p.h:158
QQmlRefCounted::release
void release() const
Definition
qqmlrefcount_p.h:148
QQmlRefPointer
Definition
qqmlrefcount_p.h:55
QQmlRefPointer::Mode
Mode
Definition
qqmlrefcount_p.h:57
QQmlRefPointer::AddRef
@ AddRef
Definition
qqmlrefcount_p.h:58
QQmlRefPointer::Adopt
@ Adopt
Definition
qqmlrefcount_p.h:59
QQmlRefPointer::data
T * data() const
Definition
qqmlrefcount_p.h:77
QQmlRefPointer::operator*
T & operator*() const
Definition
qqmlrefcount_p.h:75
QQmlRefPointer::swap
void swap(QQmlRefPointer &other) noexcept
Definition
qqmlrefcount_p.h:67
QQmlRefPointer::operator=
QQmlRefPointer< T > & operator=(const QQmlRefPointer< T > &o)
Definition
qqmlrefcount_p.h:204
QQmlRefPointer::operator=
QQmlRefPointer< T > & operator=(QQmlRefPointer< T > &&o) noexcept
Definition
qqmlrefcount_p.h:217
QQmlRefPointer::reset
void reset(T *t=nullptr)
Definition
qqmlrefcount_p.h:98
QQmlRefPointer::qHash
friend size_t qHash(const QQmlRefPointer &v, size_t seed=0) noexcept
Definition
qqmlrefcount_p.h:93
QQmlRefPointer::adopt
QQmlRefPointer< T > & adopt(T *)
Takes ownership of other.
Definition
qqmlrefcount_p.h:229
QQmlRefPointer::operator->
T * operator->() const
Definition
qqmlrefcount_p.h:74
QQmlRefPointer::~QQmlRefPointer
~QQmlRefPointer()
Definition
qqmlrefcount_p.h:198
QQmlRefPointer::operator!=
friend bool operator!=(const QQmlRefPointer &a, const QQmlRefPointer &b) noexcept
Definition
qqmlrefcount_p.h:88
QQmlRefPointer::operator==
friend bool operator==(const QQmlRefPointer &a, const QQmlRefPointer &b) noexcept
Definition
qqmlrefcount_p.h:83
QQmlRefPointer::take
T * take()
Definition
qqmlrefcount_p.h:81
QQmlRefPointer::isNull
bool isNull() const
Definition
qqmlrefcount_p.h:72
QQmlRefPointer::operator bool
operator bool() const
Definition
qqmlrefcount_p.h:76
QQml
Definition
qqmlrefcount_p.h:113
QQml::makeRefPointer
QQmlRefPointer< T > makeRefPointer(Args &&... args)
Definition
qqmlrefcount_p.h:121
Q_DECLARE_TYPEINFO_BODY
Q_DECLARE_TYPEINFO_BODY(QQmlRefPointer< T >, Q_RELOCATABLE_TYPE)
qtdeclarative
src
qml
qml
ftw
qqmlrefcount_p.h
Generated on
for Qt by
1.18.0