Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
qapplicationstatic.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2021 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#ifndef QAPPLICATIONSTATIC_H
6#define QAPPLICATIONSTATIC_H
7
8#include <QtCore/QMutex>
9#include <QtCore/qcoreapplication.h>
10#include <QtCore/qglobalstatic.h>
11
12#include <new>
13
15
16namespace QtGlobalStatic {
17template <typename QAS> struct ApplicationHolder
18{
19 using Type = typename QAS::QAS_Type;
20 using PlainType = std::remove_cv_t<Type>;
21
22 Q_CONSTINIT static inline struct { alignas(Type) unsigned char data[sizeof(Type)]; } storage = {};
24 Q_CONSTINIT static inline QBasicMutex mutex {};
25
26 static constexpr bool MutexLockIsNoexcept = noexcept(mutex.lock());
27 static constexpr bool ConstructionIsNoexcept = noexcept(QAS::innerFunction(nullptr));
28
29 ApplicationHolder() = default;
30 Q_DISABLE_COPY_MOVE(ApplicationHolder)
32 {
34 // No mutex! Up to external code to ensure no race happens.
36 realPointer()->~PlainType();
37 }
38 }
39
41 {
42 return std::launder(reinterpret_cast<PlainType *>(&storage));
43 }
44
45 // called from QGlobalStatic::instance()
47 {
49 return realPointer();
50 QMutexLocker locker(&mutex);
52 QAS::innerFunction(&storage);
53 const auto *app = QCoreApplication::instance();
56 }
57 return realPointer();
58 }
59
60 static void reset()
61 {
62 // we only synchronize using the mutex here, not the guard
63 QMutexLocker locker(&mutex);
64 realPointer()->~PlainType();
66 }
67};
68} // namespace QtGlobalStatic
69
70#define Q_APPLICATION_STATIC(TYPE, NAME, ...) \
71 namespace { struct Q_QAS_ ## NAME { \
72 typedef TYPE QAS_Type; \
73 static void innerFunction(void *pointer) \
74 noexcept(noexcept(std::remove_cv_t<QAS_Type>(__VA_ARGS__))) \
75 { \
76 new (pointer) QAS_Type(__VA_ARGS__); \
77 } \
78 }; } \
79 static QGlobalStatic<QtGlobalStatic::ApplicationHolder<Q_QAS_ ## NAME>> NAME;\
80
81
83
84#endif // QAPPLICATIONSTATIC_H
void storeRelaxed(T newValue) noexcept
T loadAcquire() const noexcept
void storeRelease(T newValue) noexcept
T loadRelaxed() const noexcept
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
\inmodule QtCore
Definition qmutex.h:317
\inmodule QtCore
Definition qmutex.h:285
void lock() noexcept
Locks the mutex.
Definition qmutex.h:290
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
Combined button and popup list for selecting options.
@ DirectConnection
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLboolean reset
QApplication app(argc, argv)
[0]
static Q_CONSTINIT struct QtGlobalStatic::ApplicationHolder::@28 storage
static constexpr bool MutexLockIsNoexcept
static constexpr bool ConstructionIsNoexcept
static Q_CONSTINIT QBasicAtomicInteger< qint8 > guard
PlainType * pointer() noexcept(MutexLockIsNoexcept &&ConstructionIsNoexcept)
static Q_CONSTINIT QBasicMutex mutex
std::remove_cv_t< Type > PlainType
Definition moc.h:24