From 3006bd2d44298babd1339c3fc3a67ad0696bcefc Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Thu, 9 May 2013 11:42:05 +0200 Subject: Make QElapsedTimer default to invalid (and now non-POD). The practical uses of a POD QElapsedTimer are not really that clear, and the number of misuses of this API are quite high. Default the state to invalid to prevent against mistakes. [ChangeLog][QtCore][QElapsedTimer] Is no longer a POD. Change-Id: I267292acf2bfca7404e3e449dd04410441d7ce26 Reviewed-by: Lars Knoll --- src/corelib/tools/qelapsedtimer.cpp | 15 +++++++++++++-- src/corelib/tools/qelapsedtimer.h | 7 +++++++ src/corelib/tools/qelapsedtimer_generic.cpp | 2 ++ .../corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp | 10 ++-------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qelapsedtimer.cpp b/src/corelib/tools/qelapsedtimer.cpp index 1da85fce96..08fc1cf5e2 100644 --- a/src/corelib/tools/qelapsedtimer.cpp +++ b/src/corelib/tools/qelapsedtimer.cpp @@ -201,6 +201,17 @@ QT_BEGIN_NAMESPACE \sa clockType(), isMonotonic() */ +/*! + \fn QElapsedTimer::QElapsedTimer() + \since 5.4 + + Constructs an invalid QElapsedTimer. A timer becomes valid once it has been + started. + + \sa isValid(), start() +*/ + + /*! \fn bool QElapsedTimer::operator ==(const QElapsedTimer &other) const @@ -230,8 +241,8 @@ void QElapsedTimer::invalidate() Q_DECL_NOTHROW } /*! - Returns \c false if this object was invalidated by a call to invalidate() and - has not been restarted since. + Returns \c false if the timer has never been started or invalidated by a + call to invalidate(). \sa invalidate(), start(), restart() */ diff --git a/src/corelib/tools/qelapsedtimer.h b/src/corelib/tools/qelapsedtimer.h index b06afe4ab4..7df5dec63a 100644 --- a/src/corelib/tools/qelapsedtimer.h +++ b/src/corelib/tools/qelapsedtimer.h @@ -57,6 +57,13 @@ public: MachAbsoluteTime, PerformanceCounter }; + + Q_DECL_CONSTEXPR QElapsedTimer() + : t1(Q_INT64_C(0x8000000000000000)) + , t2(Q_INT64_C(0x8000000000000000)) + { + } + static ClockType clockType() Q_DECL_NOTHROW; static bool isMonotonic() Q_DECL_NOTHROW; diff --git a/src/corelib/tools/qelapsedtimer_generic.cpp b/src/corelib/tools/qelapsedtimer_generic.cpp index 6324be00c0..7a52faa3c5 100644 --- a/src/corelib/tools/qelapsedtimer_generic.cpp +++ b/src/corelib/tools/qelapsedtimer_generic.cpp @@ -87,6 +87,8 @@ void QElapsedTimer::start() Q_DECL_NOTHROW and then starting the timer again with start(), but it does so in one single operation, avoiding the need to obtain the clock value twice. + Restarting the timer makes it valid again. + The following example illustrates how to use this function to calibrate a parameter to a slow operation (for example, an iteration count) so that this operation takes at least 250 milliseconds: diff --git a/tests/auto/corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp index f2852f6a50..48e3745be4 100644 --- a/tests/auto/corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp +++ b/tests/auto/corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp @@ -48,12 +48,7 @@ static const int minResolution = 50; // the minimum resolution for the tests QDebug operator<<(QDebug s, const QElapsedTimer &t) { - union { - QElapsedTimer t; - struct { qint64 t1, t2; } i; - } copy; - copy.t = t; - s.nospace() << "(" << copy.i.t1 << ", " << copy.i.t2 << ")"; + s.nospace() << "(" << t.msecsSinceReference() << ")"; return s.space(); } @@ -81,8 +76,7 @@ void tst_QElapsedTimer::validity() { QElapsedTimer t; - t.invalidate(); - QVERIFY(!t.isValid()); + QVERIFY(!t.isValid()); // non-POD now, it should always start invalid t.start(); QVERIFY(t.isValid()); -- cgit v1.2.3