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

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

source code of qtbase/src/corelib/global/qglobal.cpp