2020-04-08 18:08:14 +02:00
|
|
|
/**
|
|
|
|
* @file Stopwatch.h
|
|
|
|
*
|
|
|
|
* @date 08.04.2020
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
|
|
|
|
#define FRAMEWORK_TIMEMANAGER_STOPWATCH_H_
|
|
|
|
#include <framework/timemanager/Clock.h>
|
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
enum class StopwatchDisplayMode {
|
|
|
|
MS_DOUBLE,
|
|
|
|
MS
|
|
|
|
};
|
|
|
|
|
2020-04-08 18:27:18 +02:00
|
|
|
/**
|
|
|
|
* @brief Simple Stopwatch implementation to measure elapsed time
|
|
|
|
* @details
|
|
|
|
* This class can be used to measure elapsed times. It also displays elapsed
|
|
|
|
* times automatically on destruction if not explicitely deactivated in the
|
|
|
|
* constructor. The default time format is the elapsed time in miliseconds
|
|
|
|
* as a float.
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
class Stopwatch {
|
2020-04-08 19:05:21 +02:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Default constructor. Call "Stopwatch stopwatch" without brackets if
|
|
|
|
* no parameters are required!
|
|
|
|
* @param displayOnDestruction If set to true, displays measured time on
|
|
|
|
* object destruction
|
|
|
|
* @param displayMode Display format is either MS rounded or MS as double
|
|
|
|
* format
|
|
|
|
* @param outputPrecision If using double format, specify precision here.
|
|
|
|
*/
|
2020-04-08 18:33:38 +02:00
|
|
|
Stopwatch(bool displayOnDestruction = true,
|
2020-04-08 19:05:21 +02:00
|
|
|
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE,
|
|
|
|
uint8_t outputPrecision = 4);
|
2020-04-08 18:08:14 +02:00
|
|
|
virtual~ Stopwatch();
|
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
/**
|
|
|
|
* Caches the start time
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
void start();
|
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
/**
|
|
|
|
* Calculates the elapsed time since start and returns it
|
|
|
|
* @return elapsed time in milliseconds (rounded)
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
ms_normal_t stop();
|
2020-04-08 18:33:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the elapsed time since start and returns it
|
|
|
|
* @return elapsed time in milliseconds (doulbe precision)
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
ms_double_t stopPrecise();
|
|
|
|
|
2020-04-08 18:33:38 +02:00
|
|
|
/**
|
|
|
|
* Displays the elapsed times on the osstream, depending on internal display
|
|
|
|
* mode.
|
|
|
|
*/
|
2020-04-08 18:08:14 +02:00
|
|
|
void display();
|
2020-04-08 18:33:38 +02:00
|
|
|
|
|
|
|
StopwatchDisplayMode getDisplayMode() const;
|
|
|
|
void setDisplayMode(StopwatchDisplayMode displayMode);
|
2020-04-08 18:08:14 +02:00
|
|
|
private:
|
|
|
|
timeval startTime {0, 0};
|
|
|
|
timeval elapsedTime {0, 0};
|
2020-04-08 19:05:21 +02:00
|
|
|
double elapsedTimeMsDouble {0.0};
|
2020-04-08 18:08:14 +02:00
|
|
|
|
|
|
|
bool displayOnDestruction = true;
|
2020-04-08 19:05:21 +02:00
|
|
|
uint8_t outputPrecision = 4;
|
2020-04-08 18:33:38 +02:00
|
|
|
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MS_DOUBLE;
|
2020-04-08 18:08:14 +02:00
|
|
|
|
|
|
|
void stopInternal();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */
|