summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2012-02-22 15:18:27 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-26 13:23:49 +0100
commit3e4a3bafb639f2ec77d44c80662157745b68f546 (patch)
treee9f2c5fc8adfbc6eaf99e146bf2711f8bb9bb12a
parent47525e689694df0eec0a2f03e8bf67eed91da295 (diff)
QFixed{,Size}: reformulate some functions in a constexpr-friendly way
The main change is the addition of a new constructor that passes its argument into 'val' verbatim. In order to disambiguate it from the existing QFixed(int) constructor, it takes a second 'int' argument. This is too ugly for public API, so it's private, and only used by static QFixed fromFixed(int), which is the existing named constructor with the same semantics. The rest of the changes simply reformulate their operations in terms of fromFixed(). This makes them ready to be constexpr'ed. Change-Id: I2a3813d62bd4124064755de6b00526a60fc82c1d Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
-rw-r--r--src/gui/painting/qfixed_p.h47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/gui/painting/qfixed_p.h b/src/gui/painting/qfixed_p.h
index d30328b0fa..95fb6893b3 100644
--- a/src/gui/painting/qfixed_p.h
+++ b/src/gui/painting/qfixed_p.h
@@ -60,6 +60,8 @@
QT_BEGIN_NAMESPACE
struct QFixed {
+private:
+ QFixed(int val, int) : val(val) {} // 2nd int is just a dummy for disambiguation
public:
QFixed() : val(0) {}
QFixed(int i) : val(i<<6) {}
@@ -67,8 +69,8 @@ public:
QFixed &operator=(int i) { val = (i<<6); return *this; }
QFixed &operator=(long i) { val = (i<<6); return *this; }
- static QFixed fromReal(qreal r) { QFixed f; f.val = (int)(r*qreal(64)); return f; }
- static QFixed fromFixed(int fixed) { QFixed f; f.val = fixed; return f; }
+ static QFixed fromReal(qreal r) { return fromFixed((int)(r*qreal(64))); }
+ static QFixed fromFixed(int fixed) { return QFixed(fixed,0); } // uses private ctor
inline int value() const { return val; }
inline void setValue(int value) { val = value; }
@@ -77,23 +79,23 @@ public:
inline qreal toReal() const { return ((qreal)val)/(qreal)64; }
inline int truncate() const { return val>>6; }
- inline QFixed round() const { QFixed f; f.val = ((val)+32) & -64; return f; }
- inline QFixed floor() const { QFixed f; f.val = (val) & -64; return f; }
- inline QFixed ceil() const { QFixed f; f.val = (val+63) & -64; return f; }
+ inline QFixed round() const { return fromFixed(((val)+32) & -64); }
+ inline QFixed floor() const { return fromFixed((val) & -64); }
+ inline QFixed ceil() const { return fromFixed((val+63) & -64); }
- inline QFixed operator+(int i) const { QFixed f; f.val = (val + (i<<6)); return f; }
- inline QFixed operator+(uint i) const { QFixed f; f.val = (val + (i<<6)); return f; }
- inline QFixed operator+(const QFixed &other) const { QFixed f; f.val = (val + other.val); return f; }
+ inline QFixed operator+(int i) const { return fromFixed((val + (i<<6))); }
+ inline QFixed operator+(uint i) const { return fromFixed((val + (i<<6))); }
+ inline QFixed operator+(const QFixed &other) const { return fromFixed((val + other.val)); }
inline QFixed &operator+=(int i) { val += (i<<6); return *this; }
inline QFixed &operator+=(uint i) { val += (i<<6); return *this; }
inline QFixed &operator+=(const QFixed &other) { val += other.val; return *this; }
- inline QFixed operator-(int i) const { QFixed f; f.val = (val - (i<<6)); return f; }
- inline QFixed operator-(uint i) const { QFixed f; f.val = (val - (i<<6)); return f; }
- inline QFixed operator-(const QFixed &other) const { QFixed f; f.val = (val - other.val); return f; }
+ inline QFixed operator-(int i) const { return fromFixed((val - (i<<6))); }
+ inline QFixed operator-(uint i) const { return fromFixed((val - (i<<6))); }
+ inline QFixed operator-(const QFixed &other) const { return fromFixed((val - other.val)); }
inline QFixed &operator-=(int i) { val -= (i<<6); return *this; }
inline QFixed &operator-=(uint i) { val -= (i<<6); return *this; }
inline QFixed &operator-=(const QFixed &other) { val -= other.val; return *this; }
- inline QFixed operator-() const { QFixed f; f.val = -val; return f; }
+ inline QFixed operator-() const { return fromFixed(-val); }
inline bool operator==(const QFixed &other) const { return val == other.val; }
inline bool operator!=(const QFixed &other) const { return val != other.val; }
@@ -120,7 +122,7 @@ public:
}
return *this;
}
- inline QFixed operator/(int d) const { QFixed f; f.val = val/d; return f; }
+ inline QFixed operator/(int d) const { return fromFixed(val/d); }
inline QFixed operator/(QFixed b) const { QFixed f = *this; return (f /= b); }
inline QFixed operator>>(int d) const { QFixed f = *this; f.val >>= d; return f; }
inline QFixed &operator*=(int i) { val *= i; return *this; }
@@ -136,21 +138,21 @@ public:
val = neg ? -res : res;
return *this;
}
- inline QFixed operator*(int i) const { QFixed f = *this; return (f *= i); }
- inline QFixed operator*(uint i) const { QFixed f = *this; return (f *= i); }
+ inline QFixed operator*(int i) const { return fromFixed(val * i); }
+ inline QFixed operator*(uint i) const { return fromFixed(val * i); }
inline QFixed operator*(const QFixed &o) const { QFixed f = *this; return (f *= o); }
private:
QFixed(qreal i) : val((int)(i*qreal(64))) {}
QFixed &operator=(qreal i) { val = (int)(i*qreal(64)); return *this; }
- inline QFixed operator+(qreal i) const { QFixed f; f.val = (val + (int)(i*qreal(64))); return f; }
+ inline QFixed operator+(qreal i) const { return fromFixed((val + (int)(i*qreal(64)))); }
inline QFixed &operator+=(qreal i) { val += (int)(i*64); return *this; }
- inline QFixed operator-(qreal i) const { QFixed f; f.val = (val - (int)(i*qreal(64))); return f; }
+ inline QFixed operator-(qreal i) const { return fromFixed((val - (int)(i*qreal(64)))); }
inline QFixed &operator-=(qreal i) { val -= (int)(i*64); return *this; }
inline QFixed &operator/=(qreal r) { val = (int)(val/r); return *this; }
- inline QFixed operator/(qreal d) const { QFixed f; f.val = (int)(val/d); return f; }
+ inline QFixed operator/(qreal d) const { return fromFixed((int)(val/d)); }
inline QFixed &operator*=(qreal d) { val = (int) (val*d); return *this; }
- inline QFixed operator*(qreal d) const { QFixed f = *this; return (f *= d); }
+ inline QFixed operator*(qreal d) const { return fromFixed((int) (val*d)); }
int val;
};
Q_DECLARE_TYPEINFO(QFixed, Q_PRIMITIVE_TYPE);
@@ -206,12 +208,11 @@ inline QFixedPoint operator+(const QFixedPoint &p1, const QFixedPoint &p2)
struct QFixedSize {
QFixed width;
QFixed height;
+ QFixedSize() {}
+ QFixedSize(QFixed _width, QFixed _height) : width(_width), height(_height) {}
QSizeF toSizeF() const { return QSizeF(width.toReal(), height.toReal()); }
static QFixedSize fromSizeF(const QSizeF &s) {
- QFixedSize size;
- size.width = QFixed::fromReal(s.width());
- size.height = QFixed::fromReal(s.height());
- return size;
+ return QFixedSize(QFixed::fromReal(s.width()), QFixed::fromReal(s.height()));
}
};
Q_DECLARE_TYPEINFO(QFixedSize, Q_PRIMITIVE_TYPE);