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 <giuseppe.dangelo@kdab.com>
bb10
Marc Mutz 2014-12-20 22:13:58 +01:00
parent 3ac8b8c696
commit b952bd3605
3 changed files with 18 additions and 4 deletions

View File

@ -254,6 +254,11 @@ QSizePolicy::ControlType QSizePolicy::controlType() const
\sa QStyle::layoutSpacing()
*/
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,
@ -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;
}
}

View File

@ -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;

View File

@ -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