QtBase  v6.3.1
qpropertyprivate.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 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 QPROPERTYPRIVATE_H
41 #define QPROPERTYPRIVATE_H
42 
43 //
44 // W A R N I N G
45 // -------------
46 //
47 // This file is not part of the Qt API. It exists purely as an
48 // implementation detail. This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtCore/qglobal.h>
55 #include <QtCore/qtaggedpointer.h>
56 #include <QtCore/qmetatype.h>
57 
58 #include <functional>
59 
61 
62 class QBindingStorage;
63 
64 template<typename Class, typename T, auto Offset, auto Setter, auto Signal, auto Getter>
66 
67 namespace QtPrivate {
68 // QPropertyBindingPrivatePtr operates on a RefCountingMixin solely so that we can inline
69 // the constructor and copy constructor
70 struct RefCounted {
71  int ref = 0;
72  void addRef() {++ref;}
73  bool deref() {--ref; return ref;}
74 };
75 }
76 
77 class QQmlPropertyBinding;
80 {
81 public:
83  T &operator*() const { return *d; }
84  T *operator->() noexcept { return d; }
85  T *operator->() const noexcept { return d; }
86  explicit operator T *() { return d; }
87  explicit operator const T *() const noexcept { return d; }
88  T *data() const noexcept { return d; }
89  T *get() const noexcept { return d; }
90  const T *constData() const noexcept { return d; }
91  T *take() noexcept { T *x = d; d = nullptr; return x; }
92 
93  QPropertyBindingPrivatePtr() noexcept : d(nullptr) { }
95  {
96  if (d && (--d->ref == 0))
98  }
99  Q_CORE_EXPORT void destroyAndFreeMemory();
100 
101  explicit QPropertyBindingPrivatePtr(T *data) noexcept : d(data) { if (d) d->addRef(); }
103  : d(o.d) { if (d) d->addRef(); }
104 
105  void reset(T *ptr = nullptr) noexcept;
106 
108  {
109  reset(o.d);
110  return *this;
111  }
113  {
114  reset(o);
115  return *this;
116  }
117  QPropertyBindingPrivatePtr(QPropertyBindingPrivatePtr &&o) noexcept : d(qExchange(o.d, nullptr)) {}
119 
120  operator bool () const noexcept { return d != nullptr; }
121  bool operator!() const noexcept { return d == nullptr; }
122 
124  { qt_ptr_swap(d, other.d); }
125 
127  { return p1.d == p2.d; }
129  { return p1.d != p2.d; }
130  friend bool operator==(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
131  { return p1.d == ptr; }
132  friend bool operator!=(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
133  { return p1.d != ptr; }
134  friend bool operator==(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
135  { return ptr == p2.d; }
136  friend bool operator!=(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
137  { return ptr != p2.d; }
138  friend bool operator==(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
139  { return !p1; }
140  friend bool operator!=(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
141  { return p1; }
142  friend bool operator==(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
143  { return !p2; }
144  friend bool operator!=(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
145  { return p2; }
146 
147 private:
149 };
150 
155 
157 {
158 public:
159  // sentinel to check whether a class inherits QUntypedPropertyData
161 };
162 
163 template <typename T>
164 class QPropertyData;
165 
166 // Used for grouped property evaluations
167 namespace QtPrivate {
168 class QPropertyBindingData;
169 }
172 {
173  // acts as QPropertyBindingData::d_ptr
175  /*
176  The two members below store the original binding data and property
177  data pointer of the property which gets proxied.
178  They are set in QPropertyDelayedNotifications::addProperty
179  */
182 };
183 
184 namespace QtPrivate {
185 struct BindingEvaluationState;
186 
187 /* used in BindingFunctionVTable::createFor; on all other compilers, void would work, but on
188  MSVC this causes C2182 when compiling in C++20 mode. As we only need to provide some default
189  value which gets ignored, we introduce this dummy type.
190 */
191 struct MSVCWorkAround {};
192 
194 {
195  using CallFn = bool(*)(QMetaType, QUntypedPropertyData *, void *);
196  using DtorFn = void(*)(void *);
197  using MoveCtrFn = void(*)(void *, void *);
198  const CallFn call;
202 
203  template<typename Callable, typename PropertyType=MSVCWorkAround>
204  static constexpr BindingFunctionVTable createFor()
205  {
206  static_assert (alignof(Callable) <= alignof(std::max_align_t), "Bindings do not support overaligned functors!");
207  return {
208  /*call=*/[](QMetaType metaType, QUntypedPropertyData *dataPtr, void *f){
209  if constexpr (!std::is_invocable_v<Callable>) {
210  // we got an untyped callable
211  static_assert (std::is_invocable_r_v<bool, Callable, QMetaType, QUntypedPropertyData *> );
212  auto untypedEvaluationFunction = static_cast<Callable *>(f);
213  return std::invoke(*untypedEvaluationFunction, metaType, dataPtr);
214  } else if constexpr (!std::is_same_v<PropertyType, MSVCWorkAround>) {
215  Q_UNUSED(metaType);
216  QPropertyData<PropertyType> *propertyPtr = static_cast<QPropertyData<PropertyType> *>(dataPtr);
217  // That is allowed by POSIX even if Callable is a function pointer
218  auto evaluationFunction = static_cast<Callable *>(f);
219  PropertyType newValue = std::invoke(*evaluationFunction);
220  if constexpr (QTypeTraits::has_operator_equal_v<PropertyType>) {
221  if (newValue == propertyPtr->valueBypassingBindings())
222  return false;
223  }
224  propertyPtr->setValueBypassingBindings(std::move(newValue));
225  return true;
226  } else {
227  // Our code will never instantiate this
228  Q_UNREACHABLE();
229  return false;
230  }
231  },
232  /*destroy*/[](void *f){ static_cast<Callable *>(f)->~Callable(); },
233  /*moveConstruct*/[](void *addr, void *other){
234  new (addr) Callable(std::move(*static_cast<Callable *>(other)));
235  },
236  /*size*/sizeof(Callable)
237  };
238  }
239 };
240 
241 template<typename Callable, typename PropertyType=MSVCWorkAround>
242 inline constexpr BindingFunctionVTable bindingFunctionVTable = BindingFunctionVTable::createFor<Callable, PropertyType>();
243 
244 
245 // writes binding result into dataPtr
248  void *functor;
249 };
250 
253 
264 class Q_CORE_EXPORT QPropertyBindingData
265 {
266  // Mutable because the address of the observer of the currently evaluating binding is stored here, for
267  // notification later when the value changes.
268  mutable quintptr d_ptr = 0;
269  friend struct QT_PREPEND_NAMESPACE(QPropertyBindingDataPointer);
270  friend class QT_PREPEND_NAMESPACE(QQmlPropertyBinding);
271  friend struct QT_PREPEND_NAMESPACE(QPropertyDelayedNotifications);
272 
273  template<typename Class, typename T, auto Offset, auto Setter, auto Signal, auto Getter>
274  friend class QT_PREPEND_NAMESPACE(QObjectCompatProperty);
275 
277 public:
278  QPropertyBindingData() = default;
282 
283  // Is d_ptr pointing to a binding (1) or list of notifiers (0)?
284  static inline constexpr quintptr BindingBit = 0x1;
285  // Is d_ptr pointing to QPropertyProxyBindingData (1) or to an actual binding/list of notifiers?
286  static inline constexpr quintptr DelayedNotificationBit = 0x2;
287 
288  bool hasBinding() const { return d_ptr & BindingBit; }
289  bool isNotificationDelayed() const { return d_ptr & DelayedNotificationBit; }
290 
291  QUntypedPropertyBinding setBinding(const QUntypedPropertyBinding &newBinding,
292  QUntypedPropertyData *propertyDataPtr,
293  QPropertyObserverCallback staticObserverCallback = nullptr,
294  QPropertyBindingWrapper bindingWrapper = nullptr);
295 
297  {
298  quintptr dd = d();
299  if (dd & BindingBit)
300  return reinterpret_cast<QPropertyBindingPrivate*>(dd - BindingBit);
301  return nullptr;
302 
303  }
304 
305  void evaluateIfDirty(const QUntypedPropertyData *) const; // ### Kept for BC reasons, unused
306 
308  {
309  if (hasBinding())
310  removeBinding_helper();
311  }
312 
314  {
315  if (!currentBinding)
316  return;
317  registerWithCurrentlyEvaluatingBinding_helper(currentBinding);
318  }
319  void registerWithCurrentlyEvaluatingBinding() const;
320  void notifyObservers(QUntypedPropertyData *propertyDataPtr) const;
321  void notifyObservers(QUntypedPropertyData *propertyDataPtr, QBindingStorage *storage) const;
322 private:
334  quintptr &d_ref() const
335  {
336  quintptr &d = d_ptr;
337  if (isNotificationDelayed())
338  return reinterpret_cast<QPropertyProxyBindingData *>(d_ptr & ~(BindingBit|DelayedNotificationBit))->d_ptr;
339  return d;
340  }
341  quintptr d() const { return d_ref(); }
342  void registerWithCurrentlyEvaluatingBinding_helper(BindingEvaluationState *currentBinding) const;
343  void removeBinding_helper();
344 
345  enum NotificationResult { Delayed, Evaluated };
346  NotificationResult notifyObserver_helper(
347  QUntypedPropertyData *propertyDataPtr, QPropertyObserverPointer observer,
348  QBindingStorage *storage) const;
349 };
350 
351 template <typename T, typename Tag>
353 {
354 public:
355  constexpr QTagPreservingPointerToPointer() = default;
356 
358  : d(reinterpret_cast<quintptr*>(ptr))
359  {}
360 
362  {
363  d = reinterpret_cast<quintptr *>(ptr);
364  return *this;
365  }
366 
368  {
369  d = reinterpret_cast<quintptr *>(ptr);
370  return *this;
371  }
372 
373  void clear()
374  {
375  d = nullptr;
376  }
377 
378  void setPointer(T *ptr)
379  {
380  *d = reinterpret_cast<quintptr>(ptr) | (*d & QTaggedPointer<T, Tag>::tagMask());
381  }
382 
383  T *get() const
384  {
385  return reinterpret_cast<T*>(*d & QTaggedPointer<T, Tag>::pointerMask());
386  }
387 
388  explicit operator bool() const
389  {
390  return d != nullptr;
391  }
392 
393 private:
394  quintptr *d = nullptr;
395 };
396 
397 namespace detail {
398  template <typename F>
400 
401  template<typename T, typename C>
402  struct ExtractClassFromFunctionPointer<T C::*> { using Class = C; };
403 
404  constexpr size_t getOffset(size_t o)
405  {
406  return o;
407  }
408  constexpr size_t getOffset(size_t (*offsetFn)())
409  {
410  return offsetFn();
411  }
412 }
413 
414 } // namespace QtPrivate
415 
417 
418 #endif // QPROPERTYPRIVATE_H
The QMetaType class manages named types in the meta-object system.
Definition: qmetatype.h:328
Declares a \l QObjectBindableProperty inside containingClass of type type with name name....
Definition: qproperty_p.h:461
void swap(QPropertyBindingPrivatePtr &other) noexcept
T * get() const noexcept
const T * constData() const noexcept
friend bool operator==(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
T * data() const noexcept
friend bool operator==(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
friend bool operator==(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
Q_CORE_EXPORT void destroyAndFreeMemory()
Definition: qproperty.cpp:54
QPropertyBindingPrivatePtr(QPropertyBindingPrivatePtr &&o) noexcept
friend bool operator==(const QPropertyBindingPrivatePtr &p1, const QPropertyBindingPrivatePtr &p2) noexcept
friend bool operator!=(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
friend bool operator==(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
bool operator!() const noexcept
QPropertyBindingPrivatePtr(const QPropertyBindingPrivatePtr &o) noexcept
T * operator->() const noexcept
void reset(T *ptr=nullptr) noexcept
Definition: qproperty.cpp:59
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, const QPropertyBindingPrivatePtr &p2) noexcept
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
QPropertyBindingPrivatePtr(T *data) noexcept
friend bool operator!=(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
QPropertyBindingPrivatePtr & operator=(T *o) noexcept
The QPropertyData class is a helper class for properties with automatic property bindings.
Definition: qproperty.h:85
parameter_type valueBypassingBindings() const
Definition: qproperty.h:104
void setValueBypassingBindings(parameter_type v)
Definition: qproperty.h:105
static constexpr quintptr pointerMask()
QPropertyBindingData & operator=(QPropertyBindingData &&other)=delete
void registerWithCurrentlyEvaluatingBinding(QtPrivate::BindingEvaluationState *currentBinding) const
QPropertyBindingPrivate * binding() const
QTagPreservingPointerToPointer< T, Tag > & operator=(T **ptr)
QTagPreservingPointerToPointer< T, Tag > & operator=(QTaggedPointer< T, Tag > *ptr)
constexpr QTagPreservingPointerToPointer()=default
QPixmap p2
QPixmap p1
[0]
quint64 getOffset(const unsigned char *&s, const ElfData &context)
Definition: elfreader.cpp:79
auto it unsigned count const
Definition: hb-iter.hh:848
bool(*)(QMetaType, QUntypedPropertyData *dataPtr, QPropertyBindingFunction) QPropertyBindingWrapper
constexpr BindingFunctionVTable bindingFunctionVTable
void(*)(QUntypedPropertyData *) QPropertyObserverCallback
void
Definition: png.h:1080
#define Q_UNREACHABLE()
size_t quintptr
Definition: qglobal.h:310
ptrdiff_t qsizetype
Definition: qglobal.h:308
#define Q_DISABLE_COPY(Class)
Definition: qglobal.h:515
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Class)
Definition: qglobal.h:556
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat f
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint ref
GLenum const void * addr
Definition: qopenglext.h:8875
Q_UNUSED(salary)
[21]
QStorageInfo storage
[1]
QSharedPointer< T > other(t)
[5]
QUntypedPropertyData * propertyData
const QtPrivate::QPropertyBindingData * originalBindingData
bool(*)(QMetaType, QUntypedPropertyData *, void *) CallFn
static constexpr BindingFunctionVTable createFor()
void(*)(void *, void *) MoveCtrFn
const QtPrivate::BindingFunctionVTable * vtable
Definition: main.cpp:38