Qt 6.x
The Qt SDK
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);
44
45 ~QSharedPointer() { }
46
50
51 template <class X>
53 template <class X>
55
56 void swap(QSharedPointer<T> &other) noexcept;
57
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
77template <class T>
78size_t qHash(const QSharedPointer<T> &key, size_t seed = 0) noexcept;
79
80template <class T>
81class QWeakPointer
82{
83public:
84 // basic accessor functions
85 bool isNull() const;
86 operator bool() const;
87 bool operator!() const;
88
89 // constructors:
91 QWeakPointer(const QWeakPointer<T> &other) noexcept;
94
96
97 QWeakPointer<T> &operator=(const QWeakPointer<T> &other) noexcept;
98 QWeakPointer<T> &operator=(QWeakPointer<T> &&other) noexcept;
99 QWeakPointer<T> &operator=(const QSharedPointer<T> &other);
100
101 QWeakPointer(const QObject *other);
102 QWeakPointer<T> &operator=(const QObject *other);
103
104 void swap(QWeakPointer<T> &other) noexcept;
105
106 T *data() const;
107 void clear();
108
109 QSharedPointer<T> toStrongRef() const;
110 QSharedPointer<T> lock() const;
111};
112
113template <class T>
115{
116public:
119};
120
121template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
122template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
123template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2);
124template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2);
125template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2);
126template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2);
127template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
128template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
129template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
130template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
131template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
132template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
133template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
134template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
135template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
136template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
137template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
138template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
139
140template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
141template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);
142template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src);
143template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src);
144template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src);
145template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src);
146template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src);
147template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src);
148template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src);
149template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src);
150template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src);
151template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src);
152
153template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
154
156
157#endif // Q_QDOC
158
159#endif // QSHAREDPOINTER_H
QSharedPointer< T > sharedFromThis()
\inmodule QtCore
Definition qobject.h:90
\inmodule QtCore
bool isNull() const noexcept
Returns true if this object refers to \nullptr.
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).
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.
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.
T * get() const noexcept
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
void clear()
Clears this QSharedPointer object, dropping the reference that it may have had to the pointer.
\inmodule QtCore
b clear()
Combined button and popup list for selecting options.
bool isNull(const T &t)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
static ControlElement< T > * ptr(QWidget *widget)
GLuint64 key
GLenum src
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble t
Definition qopenglext.h:243
#define X(name)
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &ptr)
QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &ptr)
QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &ptr)
std::shared_ptr< X > qobject_pointer_cast(const std::shared_ptr< T > &src)
QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &ptr)
Q_INLINE_TEMPLATE QWeakPointer< X > qWeakPointerCast(const QSharedPointer< T > &src)
QReadWriteLock lock
[0]
QSharedPointer< T > other(t)
[5]
this swap(other)
view create()
QJSValueList args