Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qcoreevent.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qcoreevent.h"
6#include "qcoreevent_p.h"
7#include "qcoreapplication.h"
9
10#include "qbasicatomic.h"
11
12#include <qtcore_tracepoints_p.h>
13
14#include <limits>
15
17
18Q_TRACE_POINT(qtcore, QEvent_ctor, QEvent *event, QEvent::Type type);
19Q_TRACE_POINT(qtcore, QEvent_dtor, QEvent *event, QEvent::Type type);
20
269 : t(type), m_reserved(0),
270 m_inputEvent(false), m_pointerEvent(false), m_singlePointEvent(false)
271{
272 Q_TRACE(QEvent_ctor, this, type);
273}
274
322{
323 if (m_posted && QCoreApplication::instance())
325}
326
332{ return new QEvent(*this); }
333
415namespace {
416template <size_t N>
417struct QBasicAtomicBitField {
418 enum {
419 BitsPerInt = std::numeric_limits<uint>::digits,
420 NumInts = (N + BitsPerInt - 1) / BitsPerInt,
421 NumBits = N
422 };
423
424 // This atomic int points to the next (possibly) free ID saving
425 // the otherwise necessary scan through 'data':
428
429 constexpr QBasicAtomicBitField() = default;
430
431 bool allocateSpecific(int which) noexcept
432 {
433 QBasicAtomicInteger<uint> &entry = data[which / BitsPerInt];
434 const uint old = entry.loadRelaxed();
435 const uint bit = 1U << (which % BitsPerInt);
436 return !(old & bit) // wasn't taken
437 && entry.testAndSetRelaxed(old, old | bit); // still wasn't taken
438
439 // don't update 'next' here - it's unlikely that it will need
440 // to be updated, in the general case, and having 'next'
441 // trailing a bit is not a problem, as it is just a starting
442 // hint for allocateNext(), which, when wrong, will just
443 // result in a few more rounds through the allocateNext()
444 // loop.
445 }
446
447 int allocateNext() noexcept
448 {
449 // Unroll loop to iterate over ints, then bits? Would save
450 // potentially a lot of cmpxchgs, because we can scan the
451 // whole int before having to load it again.
452
453 // Then again, this should never execute many iterations, so
454 // leave like this for now:
455 for (uint i = next.loadRelaxed(); i < NumBits; ++i) {
456 if (allocateSpecific(i)) {
457 // remember next (possibly) free id:
458 const uint oldNext = next.loadRelaxed();
459 next.testAndSetRelaxed(oldNext, qMax(i + 1, oldNext));
460 return i;
461 }
462 }
463 return -1;
464 }
465};
466
467} // unnamed namespace
468
469typedef QBasicAtomicBitField<QEvent::MaxUser - QEvent::User + 1> UserEventTypeRegistry;
470
472
473static inline int registerEventTypeZeroBased(int id) noexcept
474{
475 // if the type hint hasn't been registered yet, take it:
476 if (id < UserEventTypeRegistry::NumBits && id >= 0 && userEventTypeRegistry.allocateSpecific(id))
477 return id;
478
479 // otherwise, ignore hint:
480 return userEventTypeRegistry.allocateNext();
481}
482
497{
499 return result < 0 ? -1 : QEvent::MaxUser - result ;
500}
501
528 : QEvent(Timer), id(timerId)
529{}
530
532
533
570 : QEvent(type), c(child)
571{}
572
574
575
621 : QEvent(QEvent::DynamicPropertyChange), n(name)
622{
623}
624
626
627
640 : QEvent(QEvent::DeferredDelete)
641{ }
642
644
646
647#include "moc_qcoreevent.cpp"
T loadRelaxed() const noexcept
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qcoreevent.h:372
static void removePostedEvent(QEvent *)
Removes event from the queue of posted events, and emits a warning message if appropriate.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
\inmodule QtCore
Definition qcoreevent.h:45
static int registerEventType(int hint=-1) noexcept
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
QEvent(Type type)
Constructs an event object of type type.
virtual ~QEvent()
Destroys the event.
virtual QEvent * clone() const
Creates and returns an identical copy of this event.
\inmodule QtCore
Definition qobject.h:90
\inmodule QtCore
Definition qcoreevent.h:359
QTimerEvent(int timerId)
Constructs a timer event object with the timer identifier set to timerId.
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
static int registerEventTypeZeroBased(int id) noexcept
QBasicAtomicBitField< QEvent::MaxUser - QEvent::User+1 > UserEventTypeRegistry
static Q_CONSTINIT UserEventTypeRegistry userEventTypeRegistry
#define Q_IMPL_EVENT_COMMON(Class)
Definition qcoreevent.h:31
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum GLuint id
[7]
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint name
GLfloat n
struct _cl_event * event
const GLubyte * c
GLuint entry
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define Q_TRACE(x,...)
Definition qtrace_p.h:144
#define Q_TRACE_POINT(provider, tracepoint,...)
Definition qtrace_p.h:232
unsigned int uint
Definition qtypes.h:29
QLayoutItem * child
[0]
Definition moc.h:24