QtBase  v6.3.1
qsharedpointer.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QSHAREDPOINTER_H
41 #define QSHAREDPOINTER_H
42 
43 #include <QtCore/qglobal.h>
44 #include <QtCore/qatomic.h>
45 #include <QtCore/qshareddata.h>
46 
47 #ifndef Q_QDOC
48 # include <QtCore/qsharedpointer_impl.h>
49 #else
50 
51 #include <memory> // for std::shared_ptr
52 
54 
55 
56 // These classes are here to fool qdoc into generating a better documentation
57 
58 template <class T>
59 class QSharedPointer
60 {
61 public:
62  // basic accessor functions
63  T *data() const;
64  T *get() const;
65  bool isNull() const;
66  operator bool() const;
67  bool operator!() const;
68  T &operator*() const;
69  T *operator->() const;
70 
71  // constructors
73  template <typename X> explicit QSharedPointer(X *ptr);
74  template <typename X, typename Deleter> QSharedPointer(X *ptr, Deleter d);
75  QSharedPointer(std::nullptr_t);
76  template <typename Deleter> QSharedPointer(std::nullptr_t, Deleter d);
80 
81  ~QSharedPointer() { }
82 
86 
87  void swap(QSharedPointer<T> &other) noexcept;
88 
89  QWeakPointer<T> toWeakRef() const;
90 
91  void clear();
92 
93  void reset();
94  void reset(T *t);
95  template <typename Deleter>
96  void reset(T *t, Deleter deleter);
97 
98  // casts:
99  template <class X> QSharedPointer<X> staticCast() const;
100  template <class X> QSharedPointer<X> dynamicCast() const;
101  template <class X> QSharedPointer<X> constCast() const;
102  template <class X> QSharedPointer<X> objectCast() const;
103 
104  template <typename... Args>
105  static inline QSharedPointer<T> create(Args &&... args);
106 };
107 
108 template <class T>
109 class QWeakPointer
110 {
111 public:
112  // basic accessor functions
113  bool isNull() const;
114  operator bool() const;
115  bool operator!() const;
116 
117  // constructors:
118  QWeakPointer();
119  QWeakPointer(const QWeakPointer<T> &other) noexcept;
120  QWeakPointer(QWeakPointer<T> &&other) noexcept;
122 
123  ~QWeakPointer();
124 
128 
129  QWeakPointer(const QObject *other);
131 
132  void swap(QWeakPointer<T> &other) noexcept;
133 
134  T *data() const;
135  void clear();
136 
138  QSharedPointer<T> lock() const;
139 };
140 
141 template <class T>
143 {
144 public:
147 };
148 
149 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
150 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
151 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const X *ptr2);
152 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const X *ptr2);
153 template<class T, class X> bool operator==(const T *ptr1, const QSharedPointer<X> &ptr2);
154 template<class T, class X> bool operator!=(const T *ptr1, const QSharedPointer<X> &ptr2);
155 template<class T, class X> bool operator==(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
156 template<class T, class X> bool operator!=(const QWeakPointer<T> &ptr1, const QSharedPointer<X> &ptr2);
157 template<class T, class X> bool operator==(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
158 template<class T, class X> bool operator!=(const QSharedPointer<T> &ptr1, const QWeakPointer<X> &ptr2);
159 template<class T> bool operator==(const QSharedPointer<T> &lhs, std::nullptr_t);
160 template<class T> bool operator!=(const QSharedPointer<T> &lhs, std::nullptr_t);
161 template<class T> bool operator==(std::nullptr_t, const QSharedPointer<T> &rhs);
162 template<class T> bool operator!=(std::nullptr_t, const QSharedPointer<T> &rhs);
163 template<class T> bool operator==(const QWeakPointer<T> &lhs, std::nullptr_t);
164 template<class T> bool operator!=(const QWeakPointer<T> &lhs, std::nullptr_t);
165 template<class T> bool operator==(std::nullptr_t, const QWeakPointer<T> &rhs);
166 template<class T> bool operator!=(std::nullptr_t, const QWeakPointer<T> &rhs);
167 
168 template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QSharedPointer<T> &other);
169 template <class X, class T> QSharedPointer<X> qSharedPointerCast(const QWeakPointer<T> &other);
170 template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QSharedPointer<T> &src);
171 template <class X, class T> QSharedPointer<X> qSharedPointerDynamicCast(const QWeakPointer<T> &src);
172 template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QSharedPointer<T> &src);
173 template <class X, class T> QSharedPointer<X> qSharedPointerConstCast(const QWeakPointer<T> &src);
174 template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QSharedPointer<T> &src);
175 template <class X, class T> QSharedPointer<X> qSharedPointerObjectCast(const QWeakPointer<T> &src);
176 template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(const std::shared_ptr<T> &src);
177 template <typename X, class T> std::shared_ptr<X> qobject_pointer_cast(std::shared_ptr<T> &&src);
178 template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(const std::shared_ptr<T> &src);
179 template <typename X, class T> std::shared_ptr<X> qSharedPointerObjectCast(std::shared_ptr<T> &&src);
180 
181 template <class X, class T> QWeakPointer<X> qWeakPointerCast(const QWeakPointer<T> &src);
182 
184 
185 #endif // Q_QDOC
186 
187 #endif // QSHAREDPOINTER_H
A base class that allows obtaining a QSharedPointer for an object already managed by a shared pointer...
QSharedPointer< T > sharedFromThis()
template< typename Enum > bool operator!=(Enum lhs, QFlags< Enum > rhs)
template< typename Enum > bool operator==(Enum lhs, QFlags< Enum > rhs)
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QSharedPointer class holds a strong reference to a shared pointer.
template< class X, class T > std::shared_ptr< X > qobject_pointer_cast(const std::shared_ptr< T > &src)
bool isNull() const noexcept
QSharedPointer< X > constCast() const
bool operator!() const noexcept
friend class QSharedPointer
template< class X > template< class T > QSharedPointer< X > qSharedPointerObjectCast(const QSharedPointer< T > &src)
The qSharedPointerObjectCast function is for casting a shared pointer.
QWeakPointer< T > toWeakRef() const
QSharedPointer< X > objectCast() const
T * data() const noexcept
QSharedPointer< X > dynamicCast() const
T * get() const noexcept
static QSharedPointer create(Args &&...arguments)
template< class X > template< class T > QSharedPointer< X > qSharedPointerCast(const QSharedPointer< T > &other)
QSharedPointer & operator=(const QSharedPointer &other) noexcept
QSharedPointer< X > staticCast() const
T * operator->() const noexcept
void swap(QSharedPointer &other) noexcept
template< class X > template< class T > QSharedPointer< X > qSharedPointerConstCast(const QSharedPointer< T > &src)
template< class X > template< class T > QSharedPointer< X > qSharedPointerDynamicCast(const QSharedPointer< T > &src)
The QWeakPointer class holds a weak reference to a shared pointer.
QWeakPointer & operator=(QWeakPointer< X > &&other) noexcept
QSharedPointer< T > toStrongRef() const
bool operator!() const noexcept
QSharedPointer< T > lock() const
bool isNull() const noexcept
friend class QWeakPointer
template< class X > template< class T > QWeakPointer< X > qWeakPointerCast(const QWeakPointer< T > &src)
void swap(QWeakPointer &other) noexcept
GLenum src
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble t
[9]
Definition: qopenglext.h:243
QSharedPointer< T > other(t)
[5]
Definition: main.cpp:58
Definition: main.cpp:38
#define rhs