cppnet
C++ network utilities for asynchronous servers.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Friends | List of all members
net::timers::timers< Interrupt > Class Template Reference

Provides event-loop timers. More...

#include <net/timers/timers.hpp>

Inheritance diagram for net::timers::timers< Interrupt >:
net::timers::interrupt< Interrupt >

Public Types

using interrupt_type = interrupt< Interrupt >
 The base interrupt type.
 
- Public Types inherited from net::timers::interrupt< Interrupt >
using interrupt_source_t = Source
 The underlying interrupt source type.
 

Public Member Functions

 timers ()=default
 Default constructor.
 
 timers (const timers &other)=delete
 Deleted copy constructor.
 
 timers (timers &&other) noexcept
 Move constructor.
 
auto operator= (const timers &other)=delete
 Deleted copy assignment.
 
auto operator= (timers &&other) noexcept -> timers &
 Move assignment.
 
auto add (timestamp when, handler_t handler, duration period=duration::zero()) -> timer_id
 Add a new timer.
 
template<class Rep , class Period >
auto add (std::chrono::duration< Rep, Period > when, handler_t handler, duration period=duration::zero()) -> timer_id
 Overloaded add function that uses a std::chrono::duration instead of a time_point for the first timeout.
 
auto add (std::uint64_t when, handler_t handler, std::uint64_t period=0) -> timer_id
 Overloaded add function that uses a uint64_t instead of a time_point for the first timeout and the period.
 
auto remove (timer_id tid) noexcept -> timer_id
 Removes the timer with the given id.
 
auto resolve () -> duration
 Resolves all expired event handles.
 
 ~timers ()=default
 Default destructor.
 
- Public Member Functions inherited from net::timers::interrupt< Interrupt >
auto operator() () const noexcept -> void
 Calls the underlying interrupt.
 

Friends

template<InterruptSource I>
auto swap (timers< I > &lhs, timers< I > &rhs) noexcept -> void
 Swap function.
 

Detailed Description

template<InterruptSource Interrupt>
class net::timers::timers< Interrupt >

Provides event-loop timers.

Template Parameters
InterruptAn interrupt source that satisfies the InterruptSource concept.

timers is the spritual successor to cpptime and has been modified for integration with the cppnet context_thread event-loop. As such, it exposes the same API as a CppTime::Timer for adding and removing timers. Unlike a CppTime::Timer, timers does not execute the timer callbacks in a separate thread. Instead, to resolve the timer event callbacks an event loop must explicitly call the public resolve method. The resolve method return a std::chrono duration until the next event timeout. If there are no more events in the internal event queue, then the resolve method returns duration(-1), otherwise the returned duration contains a strictly non-negative count.

Member Function Documentation

◆ add() [1/3]

template<InterruptSource Interrupt>
template<class Rep , class Period >
auto net::timers::timers< Interrupt >::add ( std::chrono::duration< Rep, Period >  when,
handler_t  handler,
duration  period = duration::zero() 
) -> timer_id

Overloaded add function that uses a std::chrono::duration instead of a time_point for the first timeout.

Template Parameters
RepThe arithmetic tick type for a std::chrono::duration.
PeriodThe std::ratio of a std::chrono::duration.
Parameters
whenThe time until the timer times out.
handlerThe timer event handler.
periodThe time between events for a periodic timer.
Returns
The id associated with this timer.

◆ add() [2/3]

template<InterruptSource Interrupt>
auto net::timers::timers< Interrupt >::add ( std::uint64_t  when,
handler_t  handler,
std::uint64_t  period = 0 
) -> timer_id

Overloaded add function that uses a uint64_t instead of a time_point for the first timeout and the period.

Parameters
whenThe number of microseconds until the event times out.
handlerThe event handler.
periodThe number of microseconds between events for a periodic timer.
Returns
The id associated with this timer.

◆ add() [3/3]

template<InterruptSource Interrupt>
auto net::timers::timers< Interrupt >::add ( timestamp  when,
handler_t  handler,
duration  period = duration::zero() 
) -> timer_id

Add a new timer.

Parameters
whenThe time at which the handler is invoked.
handlerThe callable that is invoked when the timer fires.
periodThe periodicity at which the timer fires. Only used for periodic timers.
Returns
The id associated with this timer.

◆ remove()

template<InterruptSource Interrupt>
auto net::timers::timers< Interrupt >::remove ( timer_id  tid) -> timer_id
noexcept

Removes the timer with the given id.

Parameters
tidThe timer_id to remove.
Returns
tid if the timer is not valid. Otherwise returns INVALID_TIMER.

remove is designed to be used in a self-assignment statement. When the timer has been disarmed, the original timer_id will be cleared to an INVALID state. This minimizes the risk of calling remove on a timer twice.

timer_id timer = timers.add(10, [](auto){});
timer = timers.remove(timer); // This sets timer to INVALID_TIMER if
successful.
Provides event-loop timers.
Definition timers.hpp:99
auto add(timestamp when, handler_t handler, duration period=duration::zero()) -> timer_id
Add a new timer.
auto remove(timer_id tid) noexcept -> timer_id
Removes the timer with the given id.
std::size_t timer_id
timer_id type.
Definition timers.hpp:27

◆ resolve()

template<InterruptSource Interrupt>
auto net::timers::timers< Interrupt >::resolve ( ) -> duration

Resolves all expired event handles.

Returns
The duration until the next event times out. Returns duration(-1) if the internal eventq is empty.

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