Pigweed
 
Loading...
Searching...
No Matches
pw::chrono::VirtualClock< SystemClock > Class Referenceabstract

#include <system_clock.h>

Inheritance diagram for pw::chrono::VirtualClock< SystemClock >:
pw::async::Dispatcher pw::chrono::SimulatedSystemClock pw::async::BasicDispatcher pw::async::FunctionDispatcher pw::async::HeapDispatcher

Public Member Functions

virtual SystemClock::time_point now ()=0
 Returns the current time.
 

Static Public Member Functions

static VirtualClock< SystemClock > & RealClock ()
 Returns a reference to the real system clock to aid instantiation.
 

Detailed Description

An abstract interface representing a SystemClock.

This interface allows decoupling code that uses time from the code that creates a point in time. You can use this to your advantage by injecting Clocks into interfaces rather than having implementations call SystemClock::now() directly. However, this comes at a cost of a vtable per implementation and more importantly passing and maintaining references to the VirtualClock for all of the users.

The VirtualSystemClock::RealClock() function returns a reference to the real global SystemClock.

Example:

void DoFoo(VirtualSystemClock& system_clock) {
SystemClock::time_point now = clock.now();
// ... Code which consumes now.
}
// Production code:
DoFoo(VirtualSystemCLock::RealClock);
// Test code:
MockClock test_clock();
DoFoo(test_clock);
Definition: system_clock.h:175
virtual SystemClock::time_point now()=0
Returns the current time.

This interface is thread and IRQ safe.

Member Function Documentation

◆ now()

virtual SystemClock::time_point pw::chrono::VirtualClock< SystemClock >::now ( )
pure virtual

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