summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Burchell <robin+qt@viroteck.net>2013-05-09 11:42:05 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-09-03 13:19:35 +0200
commit3006bd2d44298babd1339c3fc3a67ad0696bcefc (patch)
tree985bec0889f7d2dfbcde59848aeac9e0ea42df2a
parent8d0e6000cb326130605b2b5e688e9cdc65950794 (diff)
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 <lars.knoll@digia.com>
-rw-r--r--src/corelib/tools/qelapsedtimer.cpp15
-rw-r--r--src/corelib/tools/qelapsedtimer.h7
-rw-r--r--src/corelib/tools/qelapsedtimer_generic.cpp2
-rw-r--r--tests/auto/corelib/tools/qelapsedtimer/tst_qelapsedtimer.cpp10
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
@@ -202,6 +202,17 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \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
Returns \c true if this object and \a other contain the same time.
@@ -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());