diff options
author | Marc Mutz <[email protected]> | 2017-02-22 09:43:10 +0100 |
---|---|---|
committer | Marc Mutz <[email protected]> | 2017-02-27 23:09:27 +0000 |
commit | 49c558eb0db3ec544fface24939a16f92d794987 (patch) | |
tree | 1f74606d4163c31507316d31ff3d23d32c44f4bf | |
parent | bc641ad9f8ba3ea65dc400aa2f6520b4a5d5434d (diff) |
QSizePolicy: inline toControlTypeFieldValue()
The implementation now uses the relaxed-constexpr
qCountTrailingZeroBits() function from QtAlgorithms, making the
QSizePolicy(Policy, Policy, ControlType) constructor constexpr on
C++14 compilers. The explicit check for DefaultType remains to keep
the constructor C++11-constexpr when called with just (Policy,
Policy).
Extend the constExpr tests a bit.
Change-Id: I59690f0921d9bdee08e3615d0d1f4a9b92870c32
Reviewed-by: Giuseppe D'Angelo <[email protected]>
-rw-r--r-- | src/widgets/kernel/qsizepolicy.cpp | 24 | ||||
-rw-r--r-- | src/widgets/kernel/qsizepolicy.h | 20 | ||||
-rw-r--r-- | tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp | 5 |
3 files changed, 23 insertions, 26 deletions
diff --git a/src/widgets/kernel/qsizepolicy.cpp b/src/widgets/kernel/qsizepolicy.cpp index 477b3421a09..b5a0cd3940c 100644 --- a/src/widgets/kernel/qsizepolicy.cpp +++ b/src/widgets/kernel/qsizepolicy.cpp @@ -258,30 +258,6 @@ void QSizePolicy::setControlType(ControlType type) Q_DECL_NOTHROW bits.ctype = toControlTypeFieldValue(type); } -quint32 QSizePolicy::toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW -{ - /* - The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10, - etc. In memory, we pack it onto the available bits (CTSize) in - setControlType(), and unpack it here. - - Example: - - 0x00000001 maps to 0 - 0x00000002 maps to 1 - 0x00000004 maps to 2 - 0x00000008 maps to 3 - etc. - */ - - int i = 0; - while (true) { - if (type & (0x1 << i)) - return i; - ++i; - } -} - /*! \fn void QSizePolicy::setHeightForWidth(bool dependent) diff --git a/src/widgets/kernel/qsizepolicy.h b/src/widgets/kernel/qsizepolicy.h index e19770cf925..07d8393c6fc 100644 --- a/src/widgets/kernel/qsizepolicy.h +++ b/src/widgets/kernel/qsizepolicy.h @@ -42,6 +42,7 @@ #include <QtWidgets/qtwidgetsglobal.h> #include <QtCore/qobject.h> +#include <QtCore/qalgorithms.h> QT_BEGIN_NAMESPACE @@ -175,7 +176,24 @@ private: struct Bits; QT_SIZEPOLICY_CONSTEXPR explicit QSizePolicy(Bits b) Q_DECL_NOTHROW : bits(b) { } - static quint32 toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW; + static Q_DECL_RELAXED_CONSTEXPR quint32 toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW + { + /* + The control type is a flag type, with values 0x1, 0x2, 0x4, 0x8, 0x10, + etc. In memory, we pack it onto the available bits (CTSize) in + setControlType(), and unpack it here. + + Example: + + 0x00000001 maps to 0 + 0x00000002 maps to 1 + 0x00000004 maps to 2 + 0x00000008 maps to 3 + etc. + */ + + return qCountTrailingZeroBits(static_cast<quint32>(type)); + } struct Bits { quint32 horStretch : 8; diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 4ae1f02ce28..d50f46cc163 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -114,7 +114,10 @@ void tst_QSizePolicy::constExpr() { Q_CONSTEXPR QSizePolicy sp; Q_UNUSED(sp); } { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(); Q_UNUSED(sp); } { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred); Q_UNUSED(sp); } - { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); Q_UNUSED(sp); } + { Q_CONSTEXPR QSizePolicy sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding, QSizePolicy::DefaultType); + Q_CONSTEXPR QSizePolicy tp = sp.transposed(); Q_UNUSED(tp); } + { Q_RELAXED_CONSTEXPR auto sp = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::CheckBox); + Q_RELAXED_CONSTEXPR auto tp = sp.transposed(); Q_UNUSED(tp); } #else QSKIP("QSizePolicy cannot be constexpr with this version of the compiler."); #endif |