SPH
Classes | Enumerations | Functions
Timer.h File Reference

Measuring time intervals and executing periodic events. More...

#include "objects/containers/Array.h"
#include "objects/wrappers/Flags.h"
#include "objects/wrappers/SharedPtr.h"
#include <chrono>
#include <functional>

Go to the source code of this file.

Classes

class  Timer
 Basic time-measuring tool. Starts automatically when constructed. More...
 
class  StoppableTimer
 Simple extension of Timer allowing to pause and continue timer. More...
 

Enumerations

enum class  TimerFlags { PERIODIC = 1 << 0 , START_EXPIRED = 1 << 1 }
 
enum class  TimerUnit { SECOND , MILLISECOND , MICROSECOND , NANOSECOND }
 

Functions

SharedPtr< TimermakeTimer (const int64_t interval, const std::function< void(void)> &callback, const Flags< TimerFlags > flags=EMPTY_FLAGS)
 Creates timer with given interval and callback when time interval is finished. More...
 
std::string getFormattedTime (const int64_t time)
 Returns the human-readable formatted time in suitable units. More...
 

Detailed Description

Measuring time intervals and executing periodic events.

Author
Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
Date
2016-2021

Definition in file Timer.h.

Enumeration Type Documentation

◆ TimerFlags

enum TimerFlags
strong
Enumerator
PERIODIC 

Timer will execute callback periodically.

START_EXPIRED 

Creates expired timer, calling elapsed immediately after creating will return the timer interval.

Definition at line 16 of file Timer.h.

◆ TimerUnit

enum TimerUnit
strong
Enumerator
SECOND 
MILLISECOND 
MICROSECOND 
NANOSECOND 

Definition at line 24 of file Timer.h.

Function Documentation

◆ getFormattedTime()

std::string getFormattedTime ( const int64_t  time)

Returns the human-readable formatted time in suitable units.

Parameters
timeTime in milliseconds

Definition at line 169 of file Timer.cpp.

◆ makeTimer()

SharedPtr<Timer> makeTimer ( const int64_t  interval,
const std::function< void(void)> &  callback,
const Flags< TimerFlags flags = EMPTY_FLAGS 
)

Creates timer with given interval and callback when time interval is finished.

The callback is executed only once by default, or periodically if TimerFlags::PERIODIC flag is passed. If the timer is destroyed before the interval passes, no callback is called.

Definition at line 97 of file Timer.cpp.