Terminus
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
terminus::Timer Class Reference

The Timer class implements a set of pauseable duration counters (subsequently called timers) and mechanisms to adjust them individually or globally. More...

#include <timer.h>

Public Types

using TimerID = unsigned int
 
using TimerMSec = signed int
 

Public Member Functions

 Timer ()
 
void pause ()
 toggle global pause state More...
 
void pause (bool flag)
 set global pause state according to flag More...
 
bool isPaused ()
 
TimerID allocateTimer ()
 allocate a new timer without naming it More...
 
TimerID allocateTimer (std::string name)
 allocate a new named timer More...
 
void releaseTimer (TimerID id)
 release ressources (id and timestamp) of an unnamed timer More...
 
void releaseTimer (std::string name)
 release ressources (name, id and timestamp) of a named timer More...
 
bool isAllocated (TimerID id)
 
bool isAllocated (std::string name)
 
TimerMSec get ()
 
TimerMSec get (TimerID id)
 
TimerMSec get (std::string name)
 
void adjust (TimerMSec newNow)
 adjust the reference time to match newNow More...
 
void adjust (TimerID id, TimerMSec newNow)
 adjust the value of an unnamed timer to match newNow More...
 
void adjust (std::string name, TimerMSec newNow)
 adjust the value of a named timer to match newNow More...
 

Protected Member Functions

TimerID freeTimerID ()
 
std::chrono::steady_clock::time_point localNow ()
 
void startPause ()
 
void endPause ()
 

Static Protected Member Functions

static TimerMSec toMSec (std::chrono::steady_clock::duration duration)
 
static std::chrono::steady_clock::duration fromMSec (TimerMSec duration)
 

Protected Attributes

std::chrono::steady_clock m_clock
 
std::chrono::steady_clock::time_point m_pauseNow
 
bool m_isPaused
 
std::chrono::steady_clock::time_point m_baseTimeStamp
 
std::map< TimerID, std::chrono::steady_clock::time_point > m_timeStamps
 
std::map< std::string, TimerIDm_namedTimers
 

Detailed Description

The Timer class implements a set of pauseable duration counters (subsequently called timers) and mechanisms to adjust them individually or globally.

Timers can be allocated with a name that allows them to be accessed in contexts where their id is not known.

Member Typedef Documentation

using terminus::Timer::TimerID = unsigned int
using terminus::Timer::TimerMSec = signed int

Constructor & Destructor Documentation

terminus::Timer::Timer ( )

Member Function Documentation

void terminus::Timer::adjust ( Timer::TimerMSec  newNow)

adjust the reference time to match newNow

Parameters
newNow- time in milliseconds

this method affects both the time returned by get() and get(id) or get(name) as named and unnamed timers operate relative to the reference time

void terminus::Timer::adjust ( Timer::TimerID  id,
Timer::TimerMSec  newNow 
)

adjust the value of an unnamed timer to match newNow

Parameters
id
newNow- time in milliseconds

does nothing for unallocated timer ids

void terminus::Timer::adjust ( std::string  name,
Timer::TimerMSec  newNow 
)

adjust the value of a named timer to match newNow

Parameters
name
newNow- time in milliseconds

allocates a new timer, if it is not allocated already

Timer::TimerID terminus::Timer::allocateTimer ( )

allocate a new timer without naming it

Returns
id of the new timer
Timer::TimerID terminus::Timer::allocateTimer ( std::string  name)

allocate a new named timer

Parameters
name
Returns
id of the new timer
void terminus::Timer::endPause ( )
protected
Timer::TimerID terminus::Timer::freeTimerID ( )
protected
std::chrono::steady_clock::duration terminus::Timer::fromMSec ( Timer::TimerMSec  duration)
staticprotected
Timer::TimerMSec terminus::Timer::get ( )
Returns
reference time in milliseconds

the reference time is the time passed since instantiation of this timer object

See also
Timer::adjust()
Timer::TimerMSec terminus::Timer::get ( TimerID  id)
Parameters
id
Returns
time in milliseconds passed since allocation of an unnamed timer

returns 0 msec for unallocated timer ids

See also
Timer::adjust()
Timer::TimerMSec terminus::Timer::get ( std::string  name)
Parameters
name
Returns
time in milliseconds passed since allocation of the named timer

allocates a new named timer, if it is not allocated already

See also
Timer::adjust()
bool terminus::Timer::isAllocated ( TimerID  id)
Parameters
idof any timer
Returns
true if the given id identifies an allocated timer

The id can belong both to a named or unnamed timer

bool terminus::Timer::isAllocated ( std::string  name)
Parameters
nameof a named timer
Returns
true if name identifies an allocated timer
bool terminus::Timer::isPaused ( )
Returns
global pause state
std::chrono::steady_clock::time_point terminus::Timer::localNow ( )
protected
void terminus::Timer::pause ( )

toggle global pause state

void terminus::Timer::pause ( bool  flag)

set global pause state according to flag

Parameters
flag
void terminus::Timer::releaseTimer ( Timer::TimerID  id)

release ressources (id and timestamp) of an unnamed timer

Parameters
id
void terminus::Timer::releaseTimer ( std::string  name)

release ressources (name, id and timestamp) of a named timer

Parameters
name
void terminus::Timer::startPause ( )
protected
Timer::TimerMSec terminus::Timer::toMSec ( std::chrono::steady_clock::duration  duration)
staticprotected

Member Data Documentation

std::chrono::steady_clock::time_point terminus::Timer::m_baseTimeStamp
protected
std::chrono::steady_clock terminus::Timer::m_clock
protected
bool terminus::Timer::m_isPaused
protected
std::map<std::string, TimerID> terminus::Timer::m_namedTimers
protected
std::chrono::steady_clock::time_point terminus::Timer::m_pauseNow
protected
std::map<TimerID, std::chrono::steady_clock::time_point> terminus::Timer::m_timeStamps
protected

The documentation for this class was generated from the following files: