QtBase  v6.3.1
qscopedpointer.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 QSCOPEDPOINTER_H
41 #define QSCOPEDPOINTER_H
42 
43 #include <QtCore/qglobal.h>
44 
45 #include <stdlib.h>
46 
48 
49 template <typename T>
51 {
52  static inline void cleanup(T *pointer) noexcept
53  {
54  // Enforce a complete type.
55  // If you get a compile error here, read the section on forward declared
56  // classes in the QScopedPointer documentation.
57  typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
58  (void) sizeof(IsIncompleteType);
59 
60  delete pointer;
61  }
62  void operator()(T *pointer) const noexcept
63  {
65  }
66 };
67 
68 template <typename T>
70 {
71  static inline void cleanup(T *pointer) noexcept
72  {
73  // Enforce a complete type.
74  // If you get a compile error here, read the section on forward declared
75  // classes in the QScopedPointer documentation.
76  typedef char IsIncompleteType[ sizeof(T) ? 1 : -1 ];
77  (void) sizeof(IsIncompleteType);
78 
79  delete[] pointer;
80  }
81  void operator()(T *pointer) const noexcept
82  {
84  }
85 };
86 
88 {
89  static inline void cleanup(void *pointer) noexcept { free(pointer); }
90  void operator()(void *pointer) const noexcept { cleanup(pointer); }
91 };
92 
93 #ifndef QT_NO_QOBJECT
94 template <typename T>
96 {
97  static inline void cleanup(T *pointer) { if (pointer) pointer->deleteLater(); }
98  void operator()(T *pointer) const { cleanup(pointer); }
99 };
100 
101 class QObject;
103 #endif
104 
105 template <typename T, typename Cleanup = QScopedPointerDeleter<T> >
106 class [[nodiscard]] QScopedPointer
107 {
108 public:
109  explicit QScopedPointer(T *p = nullptr) noexcept : d(p)
110  {
111  }
112 
114  {
115  T *oldD = this->d;
116  Cleanup::cleanup(oldD);
117  }
118 
119  inline T &operator*() const
120  {
121  Q_ASSERT(d);
122  return *d;
123  }
124 
125  T *operator->() const noexcept
126  {
127  return d;
128  }
129 
130  bool operator!() const noexcept
131  {
132  return !d;
133  }
134 
135  explicit operator bool() const
136  {
137  return !isNull();
138  }
139 
140  T *data() const noexcept
141  {
142  return d;
143  }
144 
145  T *get() const noexcept
146  {
147  return d;
148  }
149 
150  bool isNull() const noexcept
151  {
152  return !d;
153  }
154 
155  void reset(T *other = nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval<T *>())))
156  {
157  if (d == other)
158  return;
159  T *oldD = qExchange(d, other);
160  Cleanup::cleanup(oldD);
161  }
162 
163 #if QT_DEPRECATED_SINCE(6, 1)
164  QT_DEPRECATED_VERSION_X_6_1("Use std::unique_ptr instead, and call release().")
165  T *take() noexcept
166  {
167  T *oldD = qExchange(d, nullptr);
168  return oldD;
169  }
170 #endif
171 
172 #if QT_DEPRECATED_SINCE(6, 2)
173  QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
174  void swap(QScopedPointer<T, Cleanup> &other) noexcept
175  {
176  qt_ptr_swap(d, other.d);
177  }
178 #endif
179 
180  typedef T *pointer;
181 
182  friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
183  {
184  return lhs.data() == rhs.data();
185  }
186 
187  friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPointer<T, Cleanup> &rhs) noexcept
188  {
189  return lhs.data() != rhs.data();
190  }
191 
192  friend bool operator==(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
193  {
194  return lhs.isNull();
195  }
196 
197  friend bool operator==(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
198  {
199  return rhs.isNull();
200  }
201 
202  friend bool operator!=(const QScopedPointer<T, Cleanup> &lhs, std::nullptr_t) noexcept
203  {
204  return !lhs.isNull();
205  }
206 
207  friend bool operator!=(std::nullptr_t, const QScopedPointer<T, Cleanup> &rhs) noexcept
208  {
209  return !rhs.isNull();
210  }
211 
212 #if QT_DEPRECATED_SINCE(6, 2)
213  QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedPointer.")
214  friend void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) noexcept
215  { p1.swap(p2); }
216 #endif
217 
218 protected:
219  T *d;
220 
221 private:
223 };
224 
225 template <typename T, typename Cleanup = QScopedPointerArrayDeleter<T> >
226 class [[nodiscard]] QScopedArrayPointer : public QScopedPointer<T, Cleanup>
227 {
228  template <typename Ptr>
229  using if_same_type = typename std::enable_if<std::is_same<typename std::remove_cv<T>::type, Ptr>::value, bool>::type;
230 public:
231  inline QScopedArrayPointer() : QScopedPointer<T, Cleanup>(nullptr) {}
232  inline ~QScopedArrayPointer() = default;
233 
234  template <typename D, if_same_type<D> = true>
236  : QScopedPointer<T, Cleanup>(p)
237  {
238  }
239 
240  inline T &operator[](int i)
241  {
242  return this->d[i];
243  }
244 
245  inline const T &operator[](int i) const
246  {
247  return this->d[i];
248  }
249 
250 #if QT_DEPRECATED_SINCE(6, 2)
251  QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
252  void swap(QScopedArrayPointer &other) noexcept // prevent QScopedPointer <->QScopedArrayPointer swaps
254 #endif
255 
256 private:
257  explicit inline QScopedArrayPointer(void *)
258  {
259  // Enforce the same type.
260 
261  // If you get a compile error here, make sure you declare
262  // QScopedArrayPointer with the same template type as you pass to the
263  // constructor. See also the QScopedPointer documentation.
264 
265  // Storing a scalar array as a pointer to a different type is not
266  // allowed and results in undefined behavior.
267  }
268 
270 };
271 
272 #if QT_DEPRECATED_SINCE(6, 2)
273 template <typename T, typename Cleanup>
274 QT_DEPRECATED_VERSION_X_6_2("Use std::unique_ptr instead of QScopedArrayPointer.")
275 inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) noexcept
276 { lhs.swap(rhs); }
277 #endif
278 
280 
281 #endif // QSCOPEDPOINTER_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
void swap(QPixmap &other) noexcept
Definition: qpixmap.h:79
The QScopedArrayPointer class stores a pointer to a dynamically allocated array of objects,...
~QScopedArrayPointer()=default
const T & operator[](int i) const
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
T & operator*() const
T * get() const noexcept
QScopedPointer(T *p=nullptr) noexcept
T * data() const noexcept
T * operator->() const noexcept
friend bool operator==(const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
friend bool operator==(std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
friend bool operator==(const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
bool operator!() const noexcept
friend bool operator!=(std::nullptr_t, const QScopedPointer< T, Cleanup > &rhs) noexcept
friend bool operator!=(const QScopedPointer< T, Cleanup > &lhs, std::nullptr_t) noexcept
friend bool operator!=(const QScopedPointer< T, Cleanup > &lhs, const QScopedPointer< T, Cleanup > &rhs) noexcept
bool isNull() const noexcept
void reset(T *other=nullptr) noexcept(noexcept(Cleanup::cleanup(std::declval< T * >())))
#define T(x)
Definition: main.cpp:42
QPixmap p2
QPixmap p1
[0]
auto it unsigned count const
Definition: hb-iter.hh:848
#define inline
Definition: md4c.c:45
GeneratorWrapper< T > take(size_t target, GeneratorWrapper< T > &&generator)
Definition: catch_p_p.h:4151
Definition: qfloat16.h:381
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld cleanup[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1 beq endif SRC MASK if dst_r_bpp DST_R else add endif PF add sub src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head pixblock_size cache_preload_simple process_pixblock_tail pixinterleave dst_w_basereg irp beq endif process_pixblock_tail_head tst beq irp if pixblock_size chunk_size tst beq pixld SRC pixld MASK if DST_R else pixld DST_R endif if src_basereg pixdeinterleave mask_basereg pixdeinterleave dst_r_basereg process_pixblock_head if pixblock_size cache_preload_simple endif process_pixblock_tail pixinterleave dst_w_basereg irp if pixblock_size chunk_size tst beq if DST_W else pixst DST_W else mov ORIG_W endif add lsl if lsl endif if lsl endif lsl endif lsl endif lsl endif subs mov DST_W if regs_shortage str endif bge start_of_loop_label endm macro generate_composite_function
void
Definition: png.h:1080
#define Q_DISABLE_COPY_MOVE(Class)
Definition: qglobal.h:519
#define QT_DEPRECATED_VERSION_X_6_2(text)
Definition: qglobal.h:460
#define QT_DEPRECATED_VERSION_X_6_1(text)
Definition: qglobal.h:452
GLenum type
Definition: qopengl.h:270
GLsizei const void * pointer
Definition: qopenglext.h:384
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
QScopedPointerObjectDeleteLater< QObject > QScopedPointerDeleteLater
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]
this swap(other)
static void cleanup(T *pointer) noexcept
void operator()(T *pointer) const noexcept
void operator()(T *pointer) const noexcept
static void cleanup(T *pointer) noexcept
void operator()(T *pointer) const
static void cleanup(T *pointer)
void operator()(void *pointer) const noexcept
static void cleanup(void *pointer) noexcept
Definition: main.cpp:38
#define rhs