Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsharedpointer.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 QSHAREDPOINTER_H
5#define QSHAREDPOINTER_H
6
7#include <QtCore/qglobal.h>
8#include <QtCore/qatomic.h>
9#include <QtCore/qshareddata.h>
10
11#ifndef Q_QDOC
12# include <QtCore/qsharedpointer_impl.h>
13#else
14
15#include <memory> // for std::shared_ptr
16
18
19
20// These classes are here to fool qdoc into generating a better documentation
21
22template <class T>
24{
25public:
26 // basic accessor functions
27 T *data() const;
28 T *get() const;
29 bool isNull() const;
30 operator bool() const;
31 bool operator!() const;
32 T &operator*() const;
33 T *operator->() const;
34
35 // constructors
37 template <typename X> explicit QSharedPointer(X *ptr);
38 template <typename X, typename Deleter> QSharedPointer(X *ptr, Deleter d);
39 QSharedPointer(std::nullptr_t);
40 template <typename Deleter> QSharedPointer(std::nullptr_t, Deleter d);
41 QSharedPointer(const QSharedPointer<T> &other);
42 QSharedPointer(const QWeakPointer<T> &other);
43 QSharedPointer(QSharedPointer<T> &&other) noexcept;
44
45 ~QSharedPointer() { }
46
47 QSharedPointer<T> &operator=(const QSharedPointer<T> &other);
48 QSharedPointer<T> &operator=(QSharedPointer<T> &&other) noexcept;
49 QSharedPointer<T> &operator=(const QWeakPointer<T> &other);
50
51 template <class X>
52 QSharedPointer(QSharedPointer<X> && other) noexcept;
53 template <class X>
54 QSharedPointer &operator=(QSharedPointer<X> && other) noexcept;
55
56 void swap(QSharedPointer<T> &other) noexcept;
57
58 QWeakPointer<T> toWeakRef() const;
59
60 void clear();
61
62 void reset();
63 void reset(T *t);
64 template <typename Deleter>
65 void reset(T *t, Deleter deleter);
66
67 // casts:
68 template <class X> QSharedPointer<X> staticCast() const;
69 template <class X> QSharedPointer<X> dynamicCast() const;
70 template <class X> QSharedPointer<X> constCast() const;
71 template <class X> QSharedPointer<X> objectCast() const;
72
73 template <typename... Args>
74 static inline QSharedPointer<T> create(Args &&... args);
75
76 // owner-based comparisons
77 template <typename X>
78 bool owner_before(const QSharedPointer<X> &other) const noexcept;
79 template <typename X>
80 bool owner_before(const QWeakPointer<X> &other) const noexcept;
81
82 template <typename X>
83 bool owner_equal(const QSharedPointer<X> &other) const noexcept;
84 template <typename X>
85 bool owner_equal(const QWeakPointer<X> &other) const noexcept;
86
87 size_t owner_hash() const noexcept;
88};
89
90template <class T>
91size_t qHash(const QSharedPointer<T> &key, size_t seed = 0) noexcept;
92
93template <class T>
94class QWeakPointer
95{
96public:
97 // basic accessor functions
98 bool isNull() const;
99 operator bool() const;
100 bool operator!() const;
101
102 // constructors:
103 QWeakPointer();
104 QWeakPointer(const QWeakPointer<T> &other) noexcept;
105 QWeakPointer(QWeakPointer<T> &&other) noexcept;
106 QWeakPointer(const QSharedPointer<T> &other);
107
109
110 QWeakPointer<T> &operator=(const QWeakPointer<T> &other) noexcept;
111 QWeakPointer<T> &operator=(QWeakPointer<T> &&other) noexcept;
112 QWeakPointer<T> &operator=(const QSharedPointer<T> &other);
113
114 QWeakPointer(const QObject *other);
115 QWeakPointer<T> &operator=(const QObject *other);
116
117 void swap(QWeakPointer<T> &other) noexcept;
118
119 T *data() const;
120 void clear();
121
122 QSharedPointer<T> toStrongRef() const;
123 QSharedPointer<T> lock() const;
124
125 // owner-based comparisons
126 template <typename X>
127 bool owner_before(const QWeakPointer<X> &other) const noexcept;
128 template <typename X>
129 bool owner_before(const QSharedPointer<X> &other) const noexcept;
130
131 template <typename X>
132 bool owner_equal(const QWeakPointer<X> &other) const noexcept;
133 template <typename X>
134 bool owner_equal(const QSharedPointer<X> &other) const noexcept;
135
136 size_t owner_hash() const noexcept;
137};
138
139template <class T>
141{
142public:
143 QSharedPointer<T> sharedFromThis();
144 QSharedPointer<const T> sharedFromThis() const;
145};
146
147template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
148template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
149template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2);
150template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2);
151template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2);
152template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2);
153template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
154template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
155template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
156template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
157template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
158template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
159template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
160template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
161template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
162template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
163template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
164template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
165
166template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
167template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);
168template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src);
169template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src);
170template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src);
171template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src);
172template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src);
173template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src);
174template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src);
175template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src);
176template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src);
177template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src);
178
179template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
180
182
183#endif // Q_QDOC
184
185#endif // QSHAREDPOINTER_H
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
template< class X, class T > std::shared_ptr< X > qobject_pointer_cast(const std::shared_ptr< T > &src)
bool owner_equal(const QSharedPointer< X > &other) const noexcept
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
template< class T, class X > bool operator==(const QSharedPointer< T > &ptr1, const QSharedPointer< X > &ptr2)
Returns true if ptr1 and ptr2 refer to the same pointer.
QSharedPointer< X > constCast() const
Performs a \tt const_cast from this pointer's type to \tt X and returns a QSharedPointer that shares ...
bool operator!() const noexcept
Returns true if this object refers to \nullptr.
friend class QSharedPointer
Creates a QSharedPointer that is null (the object is holding a reference to \nullptr).
template< class X, class T > QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &src)
Returns a shared pointer to the pointer held by src, using a dynamic cast to type \tt X to obtain an ...
QWeakPointer< T > toWeakRef() const
Returns a weak reference object that shares the pointer referenced by this object.
QSharedPointer< X > objectCast() const
T * data() const noexcept
Returns the value of the pointer referenced by this object.
template< class X, class T > QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &src)
Returns a shared pointer to the pointer held by src, cast to type \tt X.
QSharedPointer< X > dynamicCast() const
Performs a dynamic cast from this pointer's type to \tt X and returns a QSharedPointer that shares th...
~QSharedPointer()
Destroys this QSharedPointer object.
size_t owner_hash() const noexcept
friend class QWeakPointer
bool owner_before(const QWeakPointer< X > &other) const noexcept
template< class T, class X > bool operator!=(const QSharedPointer< T > &ptr1, const QSharedPointer< X > &ptr2)
Returns true if ptr1 and ptr2 refer to distinct pointers.
template< class X, class T > QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &other)
Returns a shared pointer to the pointer held by other, cast to type \tt X.
T * get() const noexcept
template< class X, class T > QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &src)
The qSharedPointerObjectCast function is for casting a shared pointer.
T & operator*() const
Provides access to the shared pointer's members.
QSharedPointer & operator=(const QSharedPointer &other) noexcept
Makes this object share other's pointer.
QSharedPointer< X > staticCast() const
Performs a static cast from this pointer's type to \tt X and returns a QSharedPointer that shares the...
T * operator->() const noexcept
Provides access to the shared pointer's members.
void swap(QSharedPointer &other) noexcept
template< class T > qHash(const QSharedPointer< T > &key, size_t seed)
Returns the hash value for key, using seed to seed the calculation.
void clear()
Clears this QSharedPointer object, dropping the reference that it may have had to the pointer.
\inmodule QtCore
Combined button and popup list for selecting options.
static ControlElement< T > * ptr(QWidget *widget)
GLuint64 key
GLenum src
GLdouble GLdouble t
Definition qopenglext.h:243
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
Q_INLINE_TEMPLATE QWeakPointer< X > qWeakPointerCast(const QSharedPointer< T > &src)
QReadWriteLock lock
[0]
QSharedPointer< T > other(t)
[5]
view create()
QJSValueList args