Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
// Qt-Security score:significant reason:default
4
5
#
ifndef
QGLOBALSTATIC_H
6
#
define
QGLOBALSTATIC_H
7
8
#
include
<
QtCore
/
qassert
.
h
>
9
#
include
<
QtCore
/
qatomic
.
h
>
10
#
include
<
QtCore
/
qtclasshelpermacros
.
h
>
11
12
#
include
<
atomic
>
// for bootstrapped (no thread) builds
13
#
include
<
type_traits
>
14
15
QT_BEGIN_NAMESPACE
16
17
namespace
QtGlobalStatic
{
18
enum
GuardValues
{
19
Destroyed
= -2,
20
Initialized
= -1,
21
Uninitialized
= 0,
22
Initializing
= 1
23
};
24
25
template
<
typename
QGS>
union
Holder
26
{
27
using
Type
=
typename
QGS::
QGS_Type
;
28
using
PlainType
=
std
::
remove_cv_t
<
Type
>;
29
30
static
constexpr
bool
ConstructionIsNoexcept
=
noexcept
(QGS::innerFunction(
nullptr
));
31
Q_CONSTINIT
static
inline
QBasicAtomicInteger
<
qint8
>
guard
= {
QtGlobalStatic
::
Uninitialized
};
32
33
// union's sole member
34
PlainType
storage
;
35
36
Holder
()
noexcept
(
ConstructionIsNoexcept
)
37
{
38
QGS::innerFunction(pointer());
39
guard.storeRelaxed(QtGlobalStatic::Initialized);
40
}
41
42
~
Holder
()
43
{
44
// TSAN does not support atomic_thread_fence and GCC complains:
45
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97868
46
// https://github.com/google/sanitizers/issues/1352
47
// QTBUG-134415
48
QT_WARNING_PUSH
49
#
if
defined
(
Q_CC_GNU_ONLY
)
&&
Q_CC_GNU
>=
1100
50
QT_WARNING_DISABLE_GCC(
"-Wtsan"
)
51
#
endif
52
// import changes to *pointer() by other threads before running ~PlainType():
53
std
::atomic_thread_fence(
std
::memory_order_acquire);
54
QT_WARNING_POP
55
pointer()->~PlainType();
56
guard.storeRelease(QtGlobalStatic::Destroyed);
57
}
58
59
PlainType
*
pointer
()
noexcept
60
{
61
return
&storage;
62
}
63
64
Q_DISABLE_COPY_MOVE
(
Holder
)
65
};
66
}
67
68
template
<
typename
Holder>
struct
QGlobalStatic
69
{
70
using
Type
=
typename
Holder::
Type
;
71
72
bool
isDestroyed
()
const
noexcept
{
return
guardValue
(
)
<=
QtGlobalStatic
::
Destroyed
; }
73
bool
exists
()
const
noexcept
{
return
guardValue
(
)
==
QtGlobalStatic
::
Initialized
; }
74
operator
Type
*()
75
{
76
if
(
isDestroyed
(
)
)
77
return
nullptr
;
78
return
instance
(
)
;
79
}
80
Type
*
operator
()()
81
{
82
if
(
isDestroyed
(
)
)
83
return
nullptr
;
84
return
instance
(
)
;
85
}
86
Type
*
operator
->()
87
{
88
Q_ASSERT_X(!
isDestroyed
(
)
, Q_FUNC_INFO,
89
"The global static was used after being destroyed"
);
90
return
instance
(
)
;
91
}
92
Type
&
operator
*()
93
{
94
Q_ASSERT_X(!
isDestroyed
(
)
, Q_FUNC_INFO,
95
"The global static was used after being destroyed"
);
96
return
*
instance
(
)
;
97
}
98
99
protected
:
100
static
Type
*
instance
()
noexcept
(
Holder
::
ConstructionIsNoexcept
)
101
{
102
static
Holder holder;
103
return
holder.pointer();
104
}
105
static
QtGlobalStatic
::
GuardValues
guardValue
()
noexcept
106
{
107
return
QtGlobalStatic
::
GuardValues
(Holder::guard.loadAcquire());
108
}
109
};
110
111
#
define
Q_GLOBAL_STATIC_WITH_ARGS
(
TYPE
,
NAME
,
ARGS
)
112
QT_WARNING_PUSH
113
QT_WARNING_DISABLE_CLANG
(
"-Wunevaluated-expression"
)
114
namespace
{
struct
Q_QGS_
##
NAME
{
115
typedef
TYPE
QGS_Type
;
116
static
void
innerFunction
(
void
*
pointer
)
117
noexcept
(
noexcept
(
std
::
remove_cv_t
<
QGS_Type
>
ARGS
)
)
118
{
119
new
(
pointer
)
QGS_Type
ARGS
;
120
}
121
}
;
}
122
Q_CONSTINIT
static
QGlobalStatic
<
QtGlobalStatic
::
Holder
<
Q_QGS_
##
NAME
>>
NAME
;
123
QT_WARNING_POP
124
/**/
125
126
#
define
Q_GLOBAL_STATIC
(
TYPE
,
NAME
,
...
)
127
Q_GLOBAL_STATIC_WITH_ARGS
(
TYPE
,
NAME
,
(
__VA_ARGS__
)
)
128
129
QT_END_NAMESPACE
130
#
endif
// QGLOBALSTATIC_H
QHash
\inmodule QtCore
Definition
qhash.h:844
QPermission
\inmodule QtCore \inheaderfile QPermissions
Definition
qpermissions.h:36
QPermissions::Private::requestPermission
void requestPermission(const QPermission &permission, const PermissionCallback &callback)
Definition
qpermissions.cpp:701
QPermissions::Private::checkPermission
Qt::PermissionStatus checkPermission(const QPermission &permission)
Definition
qpermissions.cpp:694
QPermissions
Definition
qpermissions.cpp:693
QtGlobalStatic
Definition
qglobalstatic.h:17
QtGlobalStatic::GuardValues
GuardValues
Definition
qglobalstatic.h:18
QtGlobalStatic::Destroyed
@ Destroyed
Definition
qglobalstatic.h:19
QtGlobalStatic::Initialized
@ Initialized
Definition
qglobalstatic.h:20
QtGlobalStatic::Initializing
@ Initializing
Definition
qglobalstatic.h:22
QtGlobalStatic::Uninitialized
@ Uninitialized
Definition
qglobalstatic.h:21
Qt
Definition
qcompare.h:111
Qt::PermissionStatus
PermissionStatus
Definition
qnamespace.h:1770
Qt::PermissionStatus::Denied
@ Denied
Definition
qnamespace.h:1773
Qt::PermissionStatus::Granted
@ Granted
Definition
qnamespace.h:1772
Qt::PermissionStatus::Undetermined
@ Undetermined
Definition
qnamespace.h:1771
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
Q_GLOBAL_STATIC_WITH_ARGS
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Definition
qglobalstatic.h:111
Q_GLOBAL_STATIC
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
Definition
qglobalstatic.h:126
QStringLiteral
#define QStringLiteral(str)
Definition
qstring.h:1865
QGlobalStatic
Definition
qglobalstatic.h:69
QGlobalStatic::operator()
Type * operator()()
Definition
qglobalstatic.h:80
QGlobalStatic::operator*
Type & operator*()
Definition
qglobalstatic.h:92
QGlobalStatic::Type
typename Holder::Type Type
Definition
qglobalstatic.h:70
QGlobalStatic::instance
static Type * instance() noexcept(Holder::ConstructionIsNoexcept)
Definition
qglobalstatic.h:100
QGlobalStatic::guardValue
static QtGlobalStatic::GuardValues guardValue() noexcept
Definition
qglobalstatic.h:105
QGlobalStatic::exists
bool exists() const noexcept
Definition
qglobalstatic.h:73
QGlobalStatic::operator->
Type * operator->()
Definition
qglobalstatic.h:86
QGlobalStatic::operator Type *
operator Type *()
Definition
qglobalstatic.h:74
QGlobalStatic::isDestroyed
bool isDestroyed() const noexcept
Definition
qglobalstatic.h:72
QtGlobalStatic::Holder
Definition
qglobalstatic.h:26
QtGlobalStatic::Holder::pointer
PlainType * pointer() noexcept
Definition
qglobalstatic.h:59
QtGlobalStatic::Holder::ConstructionIsNoexcept
static constexpr bool ConstructionIsNoexcept
Definition
qglobalstatic.h:30
QtGlobalStatic::Holder::Holder
Holder() noexcept(ConstructionIsNoexcept)
Definition
qglobalstatic.h:36
QtGlobalStatic::Holder::storage
PlainType storage
Definition
qglobalstatic.h:34
QtGlobalStatic::Holder::Type
typename QGS::QGS_Type Type
Definition
qglobalstatic.h:27
QtGlobalStatic::Holder::~Holder
~Holder()
Definition
qglobalstatic.h:42
qtbase
src
corelib
global
qglobalstatic.h
Generated on
for Qt by
1.18.0