diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2021-12-06 08:11:00 -0800 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2021-12-17 07:56:24 +0000 |
commit | 4631b61c813ed9c095db638f25c21916cd2928e4 (patch) | |
tree | 1be76cb21339455228e76431cedc388693e194ca /src | |
parent | 48b4f144e7f690f5ae840124eaad4eaa67a02681 (diff) |
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>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/global/qglobalstatic.h | 4 | ||||
-rw-r--r-- | src/corelib/global/qglobalstatic.qdoc | 17 | ||||
-rw-r--r-- | src/corelib/kernel/qapplicationstatic.h | 9 | ||||
-rw-r--r-- | src/corelib/kernel/qapplicationstatic.qdoc | 57 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager.cpp | 6 |
5 files changed, 37 insertions, 56 deletions
diff --git a/src/corelib/global/qglobalstatic.h b/src/corelib/global/qglobalstatic.h index cf96e5ae8e..098aee966b 100644 --- a/src/corelib/global/qglobalstatic.h +++ b/src/corelib/global/qglobalstatic.h @@ -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 diff --git a/src/corelib/global/qglobalstatic.qdoc b/src/corelib/global/qglobalstatic.qdoc index 99416ad102..1e3fc6f11c 100644 --- a/src/corelib/global/qglobalstatic.qdoc +++ b/src/corelib/global/qglobalstatic.qdoc @@ -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 diff --git a/src/corelib/kernel/qapplicationstatic.h b/src/corelib/kernel/qapplicationstatic.h index f34bd806a2..93a5062776 100644 --- a/src/corelib/kernel/qapplicationstatic.h +++ b/src/corelib/kernel/qapplicationstatic.h @@ -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 diff --git a/src/corelib/kernel/qapplicationstatic.qdoc b/src/corelib/kernel/qapplicationstatic.qdoc index 5903cdc20b..76507a2824 100644 --- a/src/corelib/kernel/qapplicationstatic.qdoc +++ b/src/corelib/kernel/qapplicationstatic.qdoc @@ -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 */ diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index 230aa40af6..b04f0cba7a 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -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) { |