BRICKS
Small, useful blocks of code, to build bigger things.
Loading...
Searching...
No Matches
bricks::timer Class Reference

A timer that can be started and aborted. More...

#include <timer.hpp>

Public Types

using completion_token = std::future<void>
 A completion token that can be used to wait for the timer to complete.
 

Public Member Functions

 timer () noexcept
 Default constructor.
 
 timer (const timer &)=delete
 A timer cannot be copied.
 
auto operator= (const timer &) -> timer &=delete
 A timer cannot be copied.
 
 timer (timer &&)=default
 Move constructor.
 
auto operator= (timer &&) -> timer &=default
 Move assignment operator.
 
 ~timer ()
 Destroy the timer object.
 
template<typename Rep = int64_t, typename Period = std::ratio<1>>
auto start (const std::chrono::duration< Rep, Period > &duration=std::chrono::duration< Rep, Period >{}) const noexcept -> completion_token
 Starts the timer, returning a completion token that will be completed when the timer expires.
 
auto abort () -> void
 Aborts the timer.
 

Detailed Description

A timer that can be started and aborted.

The timer can be started with a duration. If the timer is not aborted, the completion token returned by start will be ready after the duration has passed. If the timer is aborted, the completion token will be ready immediately. Will abort all outstanding timers when it is destroyed.

Example:

auto completion_token = t.start(std::chrono::milliseconds(5));
do {
INFO("Waiting for timer to complete");
} while (completion_token.wait_for(std::chrono::milliseconds(1)) != std::future_status::ready);

Member Typedef Documentation

◆ completion_token

using bricks::timer::completion_token = std::future<void>

A completion token that can be used to wait for the timer to complete.

The completion token can be used to wait for the timer to complete. If the timer is not aborted, the completion token will be ready after the duration has passed. If the timer is aborted, the completion token will be ready immediately.

Example:

auto completion_token = t.start(std::chrono::milliseconds(5));
do {
INFO("Waiting for timer to complete");
} while (completion_token.wait_for(std::chrono::milliseconds(1)) != std::future_status::ready);

Constructor & Destructor Documentation

◆ timer() [1/3]

bricks::timer::timer ( )
inlinenoexcept

Default constructor.

◆ timer() [2/3]

bricks::timer::timer ( const timer & )
delete

A timer cannot be copied.

◆ timer() [3/3]

bricks::timer::timer ( timer && )
default

Move constructor.

◆ ~timer()

bricks::timer::~timer ( )
inline

Destroy the timer object.

Will abort all outstanding timers.

Member Function Documentation

◆ abort()

auto bricks::timer::abort ( ) -> void
inline

Aborts the timer.

This function aborts the timer, causing any completion tokens returned by start to complete.

Example:

auto completion_token = t.start(std::chrono::milliseconds(100));
t.abort();
while (completion_token.wait_for(std::chrono::milliseconds(1)) != std::future_status::ready) {
FAIL("Timer should have been aborted"); // This will not print
}

◆ operator=() [1/2]

auto bricks::timer::operator= ( const timer & ) -> timer &=delete
delete

A timer cannot be copied.

◆ operator=() [2/2]

auto bricks::timer::operator= ( timer && ) -> timer &=default
default

Move assignment operator.

◆ start()

template<typename Rep = int64_t, typename Period = std::ratio<1>>
auto bricks::timer::start ( const std::chrono::duration< Rep, Period > & duration = std::chrono::duration<Rep, Period>{}) const -> completion_token
inlinenodiscardnoexcept

Starts the timer, returning a completion token that will be completed when the timer expires.

Example:

auto completion_token = t.start(std::chrono::milliseconds(5));
do {
INFO("Waiting for timer to complete");
} while (completion_token.wait_for(std::chrono::milliseconds(1)) != std::future_status::ready);
Parameters
durationThe duration to wait before completing the token.
Returns
A timer::completion_token that will be completed when the timer expires.

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