Qt 6.x
The Qt SDK
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
4#ifndef QQMLREFCOUNT_P_H
5#define QQMLREFCOUNT_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qglobal.h>
19#include <QtCore/qatomic.h>
20#include <private/qv4global_p.h>
21
23
24template <typename T>
25class QQmlRefCounted;
26
28{
29 Q_DISABLE_COPY_MOVE(QQmlRefCount)
30public:
31 inline QQmlRefCount();
32 inline void addref() const;
33 inline int count() const;
34
35private:
36 inline ~QQmlRefCount();
37 template <typename T> friend class QQmlRefCounted;
38
39private:
40 mutable QAtomicInt refCount;
41};
42
43template <typename T>
45{
46public:
47 inline void release() const;
48protected:
50};
51
52template<class T>
54{
55public:
56 enum Mode {
58 Adopt
59 };
64 inline ~QQmlRefPointer();
65
66 void swap(QQmlRefPointer &other) noexcept { qt_ptr_swap(o, other.o); }
67
70
71 inline bool isNull() const { return !o; }
72
73 inline T* operator->() const { return o; }
74 inline T& operator*() const { return *o; }
75 explicit inline operator bool() const { return o != nullptr; }
76 inline T* data() const { return o; }
77
79
80 inline T* take() { T *res = o; o = nullptr; return res; }
81
82 friend bool operator==(const QQmlRefPointer &a, const QQmlRefPointer &b) { return a.o == b.o; }
83 friend bool operator!=(const QQmlRefPointer &a, const QQmlRefPointer &b) { return !(a == b); }
84
85 void reset(T *t = nullptr)
86 {
87 if (t == o)
88 return;
89 if (o)
90 o->release();
91 if (t)
92 t->addref();
93 o = t;
94 }
95
96private:
97 T *o;
98};
99
100namespace QQml {
107template <typename T, typename ...Args>
109{
110 static_assert(std::is_base_of_v<QQmlRefCount, T>);
111 return QQmlRefPointer<T>(new T(std::forward<Args>(args)...), QQmlRefPointer<T>::Adopt);
112}
113}
114
115template <typename T>
117
119: refCount(1)
120{
121}
122
123QQmlRefCount::~QQmlRefCount()
124{
125 Q_ASSERT(refCount.loadRelaxed() == 0);
126}
127
129{
130 Q_ASSERT(refCount.loadRelaxed() > 0);
131 refCount.ref();
132}
133
134template <typename T>
136{
137 static_assert(std::is_base_of_v<QQmlRefCounted, T>,
138 "QQmlRefCounted<T> must be a base of T (CRTP)");
139 Q_ASSERT(refCount.loadRelaxed() > 0);
140 if (!refCount.deref())
141 delete static_cast<const T *>(this);
142}
143
144template <typename T>
146{
147 static_assert(std::is_final_v<T> || std::has_virtual_destructor_v<T>,
148 "T must either be marked final or have a virtual dtor, "
149 "lest release() runs into UB.");
150}
151
153{
154 return refCount.loadRelaxed();
155}
156
157template<class T>
159: o(nullptr)
160{
161}
162
163template<class T>
165: o(o)
166{
167 if (m == AddRef && o)
168 o->addref();
169}
170
171template<class T>
173: o(other.o)
174{
175 if (o) o->addref();
176}
177
178template <class T>
180 : o(other.take())
181{
182}
183
184template<class T>
186{
187 if (o) o->release();
188}
189
190template<class T>
192{
193 if (o == other.o)
194 return *this;
195 if (other.o)
196 other.o->addref();
197 if (o)
198 o->release();
199 o = other.o;
200 return *this;
201}
202
203template <class T>
205{
206 QQmlRefPointer<T> m(std::move(other));
207 swap(m);
208 return *this;
209}
210
215template<class T>
217{
218 if (o) o->release();
219 o = other;
220 return *this;
221}
222
224
225#endif // QQMLREFCOUNT_P_H
\inmodule QtCore
Definition qatomic.h:112
bool ref() noexcept
T loadRelaxed() const noexcept
void addref() const
int count() const
void release() const
T * data() const
friend bool operator==(const QQmlRefPointer &a, const QQmlRefPointer &b)
T & operator*() const
QQmlRefPointer< T > & operator=(const QQmlRefPointer< T > &o)
QQmlRefPointer< T > & operator=(QQmlRefPointer< T > &&o) noexcept
void reset(T *t=nullptr)
QQmlRefPointer< T > & adopt(T *)
Takes ownership of other.
T * operator->() const
friend bool operator!=(const QQmlRefPointer &a, const QQmlRefPointer &b)
bool isNull() const
Q_NODISCARD_CTOR QQmlRefPointer() noexcept
QQmlRefPointer< T > makeRefPointer(Args &&... args)
Combined button and popup list for selecting options.
#define Q_NODISCARD_CTOR
GLboolean GLboolean GLboolean b
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint res
GLdouble GLdouble t
Definition qopenglext.h:243
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
constexpr void qt_ptr_swap(T *&lhs, T *&rhs) noexcept
Definition qswap.h:43
@ Q_RELOCATABLE_TYPE
Definition qtypeinfo.h:145
#define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS)
Definition qtypeinfo.h:150
QSharedPointer< T > other(t)
[5]
this swap(other)
QJSValueList args