From ff8de321e22b1e91f7fc04ffe54609b045f23835 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Thu, 28 Jul 2022 11:41:28 +0200 Subject: [PATCH] Extract header qoverload.h from qglobal.h Task-number: QTBUG-99313 Change-Id: Id827f95b5aa5d4e0d57dcc1060a0746bcaa34db3 Reviewed-by: Thiago Macieira --- src/corelib/CMakeLists.txt | 1 + src/corelib/global/qglobal.cpp | 51 --------------------- src/corelib/global/qglobal.h | 64 +------------------------- src/corelib/global/qoverload.h | 75 +++++++++++++++++++++++++++++++ src/corelib/global/qoverload.qdoc | 53 ++++++++++++++++++++++ 5 files changed, 130 insertions(+), 114 deletions(-) create mode 100644 src/corelib/global/qoverload.h create mode 100644 src/corelib/global/qoverload.qdoc diff --git a/src/corelib/CMakeLists.txt b/src/corelib/CMakeLists.txt index 9aa0d0b2ee..2fcc5b5b98 100644 --- a/src/corelib/CMakeLists.txt +++ b/src/corelib/CMakeLists.txt @@ -62,6 +62,7 @@ qt_internal_add_module(Core global/qnativeinterface.h global/qnativeinterface_p.h global/qnumeric.cpp global/qnumeric.h global/qnumeric_p.h global/qoperatingsystemversion.cpp global/qoperatingsystemversion.h global/qoperatingsystemversion_p.h + global/qoverload.h global/qprocessordetection.h global/qrandom.cpp global/qrandom.h global/qrandom_p.h global/qsysinfo.h diff --git a/src/corelib/global/qglobal.cpp b/src/corelib/global/qglobal.cpp index 3c9d947577..796749c146 100644 --- a/src/corelib/global/qglobal.cpp +++ b/src/corelib/global/qglobal.cpp @@ -1217,57 +1217,6 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined"); \sa qMin(), qMax() */ -/*! \fn template auto qOverload(T functionPointer) - \relates - \since 5.7 - - Returns a pointer to an overloaded function. The template - parameter is the list of the argument types of the function. - \a functionPointer is the pointer to the (member) function: - - \snippet code/src_corelib_global_qglobal.cpp 52 - - If a member function is also const-overloaded \l qConstOverload and - \l qNonConstOverload need to be used. - - qOverload() requires C++14 enabled. In C++11-only code, the helper - classes QOverload, QConstOverload, and QNonConstOverload can be used directly: - - \snippet code/src_corelib_global_qglobal.cpp 53 - - \note Qt detects the necessary C++14 compiler support by way of the feature - test recommendations from - \l{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations} - {C++ Committee's Standing Document 6}. - - \sa qConstOverload(), qNonConstOverload(), {Differences between String-Based - and Functor-Based Connections} -*/ - -/*! \fn template auto qConstOverload(T memberFunctionPointer) - \relates - \since 5.7 - - Returns the \a memberFunctionPointer pointer to a constant member function: - - \snippet code/src_corelib_global_qglobal.cpp 54 - - \sa qOverload, qNonConstOverload, {Differences between String-Based - and Functor-Based Connections} -*/ - -/*! \fn template auto qNonConstOverload(T memberFunctionPointer) - \relates - \since 5.7 - - Returns the \a memberFunctionPointer pointer to a non-constant member function: - - \snippet code/src_corelib_global_qglobal.cpp 54 - - \sa qOverload, qNonConstOverload, {Differences between String-Based - and Functor-Based Connections} -*/ - /*! \macro QT_VERSION_CHECK(major, minor, patch) \relates diff --git a/src/corelib/global/qglobal.h b/src/corelib/global/qglobal.h index 21762c39e2..f6a6013518 100644 --- a/src/corelib/global/qglobal.h +++ b/src/corelib/global/qglobal.h @@ -1213,62 +1213,6 @@ template inline auto qGetPtrHelper(Ptr &ptr) noexcept -> decltype #define Q_D(Class) Class##Private * const d = d_func() #define Q_Q(Class) Class * const q = q_func() -#ifdef Q_QDOC -// Just for documentation generation -template -auto qOverload(T functionPointer); -template -auto qConstOverload(T memberFunctionPointer); -template -auto qNonConstOverload(T memberFunctionPointer); -#else -template -struct QNonConstOverload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...)) const noexcept -> decltype(ptr) - { return ptr; } - - template - static constexpr auto of(R (T::*ptr)(Args...)) noexcept -> decltype(ptr) - { return ptr; } -}; - -template -struct QConstOverload -{ - template - constexpr auto operator()(R (T::*ptr)(Args...) const) const noexcept -> decltype(ptr) - { return ptr; } - - template - static constexpr auto of(R (T::*ptr)(Args...) const) noexcept -> decltype(ptr) - { return ptr; } -}; - -template -struct QOverload : QConstOverload, QNonConstOverload -{ - using QConstOverload::of; - using QConstOverload::operator(); - using QNonConstOverload::of; - using QNonConstOverload::operator(); - - template - constexpr auto operator()(R (*ptr)(Args...)) const noexcept -> decltype(ptr) - { return ptr; } - - template - static constexpr auto of(R (*ptr)(Args...)) noexcept -> decltype(ptr) - { return ptr; } -}; - -template constexpr inline QOverload qOverload = {}; -template constexpr inline QConstOverload qConstOverload = {}; -template constexpr inline QNonConstOverload qNonConstOverload = {}; -#endif - - class QByteArray; Q_CORE_EXPORT QByteArray qgetenv(const char *varName); // need it as two functions because QString is only forward-declared here @@ -1290,13 +1234,6 @@ Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nu "Compile your code with -fPIC (and not with -fPIE)." #endif -#define QT_VA_ARGS_CHOOSE(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N -#define QT_VA_ARGS_EXPAND(...) __VA_ARGS__ // Needed for MSVC -#define QT_VA_ARGS_COUNT(...) QT_VA_ARGS_EXPAND(QT_VA_ARGS_CHOOSE(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) -#define QT_OVERLOADED_MACRO_EXPAND(MACRO, ARGC) MACRO##_##ARGC -#define QT_OVERLOADED_MACRO_IMP(MACRO, ARGC) QT_OVERLOADED_MACRO_EXPAND(MACRO, ARGC) -#define QT_OVERLOADED_MACRO(MACRO, ...) QT_VA_ARGS_EXPAND(QT_OVERLOADED_MACRO_IMP(MACRO, QT_VA_ARGS_COUNT(__VA_ARGS__))(__VA_ARGS__)) - // This macro can be used to calculate member offsets for types with a non standard layout. // It uses the fact that offsetof() is allowed to support those types since C++17 as an optional // feature. All our compilers do support this, but some issue a warning, so we wrap the offsetof() @@ -1322,6 +1259,7 @@ QT_END_NAMESPACE #include #include #include +#include #include #include diff --git a/src/corelib/global/qoverload.h b/src/corelib/global/qoverload.h new file mode 100644 index 0000000000..f28b502c4f --- /dev/null +++ b/src/corelib/global/qoverload.h @@ -0,0 +1,75 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QOVERLOAD_H +#define QOVERLOAD_H + +#if 0 +#pragma qt_class(QOverload) +#pragma qt_sync_stop_processing +#endif + +#ifdef Q_QDOC +// Just for documentation generation +template +auto qOverload(T functionPointer); +template +auto qConstOverload(T memberFunctionPointer); +template +auto qNonConstOverload(T memberFunctionPointer); +#else +template +struct QNonConstOverload +{ + template + constexpr auto operator()(R (T::*ptr)(Args...)) const noexcept -> decltype(ptr) + { return ptr; } + + template + static constexpr auto of(R (T::*ptr)(Args...)) noexcept -> decltype(ptr) + { return ptr; } +}; + +template +struct QConstOverload +{ + template + constexpr auto operator()(R (T::*ptr)(Args...) const) const noexcept -> decltype(ptr) + { return ptr; } + + template + static constexpr auto of(R (T::*ptr)(Args...) const) noexcept -> decltype(ptr) + { return ptr; } +}; + +template +struct QOverload : QConstOverload, QNonConstOverload +{ + using QConstOverload::of; + using QConstOverload::operator(); + using QNonConstOverload::of; + using QNonConstOverload::operator(); + + template + constexpr auto operator()(R (*ptr)(Args...)) const noexcept -> decltype(ptr) + { return ptr; } + + template + static constexpr auto of(R (*ptr)(Args...)) noexcept -> decltype(ptr) + { return ptr; } +}; + +template constexpr inline QOverload qOverload = {}; +template constexpr inline QConstOverload qConstOverload = {}; +template constexpr inline QNonConstOverload qNonConstOverload = {}; + +#endif // Q_QDOC + +#define QT_VA_ARGS_CHOOSE(_1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N +#define QT_VA_ARGS_EXPAND(...) __VA_ARGS__ // Needed for MSVC +#define QT_VA_ARGS_COUNT(...) QT_VA_ARGS_EXPAND(QT_VA_ARGS_CHOOSE(__VA_ARGS__, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)) +#define QT_OVERLOADED_MACRO_EXPAND(MACRO, ARGC) MACRO##_##ARGC +#define QT_OVERLOADED_MACRO_IMP(MACRO, ARGC) QT_OVERLOADED_MACRO_EXPAND(MACRO, ARGC) +#define QT_OVERLOADED_MACRO(MACRO, ...) QT_VA_ARGS_EXPAND(QT_OVERLOADED_MACRO_IMP(MACRO, QT_VA_ARGS_COUNT(__VA_ARGS__))(__VA_ARGS__)) + +#endif /* QOVERLOAD_H */ diff --git a/src/corelib/global/qoverload.qdoc b/src/corelib/global/qoverload.qdoc new file mode 100644 index 0000000000..3e1744ea07 --- /dev/null +++ b/src/corelib/global/qoverload.qdoc @@ -0,0 +1,53 @@ +// Copyright (C) 2022 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only + +/*! \fn template auto qOverload(T functionPointer) + \relates + \since 5.7 + + Returns a pointer to an overloaded function. The template + parameter is the list of the argument types of the function. + \a functionPointer is the pointer to the (member) function: + + \snippet code/src_corelib_global_qglobal.cpp 52 + + If a member function is also const-overloaded \l qConstOverload and + \l qNonConstOverload need to be used. + + qOverload() requires C++14 enabled. In C++11-only code, the helper + classes QOverload, QConstOverload, and QNonConstOverload can be used directly: + + \snippet code/src_corelib_global_qglobal.cpp 53 + + \note Qt detects the necessary C++14 compiler support by way of the feature + test recommendations from + \l{https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations} + {C++ Committee's Standing Document 6}. + + \sa qConstOverload(), qNonConstOverload(), {Differences between String-Based + and Functor-Based Connections} +*/ + +/*! \fn template auto qConstOverload(T memberFunctionPointer) + \relates + \since 5.7 + + Returns the \a memberFunctionPointer pointer to a constant member function: + + \snippet code/src_corelib_global_qglobal.cpp 54 + + \sa qOverload, qNonConstOverload, {Differences between String-Based + and Functor-Based Connections} +*/ + +/*! \fn template auto qNonConstOverload(T memberFunctionPointer) + \relates + \since 5.7 + + Returns the \a memberFunctionPointer pointer to a non-constant member function: + + \snippet code/src_corelib_global_qglobal.cpp 54 + + \sa qOverload, qNonConstOverload, {Differences between String-Based + and Functor-Based Connections} +*/