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
qglobal.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2017 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// Qt-Security score:significant reason:default
5
6#include "qplatformdefs.h"
7#include "qstring.h"
9#include "qlist.h"
10#include "qdir.h"
11#include "qdatetime.h"
12#include <private/qlocale_tools_p.h>
15
16#ifdef Q_OS_WIN
17# include <qt_windows.h>
18#endif
19
20#if defined(Q_OS_VXWORKS) && defined(_WRS_KERNEL)
21# include <envLib.h>
22#endif
23
24#if defined(Q_OS_INTEGRITY)
25extern "C" {
26 // Function mmap resides in libshm_client.a. To be able to link with it one needs
27 // to define symbols 'shm_area_password' and 'shm_area_name', because the library
28 // is meant to allow the application that links to it to use POSIX shared memory
29 // without full system POSIX.
30# pragma weak shm_area_password
31# pragma weak shm_area_name
32 char shm_area_password[] = "dummy";
33 char shm_area_name[] = "dummy";
34}
35#endif
36
37QT_BEGIN_NAMESPACE
38
39using namespace Qt::StringLiterals;
40
41/*!
42 \headerfile <QtGlobal>
43 \inmodule QtCore
44 \title Global Qt Declarations
45 \ingroup funclists
46
47 \brief The <QtGlobal> header file includes an assortment of other headers.
48
49 Up to Qt 6.5, most Qt header files included <QtGlobal>. Before Qt 6.5,
50 <QtGlobal> defined an assortment of global declarations. Most of these
51 have moved, at Qt 6.5, to separate headers, so that source code can
52 include only what it needs, rather than the whole assortment. For now,
53 <QtGlobal> includes those other headers (see next section), but future
54 releases of Qt may remove some of these headers from <QtGlobal> or
55 condition their inclusion on a version check. Likewise, in future
56 releases, some Qt headers that currently include <QtGlobal> may stop
57 doing so. The hope is that this will improve compilation times by
58 avoiding global declarations when they are not used.
59
60 \section1 List of Headers Extracted from <QtGlobal>
61
62 \table
63 \header \li Header \li Summary
64 \row \li <QFlags> \li Type-safe way of combining enum values
65 \row \li \l <QForeach> \li Qt's implementation of foreach and forever loops
66 \row \li \l <QFunctionPointer> \li Typedef for a pointer-to-function type
67 \row \li \l <QApplicationStatic> \li For Q_APPLICATION_STATIC
68 \row \li <QGlobalStatic> \li Thread-safe initialization of global static objects
69 \row \li \l <QOverload> \li Helpers for resolving member function overloads
70 \row \li <QSysInfo> \li A helper class to get system information
71 \row \li \l <QTypeInfo> \li Helpers to get type information
72 \row \li \l <QtAssert> \li Q_ASSERT and other runtime checks
73 \row \li \l <QtClassHelperMacros> \li Qt class helper macros
74 \row \li \l <QtCompilerDetection> \li Compiler-specific macro definitions
75 \row \li \l <QtDeprecationMarkers> \li Deprecation helper macros
76 \row \li \l <QtEnvironmentVariables> \li Helpers for working with environment variables
77 \row \li <QtExceptionHandling> \li Helpers for exception handling
78 \row \li \l <QtLogging> \li Qt logging helpers
79 \row \li <QtMalloc> \li Memory allocation helpers
80 \row \li \l <QtMinMax> \li Helpers for comparing values
81 \row \li \l <QtNumeric> \li Various numeric functions
82 \row \li \l <QtPreprocessorSupport> \li Helper preprocessor macros
83 \row \li \l <QtProcessorDetection> \li Architecture-specific macro definitions
84 \row \li \l <QtResource> \li Helpers for initializing and cleaning resources
85 \row \li \l <QtSwap> \li Implementation of qSwap()
86 \row \li \l <QtSystemDetection> \li Platform-specific macro definitions
87 \row \li \l <QtTranslation> \li Qt translation helpers
88 \row \li \l <QtTypeTraits> \li Qt type traits
89 \row \li \l <QtTypes> \li Qt fundamental type declarations
90 \row \li \l <QtVersionChecks> \li QT_VERSION_CHECK and related checks
91 \row \li \l <QtVersion> \li QT_VERSION_STR and qVersion()
92 \endtable
93*/
94
95/*
96 Dijkstra's bisection algorithm to find the square root of an integer.
97 Deliberately not exported as part of the Qt API, but used in
98 qtextdocument.cpp.
99*/
100Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n)
101{
102 // n must be in the range 0...UINT_MAX/2-1
103 if (n >= (UINT_MAX >> 2)) {
104 unsigned int r = 2 * qt_int_sqrt(n / 4);
105 unsigned int r2 = r + 1;
106 return (n >= r2 * r2) ? r2 : r;
107 }
108 uint h, p = 0, q = 1, r = n;
109 while (q <= n)
110 q <<= 2;
111 while (q != 1) {
112 q >>= 2;
113 h = p + q;
114 p >>= 1;
115 if (r >= h) {
116 p += q;
117 r -= h;
118 }
119 }
120 return p;
121}
122
123// Also specified to behave as if they call tzset():
124// localtime() -- but not localtime_r(), which we use when threaded
125// strftime() -- not used (except in tests)
126
131
132Q_GLOBAL_STATIC(QInternal_CallBackTable, global_callback_table)
133
134bool QInternal::registerCallback(Callback cb, qInternalCallback callback)
135{
136 if (unsigned(cb) < unsigned(QInternal::LastCallback)) {
137 QInternal_CallBackTable *cbt = global_callback_table();
138 cbt->callbacks.resize(cb + 1);
139 cbt->callbacks[cb].append(callback);
140 return true;
141 }
142 return false;
143}
144
145bool QInternal::unregisterCallback(Callback cb, qInternalCallback callback)
146{
147 if (unsigned(cb) < unsigned(QInternal::LastCallback)) {
148 if (global_callback_table.exists()) {
149 QInternal_CallBackTable *cbt = global_callback_table();
150 return cbt->callbacks[cb].removeAll(callback) > 0;
151 }
152 }
153 return false;
154}
155
156bool QInternal::activateCallbacks(Callback cb, void **parameters)
157{
158 Q_ASSERT_X(cb >= 0, "QInternal::activateCallback()", "Callback id must be a valid id");
159
160 if (!global_callback_table.exists())
161 return false;
162
163 QInternal_CallBackTable *cbt = &(*global_callback_table);
164 if (cbt && cb < cbt->callbacks.size()) {
165 QList<qInternalCallback> callbacks = cbt->callbacks[cb];
166 bool ret = false;
167 for (int i = 0; i < callbacks.size(); ++i)
168 ret |= (callbacks.at(i))(parameters);
169 return ret;
170 }
171 return false;
172}
173
174/*!
175 \macro QT_NO_KEYWORDS
176 \relates <QtGlobal>
177
178 Define this macro to disable the Qt-specific keywords that are usually enabled,
179 such as \c signals and \c slots. Use \c Q_SIGNALS and \c Q_SLOTS instead.
180
181 Libraries should define this macro to make sure that they don't use the generic
182 keywords without the \c Q_ prefix in their public headers.
183
184 \sa QT_NO_FOREACH
185*/
186
187/*!
188 \macro QT_NAMESPACE
189 \internal
190
191 If this macro is defined to \c ns all Qt classes are put in a namespace
192 called \c ns. Also, moc will output code putting metaobjects etc.
193 into namespace \c ns.
194
195 \sa QT_BEGIN_NAMESPACE, QT_END_NAMESPACE,
196 QT_PREPEND_NAMESPACE, QT_USE_NAMESPACE,
197 QT_BEGIN_INCLUDE_NAMESPACE, QT_END_INCLUDE_NAMESPACE,
198 QT_BEGIN_MOC_NAMESPACE, QT_END_MOC_NAMESPACE,
199*/
200
201/*!
202 \macro QT_PREPEND_NAMESPACE(identifier)
203 \internal
204
205 This macro qualifies \a identifier with the full namespace.
206 It expands to \c{::QT_NAMESPACE::identifier} if \c QT_NAMESPACE is defined
207 and only \a identifier otherwise.
208
209 \sa QT_NAMESPACE
210*/
211
212/*!
213 \macro QT_USE_NAMESPACE
214 \internal
215
216 This macro expands to using QT_NAMESPACE if QT_NAMESPACE is defined
217 and nothing otherwise.
218
219 \sa QT_NAMESPACE
220*/
221
222/*!
223 \macro QT_BEGIN_NAMESPACE
224 \internal
225
226 This macro expands to
227
228 \snippet code/src_corelib_global_qglobal.cpp begin namespace macro
229
230 if \c QT_NAMESPACE is defined and nothing otherwise. If should always
231 appear in the file-level scope and be followed by \c QT_END_NAMESPACE
232 at the same logical level with respect to preprocessor conditionals
233 in the same file.
234
235 As a rule of thumb, \c QT_BEGIN_NAMESPACE should appear in all Qt header
236 and Qt source files after the last \c{#include} line and before the first
237 declaration.
238
239 If that rule can't be followed because, e.g., \c{#include} lines and
240 declarations are wildly mixed, place \c QT_BEGIN_NAMESPACE before
241 the first declaration and wrap the \c{#include} lines in
242 \c QT_BEGIN_INCLUDE_NAMESPACE and \c QT_END_INCLUDE_NAMESPACE.
243
244 When using the \c QT_NAMESPACE feature in user code
245 (e.g., when building plugins statically linked to Qt) where
246 the user code is not intended to go into the \c QT_NAMESPACE
247 namespace, all forward declarations of Qt classes need to
248 be wrapped in \c QT_BEGIN_NAMESPACE and \c QT_END_NAMESPACE.
249 After that, a \c QT_USE_NAMESPACE should follow.
250 No further changes should be needed.
251
252 \sa QT_NAMESPACE
253*/
254
255/*!
256 \macro QT_END_NAMESPACE
257 \internal
258
259 This macro expands to
260
261 \snippet code/src_corelib_global_qglobal.cpp end namespace macro
262
263 if \c QT_NAMESPACE is defined and nothing otherwise. It is used to cancel
264 the effect of \c QT_BEGIN_NAMESPACE.
265
266 If a source file ends with a \c{#include} directive that includes a moc file,
267 \c QT_END_NAMESPACE should be placed before that \c{#include}.
268
269 \sa QT_NAMESPACE
270*/
271
272/*!
273 \macro QT_BEGIN_INCLUDE_NAMESPACE
274 \internal
275
276 This macro is equivalent to \c QT_END_NAMESPACE.
277 It only serves as syntactic sugar and is intended
278 to be used before #include lines within a
279 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
280
281 \sa QT_NAMESPACE
282*/
283
284/*!
285 \macro QT_END_INCLUDE_NAMESPACE
286 \internal
287
288 This macro is equivalent to \c QT_BEGIN_NAMESPACE.
289 It only serves as syntactic sugar and is intended
290 to be used after #include lines within a
291 \c QT_BEGIN_NAMESPACE ... \c QT_END_NAMESPACE block.
292
293 \sa QT_NAMESPACE
294*/
295
296/*!
297 \macro QT_BEGIN_MOC_NAMESPACE
298 \internal
299
300 This macro is output by moc at the beginning of
301 moc files. It is equivalent to \c QT_USE_NAMESPACE.
302
303 \sa QT_NAMESPACE
304*/
305
306/*!
307 \macro QT_END_MOC_NAMESPACE
308 \internal
309
310 This macro is output by moc at the beginning of
311 moc files. It expands to nothing.
312
313 \sa QT_NAMESPACE
314*/
315
316/*!
317 \macro qMove(x)
318 \relates <QtGlobal>
319 \deprecated
320
321 Use \c std::move instead.
322
323 It expands to "std::move".
324
325 qMove takes an rvalue reference to its parameter \a x, and converts it to an xvalue.
326*/
327
328/*!
329 \macro QT_ENABLE_STRICT_MODE_UP_TO
330 \relates <QtGlobal>
331 \since 6.8
332
333 Defining this macro will disable a number of Qt APIs that are
334 deemed suboptimal or dangerous.
335
336 This macro's value must be set to a Qt version, using
337 \l{QT_VERSION_CHECK}'s encoding. For instance, in order to set it
338 to Qt 6.6, define \c{QT_ENABLE_STRICT_MODE_UP_TO=0x060600}.
339 This will disable only the APIs introduced in versions up to (and
340 including) the specified Qt version.
341
342 If the \l QT_DISABLE_DEPRECATED_UP_TO macro is \e not defined,
343 then QT_ENABLE_STRICT_MODE_UP_TO will define it as well,
344 to the same value.
345
346 This macro should always be set to the minimum Qt version that
347 your project wants to support.
348
349 The APIs disabled by this macro are listed in the table below,
350 together with the minimum value to use in order to disable them.
351
352 \table
353 \header \li Version \li Disabled APIs
354 \row \li 6.0.0 \li \l{foreach-keyword}{foreach} (see \l{QT_NO_FOREACH})
355 \row \li 6.0.0 \li QString conversions towards \c{const char *} / QByteArray (see \l{QT_NO_CAST_TO_ASCII})
356 \row \li 6.0.0 \li QByteArray implicit conversions towards \c{const char *} (see \l{QT_NO_CAST_FROM_BYTEARRAY})
357 \row \li 6.0.0 \li QUrl implicit conversions from QString (see \l{QT_NO_URL_CAST_FROM_STRING})
358 \row \li 6.0.0 \li Allowing narrowing conversions in signal-slot connections (see \l{QT_NO_NARROWING_CONVERSIONS_IN_CONNECT})
359 \row \li 6.0.0 \li Java-style iterators for Qt containers
360 \row \li 6.6.0 \li The qExchange() function (see \l{QT_NO_QEXCHANGE})
361 \row \li 6.7.0 \li Overloads of QObject::connect that do not take a context object (see \l{QT_NO_CONTEXTLESS_CONNECT})
362 \row \li 6.8.0 \li The qAsConst() function (see \l{QT_NO_QASCONST})
363 \row \li 6.8.0 \li File-related I/O classes have their \c{open()} functions marked \c{[[nodiscard]]} (see \l{QT_USE_NODISCARD_FILE_OPEN})
364 \row\li 6.9.0 \li The qsnprintf() and qvnprintf() functions (see \l{QT_NO_QSNPRINTF}).
365 \row\li 6.11.0 \li Support for qHash overloads without a seed (see \l{QT_NO_SINGLE_ARGUMENT_QHASH_OVERLOAD}).
366 \endtable
367
368 Moreover, individual APIs may also get disabled as part of the
369 setting of QT_DISABLE_DEPRECATED_UP_TO. Please refer to each class'
370 documentation for more details.
371
372 \sa QT_DISABLE_DEPRECATED_UP_TO, QT_NO_KEYWORDS, QT_VERSION_CHECK
373*/
374
375Q_LOGGING_CATEGORY(lcNativeInterface, "qt.nativeinterface")
376
377QT_END_NAMESPACE
378
379#ifndef QT_NO_QOBJECT
380#include "moc_qnamespace.cpp"
381#endif
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
Q_CORE_EXPORT Q_DECL_CONST_FUNCTION unsigned int qt_int_sqrt(unsigned int n)
\inmodule QtCore \title Global Qt Declarations
Definition qglobal.cpp:100
QList< QList< qInternalCallback > > callbacks
Definition qglobal.cpp:129