Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qglobalstatic.h
Go to the documentation of this file.
1// Copyright (C) 2021 Intel Corporation.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGLOBALSTATIC_H
5#define QGLOBALSTATIC_H
6
7#include <QtCore/qassert.h>
8#include <QtCore/qatomic.h>
9#include <QtCore/qtclasshelpermacros.h>
10
11#include <atomic> // for bootstrapped (no thread) builds
12#include <type_traits>
13
14QT_BEGIN_NAMESPACE
15
16namespace QtGlobalStatic {
23
24template <typename QGS> union Holder
25{
26 using Type = typename QGS::QGS_Type;
28
29 static constexpr bool ConstructionIsNoexcept = noexcept(QGS::innerFunction(nullptr));
31
32 // union's sole member
34
36 {
37 QGS::innerFunction(pointer());
38 guard.storeRelaxed(QtGlobalStatic::Initialized);
39 }
40
42 {
43 // TSAN does not support atomic_thread_fence and GCC complains:
44 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97868
45 // https://github.com/google/sanitizers/issues/1352
46 // QTBUG-134415
47QT_WARNING_PUSH
48#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU >= 1100
49QT_WARNING_DISABLE_GCC("-Wtsan")
50#endif
51 // import changes to *pointer() by other threads before running ~PlainType():
52 std::atomic_thread_fence(std::memory_order_acquire);
53QT_WARNING_POP
54 pointer()->~PlainType();
55 guard.storeRelease(QtGlobalStatic::Destroyed);
56 }
57
58 PlainType *pointer() noexcept
59 {
60 return &storage;
61 }
62
64};
65}
66
67template <typename Holder> struct QGlobalStatic
68{
69 using Type = typename Holder::Type;
70
71 bool isDestroyed() const noexcept { return guardValue() <= QtGlobalStatic::Destroyed; }
72 bool exists() const noexcept { return guardValue() == QtGlobalStatic::Initialized; }
74 {
75 if (isDestroyed())
76 return nullptr;
77 return instance();
78 }
80 {
81 if (isDestroyed())
82 return nullptr;
83 return instance();
84 }
86 {
87 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
88 "The global static was used after being destroyed");
89 return instance();
90 }
92 {
93 Q_ASSERT_X(!isDestroyed(), Q_FUNC_INFO,
94 "The global static was used after being destroyed");
95 return *instance();
96 }
97
98protected:
100 {
101 static Holder holder;
102 return holder.pointer();
103 }
105 {
106 return QtGlobalStatic::GuardValues(Holder::guard.loadAcquire());
107 }
108};
109
110#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
111 QT_WARNING_PUSH
112 QT_WARNING_DISABLE_CLANG("-Wunevaluated-expression")
113 namespace { struct Q_QGS_ ## NAME {
114 typedef TYPE QGS_Type;
115 static void innerFunction(void *pointer)
116 noexcept(noexcept(std::remove_cv_t<QGS_Type> ARGS))
117 {
118 new (pointer) QGS_Type ARGS;
119 }
120 }; }
121 Q_CONSTINIT static QGlobalStatic<QtGlobalStatic::Holder<Q_QGS_ ## NAME>> NAME;
122 QT_WARNING_POP
123 /**/
124
125#define Q_GLOBAL_STATIC(TYPE, NAME, ...)
126 Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, (__VA_ARGS__))
127
128QT_END_NAMESPACE
129#endif // QGLOBALSTATIC_H
\inmodule QtCore
Definition qhash.h:837
\inmodule QtCore \inheaderfile QPermissions
void requestPermission(const QPermission &permission, const PermissionCallback &callback)
Qt::PermissionStatus checkPermission(const QPermission &permission)
Definition qcompare.h:76
PermissionStatus
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
#define QStringLiteral(str)
Definition qstring.h:1826
Type * operator()()
Type & operator*()
typename Holder::Type Type
static Type * instance() noexcept(Holder::ConstructionIsNoexcept)
static QtGlobalStatic::GuardValues guardValue() noexcept
bool exists() const noexcept
Type * operator->()
bool isDestroyed() const noexcept
PlainType * pointer() noexcept
static constexpr bool ConstructionIsNoexcept
Holder() noexcept(ConstructionIsNoexcept)
typename QGS::QGS_Type Type