From ce3402b5efc929b99d6ecb27f47e671cdcfae393 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 19 Jan 2015 11:29:52 +0100 Subject: QSizePolicy: add some constexpr Also add a basic test for constexpr expressions involving QSizePolicy. GCC < 4.8.0 has problems with initializing variant members in constexpr ctors: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54922 Instead of revoking its Q_COMPILER_CONSTEXPR, which is a source-incompatible change, simply note the fact so we can selectively provide constexpr for QSizePolicy. Change-Id: Iba8e5a7cdf847d73e8e2b6bb6211fb3c9846aa0a Reviewed-by: Thiago Macieira --- doc/global/qt-cpp-defines.qdocconf | 1 + src/widgets/kernel/qsizepolicy.h | 38 ++++++++++++++-------- .../widgets/kernel/qsizepolicy/qsizepolicy.pro | 1 + .../widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp | 14 ++++++++ 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/doc/global/qt-cpp-defines.qdocconf b/doc/global/qt-cpp-defines.qdocconf index 8ac5b70bbb..fc3f78b925 100644 --- a/doc/global/qt-cpp-defines.qdocconf +++ b/doc/global/qt-cpp-defines.qdocconf @@ -171,6 +171,7 @@ Cpp.ignoretokens += \ QT_END_NAMESPACE \ QT_FASTCALL \ QT_MUTEX_LOCK_NOEXCEPT \ + QT_SIZEPOLICY_CONSTEXPR \ QT_WARNING_DISABLE_DEPRECATED \ QT_WARNING_PUSH \ QT_WARNING_POP \ diff --git a/src/widgets/kernel/qsizepolicy.h b/src/widgets/kernel/qsizepolicy.h index 3934c0f1d0..a0098cfd74 100644 --- a/src/widgets/kernel/qsizepolicy.h +++ b/src/widgets/kernel/qsizepolicy.h @@ -45,6 +45,15 @@ QT_BEGIN_NAMESPACE +// gcc < 4.8.0 has problems with init'ing variant members in constexpr ctors +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54922 +#if !defined(Q_CC_GNU) || defined(Q_CC_INTEL) || defined(Q_CC_CLANG) || Q_CC_GNU >= 408 +# define QT_SIZEPOLICY_CONSTEXPR Q_DECL_CONSTEXPR +#endif + +#ifndef QT_SIZEPOLICY_CONSTEXPR +# define QT_SIZEPOLICY_CONSTEXPR +#endif class QVariant; class QSizePolicy; @@ -94,7 +103,7 @@ public: Q_DECLARE_FLAGS(ControlTypes, ControlType) Q_FLAG(ControlTypes) - QSizePolicy() : data(0) { } + QT_SIZEPOLICY_CONSTEXPR QSizePolicy() : data(0) { } QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType) : data(0) { @@ -102,48 +111,47 @@ public: bits.verPolicy = vertical; setControlType(type); } - Policy horizontalPolicy() const { return static_cast(bits.horPolicy); } - Policy verticalPolicy() const { return static_cast(bits.verPolicy); } + QT_SIZEPOLICY_CONSTEXPR Policy horizontalPolicy() const { return static_cast(bits.horPolicy); } + QT_SIZEPOLICY_CONSTEXPR Policy verticalPolicy() const { return static_cast(bits.verPolicy); } ControlType controlType() const; void setHorizontalPolicy(Policy d) { bits.horPolicy = d; } void setVerticalPolicy(Policy d) { bits.verPolicy = d; } void setControlType(ControlType type); - Qt::Orientations expandingDirections() const { + QT_SIZEPOLICY_CONSTEXPR Qt::Orientations expandingDirections() const { return ( (verticalPolicy() & ExpandFlag) ? Qt::Vertical : Qt::Orientations() ) | ( (horizontalPolicy() & ExpandFlag) ? Qt::Horizontal : Qt::Orientations() ) ; } - void setHeightForWidth(bool b) { bits.hfw = b; } - bool hasHeightForWidth() const { return bits.hfw; } + void setHeightForWidth(bool b) { bits.hfw = b; } + QT_SIZEPOLICY_CONSTEXPR bool hasHeightForWidth() const { return bits.hfw; } void setWidthForHeight(bool b) { bits.wfh = b; } - bool hasWidthForHeight() const { return bits.wfh; } + QT_SIZEPOLICY_CONSTEXPR bool hasWidthForHeight() const { return bits.wfh; } - bool operator==(const QSizePolicy& s) const { return data == s.data; } - bool operator!=(const QSizePolicy& s) const { return data != s.data; } + QT_SIZEPOLICY_CONSTEXPR bool operator==(const QSizePolicy& s) const { return data == s.data; } + QT_SIZEPOLICY_CONSTEXPR bool operator!=(const QSizePolicy& s) const { return data != s.data; } friend Q_DECL_CONST_FUNCTION uint qHash(QSizePolicy key, uint seed) Q_DECL_NOTHROW { return qHash(key.data, seed); } operator QVariant() const; - int horizontalStretch() const { return static_cast(bits.horStretch); } - int verticalStretch() const { return static_cast(bits.verStretch); } + QT_SIZEPOLICY_CONSTEXPR int horizontalStretch() const { return static_cast(bits.horStretch); } + QT_SIZEPOLICY_CONSTEXPR int verticalStretch() const { return static_cast(bits.verStretch); } void setHorizontalStretch(int stretchFactor) { bits.horStretch = static_cast(qBound(0, stretchFactor, 255)); } void setVerticalStretch(int stretchFactor) { bits.verStretch = static_cast(qBound(0, stretchFactor, 255)); } - bool retainSizeWhenHidden() const { return bits.retainSizeWhenHidden; } + QT_SIZEPOLICY_CONSTEXPR bool retainSizeWhenHidden() const { return bits.retainSizeWhenHidden; } void setRetainSizeWhenHidden(bool retainSize) { bits.retainSizeWhenHidden = retainSize; } void transpose(); - private: #ifndef QT_NO_DATASTREAM friend Q_WIDGETS_EXPORT QDataStream &operator<<(QDataStream &, const QSizePolicy &); friend Q_WIDGETS_EXPORT QDataStream &operator>>(QDataStream &, QSizePolicy &); #endif - QSizePolicy(int i) : data(i) { } + QT_SIZEPOLICY_CONSTEXPR QSizePolicy(int i) : data(i) { } struct Bits { quint32 horStretch : 8; @@ -187,6 +195,8 @@ inline void QSizePolicy::transpose() { setVerticalStretch(hStretch); } +#undef QT_SIZEPOLICY_CONSTEXPR + QT_END_NAMESPACE #endif // QSIZEPOLICY_H diff --git a/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro b/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro index 84629c7c0a..d325bc4aeb 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro +++ b/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro @@ -1,4 +1,5 @@ CONFIG += testcase +contains(QT_CONFIG, c++14): CONFIG += c++14 TARGET = tst_qsizepolicy QT += widgets widgets-private testlib diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index cd5aa03689..15bd0a8c1c 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -41,6 +41,7 @@ class tst_QSizePolicy : public QObject private Q_SLOTS: void cleanup() { QVERIFY(QApplication::topLevelWidgets().isEmpty()); } void qtest(); + void constExpr(); void defaultValues(); void getSetCheck_data() { data(); } void getSetCheck(); @@ -102,6 +103,19 @@ void tst_QSizePolicy::qtest() #undef CHECK2 } +void tst_QSizePolicy::constExpr() +{ +/* gcc < 4.8.0 has problems with init'ing variant members in constexpr ctors */ +/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54922 */ +#if !defined(Q_CC_GNU) || defined(Q_CC_INTEL) || defined(Q_CC_CLANG) || Q_CC_GNU >= 408 + // check that certain ctors are constexpr (compile-only): + { Q_CONSTEXPR QSizePolicy sp; Q_UNUSED(sp); } + { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(); Q_UNUSED(sp); } +#else + QSKIP("QSizePolicy cannot be constexpr with this version of the compiler."); +#endif +} + void tst_QSizePolicy::defaultValues() { { -- cgit v1.2.3