morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::reactive::testing::ManualScheduler Class Referencefinal

A deterministic Scheduler: its clock moves only in advance(), which fires what falls due on the calling thread. More...

#include <manual_scheduler.hpp>

Inheritance diagram for morph::reactive::testing::ManualScheduler:
morph::reactive::Scheduler

Public Member Functions

 ManualScheduler (ManualScheduler const &)=delete
ManualScheduler & operator= (ManualScheduler const &)=delete
 ManualScheduler (ManualScheduler &&)=delete
ManualScheduler & operator= (ManualScheduler &&)=delete
TimerHandle after (std::chrono::milliseconds delay, std::function< void()> fn) override
 Runs fn once, delay after the current time.
TimerHandle every (std::chrono::milliseconds period, std::function< void()> fn) override
 Runs fn every period, starting one period after the current time.
void advance (std::chrono::milliseconds delta)
 Moves the clock forward by delta, stopping at each deadline on the way to fire what is due there.
std::chrono::milliseconds now () const
 The scheduler's clock: the sum of every advance() so far, or, inside a callback, that callback's deadline. It never decreases.
std::size_t pendingTimers () const
 How many timers are scheduled: periodic ones until cancelled, one-shots until they fire or are cancelled.
Public Member Functions inherited from morph::reactive::Scheduler
 Scheduler (Scheduler const &)=delete
Scheduler & operator= (Scheduler const &)=delete
 Scheduler (Scheduler &&)=delete
Scheduler & operator= (Scheduler &&)=delete

Detailed Description

A deterministic Scheduler: its clock moves only in advance(), which fires what falls due on the calling thread.

advance(delta) models time passing while the owner loop runs and is never blocked: the clock stops at each deadline in turn, so every() fires once per elapsed period, and a timer a callback creates fires in the same advance when its deadline falls within it. Timers due at the same instant fire in the order they were created (a periodic timer keeps the place its first scheduling gave it). One exception keeps a self-re-arming zero-delay timer from looping: a timer a callback creates with a non-positive delay is due at the next advance(), not at the current instant.

It keeps the Scheduler contract: a timer cancelled by an earlier callback does not fire, a callback may cancel any timer, its own included, and handles may outlive the scheduler. Not thread-safe.

Definition at line 33 of file manual_scheduler.hpp.

Member Function Documentation

◆ after()

TimerHandle morph::reactive::testing::ManualScheduler::after ( std::chrono::milliseconds delay,
std::function< void()> fn )
inlinenodiscardoverridevirtual

Runs fn once, delay after the current time.

Parameters
delayHow long to wait; a negative delay counts as zero, which is due at the next advance(), advance(0ms) included.
fnThe callback.
Returns
The handle; destroying it before the deadline cancels the call.

Implements morph::reactive::Scheduler.

Definition at line 47 of file manual_scheduler.hpp.

◆ every()

TimerHandle morph::reactive::testing::ManualScheduler::every ( std::chrono::milliseconds period,
std::function< void()> fn )
inlinenodiscardoverridevirtual

Runs fn every period, starting one period after the current time.

Parameters
periodThe interval; must be positive.
fnThe callback.
Returns
The handle; destroying it stops the repetition.
Exceptions
std::invalid_argumentwhen period is not positive.

Implements morph::reactive::Scheduler.

Definition at line 56 of file manual_scheduler.hpp.

◆ advance()

void morph::reactive::testing::ManualScheduler::advance ( std::chrono::milliseconds delta)
inline

Moves the clock forward by delta, stopping at each deadline on the way to fire what is due there.

A callback sees the clock at its own deadline. A periodic timer is next due one period after the deadline it fired for. A callback that throws ends the advance: the exception propagates, the clock stays at that deadline, the throwing timer is rescheduled (periodic) or gone (one-shot) as if it had returned, and the rest fire at the next advance.

Parameters
deltaHow far to move; must not be negative.
Exceptions
std::invalid_argumentwhen delta is negative.
std::logic_errorwhen called from a timer callback.

Definition at line 73 of file manual_scheduler.hpp.

◆ now()

std::chrono::milliseconds morph::reactive::testing::ManualScheduler::now ( ) const
inlinenodiscard

The scheduler's clock: the sum of every advance() so far, or, inside a callback, that callback's deadline. It never decreases.

Returns
The current time.

Definition at line 101 of file manual_scheduler.hpp.

◆ pendingTimers()

std::size_t morph::reactive::testing::ManualScheduler::pendingTimers ( ) const
inlinenodiscard

How many timers are scheduled: periodic ones until cancelled, one-shots until they fire or are cancelled.

Returns
The count.

Definition at line 106 of file manual_scheduler.hpp.


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