BoosterSeat  0.1
A C++ library that includes common utilities that are used in other projects.
bst::Stopwatch Class Reference

A simple stopwatch class. Basic start (resume), stop (pause), and reset functionality. More...

#include <stopwatch.hpp>

Inheritance diagram for bst::Stopwatch:
Collaboration diagram for bst::Stopwatch:

Public Types

enum class  Scale { SECONDS , MILLISECONDS , MICROSECONDS }
 The resolution/scale of the time returned by elapsed() More...
 

Public Member Functions

 Stopwatch ()=default
 Construct a new Stopwatch object. The stopwatch is stopped. More...
 
 ~Stopwatch ()=default
 Default destructor. More...
 
int64_t elapsedMicroseconds () const
 Get the elapsed time in microseconds. More...
 
double elapsedMicrosecondsF () const
 Get the elapsed time in microseconds in a floating point format. More...
 
int64_t elapsedMilliseconds () const
 Get the elapsed time in milliseconds. More...
 
double elapsedMillisecondsF () const
 Get the elapsed time in milliseconds in a floating point format. More...
 
int64_t elapsedSeconds () const
 Get the elapsed time in seconds. More...
 
double elapsedSecondsF () const
 Get the elapsed time in seconds in a floating point format. More...
 
bool isRunning () const
 Returns the current state of the stopwatch. More...
 
void pause ()
 alias for stop() as it does what you want it to do. More...
 
void reset (bool stop=true)
 Resets the elapsed time. The stopwatch will keep running by default. More...
 
void resume ()
 alias for start() as it does what you want it to do. More...
 
void start ()
 Start the stopwatch. More...
 
void stop ()
 Stops/Pauses the stopwatch. Does not reset the elapsed time. More...
 

Protected Types

enum class  State { RUNNING , STOPPED }
 The state of the stopwatch. More...
 

Protected Member Functions

bst::clck::Duration getElapsed () const
 Handles the timekeeping. More...
 

Private Attributes

clck::Clock clock_
 
clck::Duration elapsed_ = clck::Duration::zero()
 
clck::TimePoint start_ = clck::TimePoint::min()
 
State state_ = State::STOPPED
 
clck::TimePoint stop_time_ = clck::TimePoint::min()
 

Detailed Description

A simple stopwatch class. Basic start (resume), stop (pause), and reset functionality.

Member Enumeration Documentation

◆ Scale

enum bst::Stopwatch::Scale
strong

The resolution/scale of the time returned by elapsed()

Enumerator
SECONDS 
MILLISECONDS 
MICROSECONDS 

◆ State

enum bst::IStopwatch::State
strongprotectedinherited

The state of the stopwatch.

Enumerator
RUNNING 
STOPPED 

Constructor & Destructor Documentation

◆ Stopwatch()

bst::Stopwatch::Stopwatch ( )
default

Construct a new Stopwatch object. The stopwatch is stopped.

◆ ~Stopwatch()

bst::Stopwatch::~Stopwatch ( )
default

Default destructor.

Member Function Documentation

◆ start()

void bst::Stopwatch::start ( )
inlinevirtual

Start the stopwatch.

Exceptions
BoosterSeatExceptionif the stopwatch is already running.

Implements bst::IStopwatch.

References clock_, bst::IStopwatch::RUNNING, start_, state_, and bst::IStopwatch::STOPPED.

Referenced by resume(), and TEST().

Here is the caller graph for this function:

◆ stop()

void bst::Stopwatch::stop ( )
inlinevirtual

Stops/Pauses the stopwatch. Does not reset the elapsed time.

Implements bst::IStopwatch.

References clock_, elapsed_, bst::IStopwatch::RUNNING, start_, state_, stop_time_, and bst::IStopwatch::STOPPED.

Referenced by pause(), reset(), and TEST().

Here is the caller graph for this function:

◆ reset()

void bst::Stopwatch::reset ( bool  stop = true)
inlinevirtual

Resets the elapsed time. The stopwatch will keep running by default.

Parameters
stopIf true, the stopwatch will be stopped.

Implements bst::IStopwatch.

References clock_, elapsed_, start_, state_, stop(), and bst::IStopwatch::STOPPED.

Referenced by TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ isRunning()

bool bst::Stopwatch::isRunning ( ) const
inlinevirtual

Returns the current state of the stopwatch.

Implements bst::IStopwatch.

References bst::IStopwatch::RUNNING, and state_.

Referenced by TEST().

Here is the caller graph for this function:

◆ elapsedMicroseconds()

int64_t bst::Stopwatch::elapsedMicroseconds ( ) const
inlinevirtual

Get the elapsed time in microseconds.

Implements bst::IStopwatch.

References getElapsed().

Referenced by TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elapsedMicrosecondsF()

double bst::Stopwatch::elapsedMicrosecondsF ( ) const
inlinevirtual

Get the elapsed time in microseconds in a floating point format.

Implements bst::IStopwatch.

References getElapsed().

Referenced by TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elapsedMilliseconds()

int64_t bst::Stopwatch::elapsedMilliseconds ( ) const
inlinevirtual

Get the elapsed time in milliseconds.

Implements bst::IStopwatch.

References getElapsed().

Here is the call graph for this function:

◆ elapsedMillisecondsF()

double bst::Stopwatch::elapsedMillisecondsF ( ) const
inlinevirtual

Get the elapsed time in milliseconds in a floating point format.

Implements bst::IStopwatch.

References getElapsed().

Referenced by TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ elapsedSeconds()

int64_t bst::Stopwatch::elapsedSeconds ( ) const
inlinevirtual

Get the elapsed time in seconds.

Implements bst::IStopwatch.

References getElapsed().

Here is the call graph for this function:

◆ elapsedSecondsF()

double bst::Stopwatch::elapsedSecondsF ( ) const
inlinevirtual

Get the elapsed time in seconds in a floating point format.

Implements bst::IStopwatch.

References getElapsed().

Referenced by TEST().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pause()

void bst::Stopwatch::pause ( )
inlinevirtual

alias for stop() as it does what you want it to do.

Implements bst::IStopwatch.

References stop().

Here is the call graph for this function:

◆ resume()

void bst::Stopwatch::resume ( )
inlinevirtual

alias for start() as it does what you want it to do.

Implements bst::IStopwatch.

References start().

Here is the call graph for this function:

◆ getElapsed()

bst::clck::Duration bst::Stopwatch::getElapsed ( ) const
inlineprotected

Handles the timekeeping.

Returns
clck::Duration - The elapsed time

References clock_, elapsed_, bst::IStopwatch::RUNNING, start_, and state_.

Referenced by elapsedMicroseconds(), elapsedMicrosecondsF(), elapsedMilliseconds(), elapsedMillisecondsF(), elapsedSeconds(), and elapsedSecondsF().

Here is the caller graph for this function:

Member Data Documentation

◆ clock_

clck::Clock bst::Stopwatch::clock_
private

Referenced by getElapsed(), reset(), start(), and stop().

◆ start_

clck::TimePoint bst::Stopwatch::start_ = clck::TimePoint::min()
private

Referenced by getElapsed(), reset(), start(), and stop().

◆ stop_time_

clck::TimePoint bst::Stopwatch::stop_time_ = clck::TimePoint::min()
private

Referenced by stop().

◆ elapsed_

clck::Duration bst::Stopwatch::elapsed_ = clck::Duration::zero()
private

Referenced by getElapsed(), reset(), and stop().

◆ state_

State bst::Stopwatch::state_ = State::STOPPED
private

Referenced by getElapsed(), isRunning(), reset(), start(), and stop().