⏱️ golxzn::os::chrono ⏱️ 1.2.4
Chrono for golxzn's projects.
Loading...
Searching...
No Matches
golxzn::os::chrono::timer< OnTimerDone, BaseClock > Class Template Reference

Class that represents timer. More...

#include <timer.hpp>

Public Types

using base_clock = BaseClock
 Base clock type.
 
using time_point = typename base_clock::time_point
 Type of time point from base_clock.
 
using timer_end_callback = OnTimerDone
 Type of callback that will be called when timer is done.
 

Public Member Functions

template<class Rep , class Period >
 timer (const std::chrono::duration< Rep, Period > timer_interval, timer_end_callback &&callback)
 Timer constructor from timer interval and callback.
 
 timer (const time timer_interval, timer_end_callback &&callback)
 Timer constructor from timer interval and callback.
 
void update ()
 Updates timer. (only when GOLXZN_MULTITHREADING is not defined)
 
bool is_done () const noexcept
 Returns true if timer is done.
 
bool is_running () const noexcept
 Returns true if timer is running.
 
time time_left () const noexcept
 Returns time left to the end of the timer.
 

Detailed Description

template<class OnTimerDone, class BaseClock = utils::default_base_clock>
class golxzn::os::chrono::timer< OnTimerDone, BaseClock >

Class that represents timer.

Warning
This class behaviour is different with and without GOLXZN_MULTITHREADING macros definition.
Template Parameters
OnTimerDonetype of function that will be called when timer is done.
BaseClockclock that will be used for measurement. It has to be monotonic and STL compatible.

This class is used to measure time. It calls callback when it's done. If GOLXZN_MULTITHREADING is not defined it doesn't use a thread. So to call callback it has to be updated in main thread. It storing callback. If GOLXZN_MULTITHREADING is defined it uses a thread. So to call callback it doesn't have to be updated. It doesn't storing callback, but std::thread

Example of using:

std::atomic_bool executed{ false };
std::function<void()>{ [&executed] { executed.store(true); } }
};
while(!timer2.is_done()) {
std::this_thread::sleep_for(1ms);
}
Class that represents timer.
Definition timer.hpp:60

Constructor & Destructor Documentation

◆ timer() [1/2]

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
template<class Rep , class Period >
golxzn::os::chrono::timer< OnTimerDone, BaseClock >::timer ( const std::chrono::duration< Rep, Period >  timer_interval,
timer_end_callback &&  callback 
)

Timer constructor from timer interval and callback.

Timer's callback will be called after timer_interval.

Warning
There's no invalid function checking! It'll terminate if callback is invalid.
Parameters
timer_intervalTimer interval.
callbackFunction that will be called after timer_interval.
precisionPrecision of timer. By default it's 1 microsecond (1us). Only if GOLXZN_MULTITHREADING is defined.

◆ timer() [2/2]

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
golxzn::os::chrono::timer< OnTimerDone, BaseClock >::timer ( const time  timer_interval,
timer_end_callback &&  callback 
)

Timer constructor from timer interval and callback.

Timer's callback will be called after timer_interval.

Warning
There's no invalid function checking! It'll terminate if callback is invalid.
Parameters
timer_intervalTimer interval.
callbackFunction that will be called after timer_interval.
precisionPrecision of timer. By default it's 1 microsecond (1us). Only if GOLXZN_MULTITHREADING is defined.

Member Function Documentation

◆ is_done()

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
bool golxzn::os::chrono::timer< OnTimerDone, BaseClock >::is_done ( ) const
noexcept

Returns true if timer is done.

See also
is_running()
time_left()

◆ is_running()

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
bool golxzn::os::chrono::timer< OnTimerDone, BaseClock >::is_running ( ) const
noexcept

Returns true if timer is running.

See also
is_done()
time_left()

◆ time_left()

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
time golxzn::os::chrono::timer< OnTimerDone, BaseClock >::time_left ( ) const
noexcept

Returns time left to the end of the timer.

See also
is_running()
is_done()

◆ update()

template<class OnTimerDone , class BaseClock = utils::default_base_clock>
void golxzn::os::chrono::timer< OnTimerDone, BaseClock >::update ( )

Updates timer. (only when GOLXZN_MULTITHREADING is not defined)

Warning
There's no invalid function checking! It'll terminate if callback is invalid.

Should be called in main thread. It's not needed if GOLXZN_MULTITHREADING is defined.


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