diff options
author | Thierry Bastian <thierry.bastian@nokia.com> | 2009-08-14 11:01:37 (GMT) |
---|---|---|
committer | Thierry Bastian <thierry.bastian@nokia.com> | 2009-08-14 11:03:51 (GMT) |
commit | b770651f19741907cd415ea9ad6e087cb32cc948 (patch) | |
tree | ecc163e8b33af66454b84ca7f9159d11d6f0cd91 /src/corelib/kernel | |
parent | 51ce8a80acebc67ef09d506d89a2ee1972377877 (diff) | |
download | Qt-b770651f19741907cd415ea9ad6e087cb32cc948.zip Qt-b770651f19741907cd415ea9ad6e087cb32cc948.tar.gz Qt-b770651f19741907cd415ea9ad6e087cb32cc948.tar.bz2 |
QVariant: added toFloat and toReal
Made better use of qreal all over the place. We were previously
using QVariant::toDouble a lot. That is triggering unnecessary
conversions between float and double on embedded.
Reviewed-by: ogoffart
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qvariant.cpp | 57 | ||||
-rw-r--r-- | src/corelib/kernel/qvariant.h | 2 |
2 files changed, 48 insertions, 11 deletions
diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index b26cfdd..3c430eb 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -936,10 +936,10 @@ static bool convert(const QVariant::Private *d, QVariant::Type t, void *result, float *f = static_cast<float *>(result); switch (d->type) { case QVariant::String: - *f = float(v_cast<QString>(d)->toDouble(ok)); + *f = v_cast<QString>(d)->toFloat(ok); break; case QVariant::ByteArray: - *f = float(v_cast<QByteArray>(d)->toDouble(ok)); + *f = v_cast<QByteArray>(d)->toFloat(ok); break; case QVariant::Bool: *f = float(d->data.b); @@ -1054,7 +1054,7 @@ static void streamDebug(QDebug dbg, const QVariant &v) dbg.nospace() << v.toULongLong(); break; case QMetaType::Float: - dbg.nospace() << qVariantValue<float>(v); + dbg.nospace() << v.toFloat(); break; case QMetaType::QObjectStar: dbg.nospace() << qVariantValue<QObject *>(v); @@ -2331,16 +2331,17 @@ QBitArray QVariant::toBitArray() const } template <typename T> -inline T qNumVariantToHelper(const QVariant::Private &d, QVariant::Type t, +inline T qNumVariantToHelper(const QVariant::Private &d, const QVariant::Handler *handler, bool *ok, const T& val) { + uint t = qMetaTypeId<T>(); if (ok) *ok = true; if (d.type == t) return val; T ret; - if (!handler->convert(&d, t, &ret, ok) && ok) + if (!handler->convert(&d, QVariant::Type(t), &ret, ok) && ok) *ok = false; return ret; } @@ -2362,7 +2363,7 @@ inline T qNumVariantToHelper(const QVariant::Private &d, QVariant::Type t, */ int QVariant::toInt(bool *ok) const { - return qNumVariantToHelper<int>(d, Int, handler, ok, d.data.i); + return qNumVariantToHelper<int>(d, handler, ok, d.data.i); } /*! @@ -2382,7 +2383,7 @@ int QVariant::toInt(bool *ok) const */ uint QVariant::toUInt(bool *ok) const { - return qNumVariantToHelper<uint>(d, UInt, handler, ok, d.data.u); + return qNumVariantToHelper<uint>(d, handler, ok, d.data.u); } /*! @@ -2397,7 +2398,7 @@ uint QVariant::toUInt(bool *ok) const */ qlonglong QVariant::toLongLong(bool *ok) const { - return qNumVariantToHelper<qlonglong>(d, LongLong, handler, ok, d.data.ll); + return qNumVariantToHelper<qlonglong>(d, handler, ok, d.data.ll); } /*! @@ -2413,7 +2414,7 @@ qlonglong QVariant::toLongLong(bool *ok) const */ qulonglong QVariant::toULongLong(bool *ok) const { - return qNumVariantToHelper<qulonglong>(d, ULongLong, handler, ok, d.data.ull); + return qNumVariantToHelper<qulonglong>(d, handler, ok, d.data.ull); } /*! @@ -2440,7 +2441,7 @@ bool QVariant::toBool() const /*! Returns the variant as a double if the variant has type() \l - Double, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l + Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l UInt, or \l ULongLong; otherwise returns 0.0. If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be @@ -2450,7 +2451,41 @@ bool QVariant::toBool() const */ double QVariant::toDouble(bool *ok) const { - return qNumVariantToHelper<double>(d, Double, handler, ok, d.data.d); + return qNumVariantToHelper<double>(d, handler, ok, d.data.d); +} + +/*! + Returns the variant as a float if the variant has type() \l + Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l + UInt, or \l ULongLong; otherwise returns 0.0. + + \since 4.6 + + If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be + converted to a double; otherwise \c{*}\a{ok} is set to false. + + \sa canConvert(), convert() +*/ +float QVariant::toFloat(bool *ok) const +{ + return qNumVariantToHelper<float>(d, handler, ok, d.data.d); +} + +/*! + Returns the variant as a qreal if the variant has type() \l + Double, \l QMetaType::Float, \l Bool, \l ByteArray, \l Int, \l LongLong, \l String, \l + UInt, or \l ULongLong; otherwise returns 0.0. + + \since 4.6 + + If \a ok is non-null: \c{*}\a{ok} is set to true if the value could be + converted to a double; otherwise \c{*}\a{ok} is set to false. + + \sa canConvert(), convert() +*/ +qreal QVariant::toReal(bool *ok) const +{ + return qNumVariantToHelper<qreal>(d, handler, ok, d.data.d); } /*! diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 569355a..79bd5b8 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -250,6 +250,8 @@ class Q_CORE_EXPORT QVariant qulonglong toULongLong(bool *ok = 0) const; bool toBool() const; double toDouble(bool *ok = 0) const; + float toFloat(bool *ok = 0) const; + qreal toReal(bool *ok = 0) const; QByteArray toByteArray() const; QBitArray toBitArray() const; QString toString() const; |