From 7b84defd5625100532d5b55d9d35f0b8f99d4071 Mon Sep 17 00:00:00 2001 From: Klaas Freitag Date: Wed, 26 Mar 2014 18:00:02 +0100 Subject: [PATCH] Add a stopwatch utility class with lap times. Allows to meassure the duration of something that started at a point of time, with some small convenience methods. --- src/mirall/utility.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++ src/mirall/utility.h | 18 ++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/src/mirall/utility.cpp b/src/mirall/utility.cpp index bafb424aa..6da0f7abe 100644 --- a/src/mirall/utility.cpp +++ b/src/mirall/utility.cpp @@ -490,4 +490,58 @@ bool Utility::isLinux() #endif } +# define STOPWATCH_END_TAG QLatin1String("_STOPWATCH_END") + +void Utility::StopWatch::start() +{ + _startTime = QDateTime::currentDateTime(); + _timer.start(); +} + +void Utility::StopWatch::stop() +{ + addLapTime(QLatin1String(STOPWATCH_END_TAG)); + _timer.invalidate(); +} + +quint64 Utility::StopWatch::addLapTime( const QString& lapName ) +{ + if( !_timer.isValid() ) { + start(); + } + quint64 re = _timer.elapsed(); + QPair p(lapName, re); + _lapTimes.append(p); + return re; +} + +QDateTime Utility::StopWatch::startTime() +{ + return _startTime; +} + +QDateTime Utility::StopWatch::timeOfLap( const QString& lapName ) +{ + quint64 t = durationOfLap(lapName); + if( t ) { + QDateTime re(_startTime); + return re.addMSecs(t); + } + + return QDateTime(); +} + +quint64 Utility::StopWatch::durationOfLap( const QString& lapName ) +{ + QPair lapPair; + + foreach( lapPair, _lapTimes ) { + if( lapPair.first == lapName ) { + return lapPair.second; + } + } + return 0; +} + + } // namespace Mirall diff --git a/src/mirall/utility.h b/src/mirall/utility.h index 41760cdb4..904d2b87e 100644 --- a/src/mirall/utility.h +++ b/src/mirall/utility.h @@ -18,6 +18,8 @@ #include #include #include +#include +#include class QWidget; @@ -65,6 +67,22 @@ namespace Utility bool isMac(); bool isUnix(); bool isLinux(); // use with care + + class StopWatch { + private: + QList > _lapTimes; + QDateTime _startTime; + QElapsedTimer _timer; + public: + void start(); + void stop(); + quint64 addLapTime( const QString& lapName ); + + // out helpers, return the masured times. + QDateTime startTime(); + QDateTime timeOfLap( const QString& lapName ); + quint64 durationOfLap( const QString& lapName ); + }; } }