Q_{APPLICATION,GLOBAL}_STATIC: use variadic macros

We can't remove Q_GLOBAL_STATIC_WITH_ARGS, for compatibility reasons.
It's also the only way to pass uniform initialization (i.e., initialize
the value as value{with_braces}), though I don't think this is used
almost anywhere due to the fact that you couldn't pass more than one
argument. But Q_APPLICATION_STATIC is new in 6.3, so we have time to
change it.

[ChangeLog][QtCore][QGlobalStatic] The Q_GLOBAL_STATIC macro is now
variadic. Any extra arguments are used as constructor arguments,
obliterating the need to use Q_GLOBAL_STATIC_WITH_ARGS().

Pick-to: 6.3
Change-Id: Ib42b3adc93bf4d43bd55fffd16be3656a512fe53
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
bb10
Thiago Macieira 2021-12-06 08:11:00 -08:00 committed by Marc Mutz
parent 48b4f144e7
commit 4631b61c81
5 changed files with 37 additions and 56 deletions

View File

@ -145,8 +145,8 @@ protected:
static QGlobalStatic<QtGlobalStatic::Holder<Q_QGS_ ## NAME>> NAME; \
/**/
#define Q_GLOBAL_STATIC(TYPE, NAME) \
Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ())
#define Q_GLOBAL_STATIC(TYPE, NAME, ...) \
Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, (__VA_ARGS__))
QT_END_NAMESPACE
#endif // QGLOBALSTATIC_H

View File

@ -26,7 +26,7 @@
****************************************************************************/
/*!
\macro Q_GLOBAL_STATIC(Type, VariableName)
\macro Q_GLOBAL_STATIC(Type, VariableName, ...)
\since 5.1
\relates QGlobalStatic
@ -36,6 +36,11 @@
will not increase the application or the library's load time. Additionally,
the object is initialized in a thread-safe manner on all platforms.
Since Qt 6.3, this macro admits variadic arguments, which are used to
initialize the object, thus making the need for \l
Q_GLOBAL_STATIC_WITH_ARGS unnecessary. Please note the arguments do not
require an extra set of parentheses, unlike the older macro.
The typical use of this macro is as follows, in a global context (that is,
outside of any function bodies):
@ -273,12 +278,13 @@
\endomit
\sa Q_GLOBAL_STATIC_WITH_ARGS(), QGlobalStatic
\sa Q_GLOBAL_STATIC_WITH_ARGS(), Q_APPLICATION_STATIC(), QGlobalStatic
*/
/*!
\macro Q_GLOBAL_STATIC_WITH_ARGS(Type, VariableName, Arguments)
\since 5.1
\obsolete
\relates QGlobalStatic
Creates a global and static object of type \l QGlobalStatic, of name \a
@ -296,7 +302,12 @@
\endcode
The \a Arguments macro parameter must always include the parentheses or, if
C++11 uniform initialization is allowed, the braces.
C++11 uniform initialization is allowed, the braces. The above call is
equivalent to
\code
Q_GLOBAL_STATIC(MyType, staticType, 42, "Hello", "World")
\endcode
Aside from the actual initialization of the contents with the supplied
arguments, this macro behaves identically to Q_GLOBAL_STATIC(). Please

View File

@ -100,21 +100,18 @@ template <typename QAS> struct ApplicationHolder
};
} // namespace QtGlobalStatic
#define Q_APPLICATION_STATIC_WITH_ARGS(TYPE, NAME, ARGS) \
#define Q_APPLICATION_STATIC(TYPE, NAME, ...) \
namespace { struct Q_QAS_ ## NAME { \
typedef TYPE QAS_Type; \
static void innerFunction(void *pointer) \
noexcept(noexcept(std::remove_cv_t<QAS_Type> ARGS)) \
noexcept(noexcept(std::remove_cv_t<QAS_Type>(__VA_ARGS__))) \
{ \
new (pointer) QAS_Type ARGS; \
new (pointer) QAS_Type(__VA_ARGS__); \
} \
}; } \
static QGlobalStatic<QtGlobalStatic::ApplicationHolder<Q_QAS_ ## NAME>> NAME;\
/**/
#define Q_APPLICATION_STATIC(TYPE, NAME) \
Q_APPLICATION_STATIC_WITH_ARGS(TYPE, NAME, ())
QT_END_NAMESPACE
#endif // QAPPLICATIONSTATIC_H

View File

@ -26,17 +26,18 @@
****************************************************************************/
/*!
\macro Q_APPLICATION_STATIC(Type, VariableName)
\macro Q_APPLICATION_STATIC(Type, VariableName, ...)
\since 6.3
\relates QGlobalStatic
This macro extends Q_GLOBAL_STATIC and creates a global and static object
of type \l QGlobalStatic, of name \a VariableName and that behaves as a
pointer to \a Type, where the actual lifetime of the type is bound to the
QCoreApplication. The object created by Q_APPLICATION_STATIC initializes
itself on the first use, which means that it will not increase the application
or the library's load time. Additionally, the object is initialized in a
thread-safe manner on all platforms.
of type \l QGlobalStatic, of name \a VariableName, initialized by the
variadic arguments, and that behaves as a pointer to \a Type, where the
actual lifetime of the type is bound to the QCoreApplication. The object
created by Q_APPLICATION_STATIC initializes itself on the first use, which
means that it will not increase the application or the library's load time.
Additionally, the object is initialized in a thread-safe manner on all
platforms.
In contrast to Q_GLOBAL_STATIC where the type is only meant to be destroyed at
program exit, here the actual lifetime of the type is bound to the lifetime of
@ -56,9 +57,14 @@
outside of any function bodies):
\code
Q_APPLICATION_STATIC(MyQObjectType, staticType)
Q_APPLICATION_STATIC(MyQObjectType, staticType, "some string", function())
\endcode
Do note that the arguments passed in variadic fashion to this macro are
evaluated every time the object is constructed, so in the above example,
the function \c{function} will be called more than once if the object is
recreated.
Aside from the value also being bound to the lifetime of the QCoreApplication,
this macro behaves identically to Q_GLOBAL_STATIC(). Please see that macro's
documentation for more information.
@ -99,38 +105,5 @@
\endomit
\sa Q_APPLICATION_STATIC_WITH_ARGS(), QGlobalStatic
*/
/*!
\macro Q_APPLICATION_STATIC_WITH_ARGS(Type, VariableName, Arguments)
\since 6.3
\relates QGlobalStatic
Creates a global and static object of type \l QGlobalStatic, of name \a
VariableName, initialized by the arguments \a Arguments and that behaves as
a pointer to \a Type, where the actual lifetime of the type is bound to the
QCoreApplication. The object created by Q_APPLICATION_STATIC_WITH_ARGS
initializes itself on the first use, which means that it will not increase
the application or the library's load time. Additionally, the object is
initialized in a thread-safe manner on all platforms.
Since the value is bound to the QCoreApplication it should only ever be accessed,
if there is a valid QCoreApplication::instance().
The typical use of this macro is as follows, in a global context (that is,
outside of any function bodies):
\code
Q_APPLICATION_STATIC_WITH_ARGS(MyQObjectType, staticType, (42, "Hello", "World"))
\endcode
The \a Arguments macro parameter must always include the parentheses or, if
C++11 uniform initialization is allowed, the braces.
Aside from the actual initialization of the contents with the supplied
arguments, this macro behaves identically to Q_APPLICATION_STATIC(). Please
see that macro's documentation for more information.
\sa Q_APPLICATION_STATIC(), QGlobalStatic
\sa Q_GLOBAL_STATIC, QGlobalStatic
*/

View File

@ -106,9 +106,9 @@ Q_APPLICATION_STATIC(QNetworkAccessFileBackendFactory, fileBackend)
Q_GLOBAL_STATIC(QNetworkAccessDebugPipeBackendFactory, debugpipeBackend)
#endif
Q_APPLICATION_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QNetworkAccessBackendFactory_iid,
QLatin1String("/networkaccess")))
Q_APPLICATION_STATIC(QFactoryLoader, loader, QNetworkAccessBackendFactory_iid,
QLatin1String("/networkaccess"))
#if defined(Q_OS_MACOS)
bool getProxyAuth(const QString& proxyHostname, const QString &scheme, QString& username, QString& password)
{