2020-04-08 18:08:14 +02:00
|
|
|
#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 {
|
2020-04-08 19:24:24 +02:00
|
|
|
MILLIS,
|
|
|
|
SECONDS
|
2020-04-08 18:33:38 +02:00
|
|
|
};
|
|
|
|
|
2020-04-08 18:27:18 +02:00
|
|
|
/**
|
2020-05-29 02:23:14 +02:00
|
|
|
* @brief Simple Stopwatch implementation to measure elapsed time
|
2020-04-08 18:27:18 +02:00
|
|
|
* @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
|
2020-05-29 17:43:37 +02:00
|
|
|
* in seconds as a double.
|
2020-05-29 02:23:14 +02:00
|
|
|
* @author R. Mueller
|
2020-04-08 18:27:18 +02:00
|
|
|
*/
|
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 19:24:24 +02:00
|
|
|
Stopwatch(bool displayOnDestruction = true, StopwatchDisplayMode displayMode
|
|
|
|
= StopwatchDisplayMode::MILLIS);
|
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 19:24:24 +02:00
|
|
|
millis_t stop();
|
2020-05-29 17:43:37 +02:00
|
|
|
/**
|
|
|
|
* Calculates the elapsed time since start and returns it
|
|
|
|
* @return elapsed time in seconds (double precision)
|
|
|
|
*/
|
2020-04-08 19:24:24 +02:00
|
|
|
seconds_t stopSeconds();
|
2020-04-08 18:08:14 +02:00
|
|
|
|
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-23 14:06:48 +02:00
|
|
|
bool displayOnDestruction = true;
|
2020-04-08 18:08:14 +02:00
|
|
|
private:
|
|
|
|
timeval startTime {0, 0};
|
|
|
|
timeval elapsedTime {0, 0};
|
|
|
|
|
2020-04-08 19:24:24 +02:00
|
|
|
StopwatchDisplayMode displayMode = StopwatchDisplayMode::MILLIS;
|
2020-04-23 14:06:48 +02:00
|
|
|
|
2020-04-08 18:08:14 +02:00
|
|
|
void stopInternal();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* FRAMEWORK_TIMEMANAGER_STOPWATCH_H_ */
|