summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <[email protected]>2014-12-20 22:13:58 +0100
committerMarc Mutz <[email protected]>2017-02-23 05:44:51 +0000
commitb952bd3605315465b1ceb90e0924707f781a9f9e (patch)
tree3dbe0709710b50b46085dcf8e7bd739fff90baef
parent3ac8b8c6961bb5b2ae82bbaee154282c54bd0b90 (diff)
QSizePolicy: make (Policy,Policy) ctor constexpr
Unfortunately, that ctor also takes a ControlType argument (defaulted), and calls the non-constexpr, non-inline function setControlType(). In order to make at least the two-arg version constexpr, I added a use of the ternary operator to check for type == DefaultType, making all calls of the ctor that use type == DefaultType constexpr. For init'ing an aggregate type without ctor in the ctor-init-list, I needed to require uniform initialization, too. C++11-style constexpr cannot call void functions, so I needed to extract the transformation part of setControlType() into a new function that returns the result instead of storing it directly. Saves a surprising 2K in QtWidgets text size on GCC 4.9, AMD64 Linux stripped release builds. Change-Id: Ib4adf5fd6e54d5345dbfe1c298554278faf13c58 Reviewed-by: Giuseppe D'Angelo <[email protected]>
-rw-r--r--src/widgets/kernel/qsizepolicy.cpp11
-rw-r--r--src/widgets/kernel/qsizepolicy.h9
-rw-r--r--tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp2
3 files changed, 18 insertions, 4 deletions
diff --git a/src/widgets/kernel/qsizepolicy.cpp b/src/widgets/kernel/qsizepolicy.cpp
index a03523d2afd..b08a9abb1e8 100644
--- a/src/widgets/kernel/qsizepolicy.cpp
+++ b/src/widgets/kernel/qsizepolicy.cpp
@@ -255,6 +255,11 @@ QSizePolicy::ControlType QSizePolicy::controlType() const
*/
void QSizePolicy::setControlType(ControlType type)
{
+ 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
@@ -271,10 +276,8 @@ void QSizePolicy::setControlType(ControlType type)
int i = 0;
while (true) {
- if (type & (0x1 << i)) {
- bits.ctype = i;
- return;
- }
+ if (type & (0x1 << i))
+ return i;
++i;
}
}
diff --git a/src/widgets/kernel/qsizepolicy.h b/src/widgets/kernel/qsizepolicy.h
index eceb08259eb..83ce5853ab4 100644
--- a/src/widgets/kernel/qsizepolicy.h
+++ b/src/widgets/kernel/qsizepolicy.h
@@ -119,12 +119,19 @@ public:
QT_SIZEPOLICY_CONSTEXPR QSizePolicy() : data(0) { }
+#ifdef Q_COMPILER_UNIFORM_INIT
+ QT_SIZEPOLICY_CONSTEXPR QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
+ : bits{0, 0, quint32(horizontal), quint32(vertical),
+ type == DefaultType ? 0 : toControlTypeFieldValue(type), 0, 0, 0}
+ {}
+#else
QSizePolicy(Policy horizontal, Policy vertical, ControlType type = DefaultType)
: data(0) {
bits.horPolicy = horizontal;
bits.verPolicy = vertical;
setControlType(type);
}
+#endif // uniform-init
QT_SIZEPOLICY_CONSTEXPR Policy horizontalPolicy() const { return static_cast<Policy>(bits.horPolicy); }
QT_SIZEPOLICY_CONSTEXPR Policy verticalPolicy() const { return static_cast<Policy>(bits.verPolicy); }
ControlType controlType() const;
@@ -176,6 +183,8 @@ private:
struct Bits;
QT_SIZEPOLICY_CONSTEXPR explicit QSizePolicy(Bits b) Q_DECL_NOTHROW : bits(b) { }
+ static quint32 toControlTypeFieldValue(ControlType type) Q_DECL_NOTHROW;
+
struct Bits {
quint32 horStretch : 8;
quint32 verStretch : 8;
diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp
index 98b765a6c6d..4ae1f02ce28 100644
--- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp
+++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp
@@ -113,6 +113,8 @@ void tst_QSizePolicy::constExpr()
// check that certain ctors are constexpr (compile-only):
{ 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); }
#else
QSKIP("QSizePolicy cannot be constexpr with this version of the compiler.");
#endif