morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
morph::time::ScopedNowOverride Class Reference

RAII helper that fixes DateTime::now() to a constant instant (or a custom callable) for its lifetime and restores the previous override on destruction. More...

#include <datetime.hpp>

Public Member Functions

 ScopedNowOverride (DateTime fixedInstant)
 Installs a clock that always returns fixedInstant.
 ScopedNowOverride (std::function< DateTime()> clock)
 Installs an arbitrary clock callable, saving whatever override was there before.
 ~ScopedNowOverride ()
 Restores the saved override (or "no override", if none was installed before).
 ScopedNowOverride (const ScopedNowOverride &)=delete
ScopedNowOverride & operator= (const ScopedNowOverride &)=delete
 ScopedNowOverride (ScopedNowOverride &&)=delete
ScopedNowOverride & operator= (ScopedNowOverride &&)=delete

Detailed Description

RAII helper that fixes DateTime::now() to a constant instant (or a custom callable) for its lifetime and restores the previous override on destruction.

Mirrors morph::journal::ScopedActionLog and morph::log::ScopedLoggerOverride — the same scoped-install-then-restore pattern used for the codebase's other process-wide override seams, so one test's fixed "now" never leaks into the next.

{
morph::time::ScopedNowOverride guard{someFixedInstant};
// ... DateTime::now()/Timestamp::now(), anywhere, return someFixedInstant ...
} // previous override (or real wall-clock time) restored here
RAII helper that fixes DateTime::now() to a constant instant (or a custom callable) for its lifetime ...
Definition datetime.hpp:340

Definition at line 340 of file datetime.hpp.

Constructor & Destructor Documentation

◆ ScopedNowOverride() [1/2]

morph::time::ScopedNowOverride::ScopedNowOverride ( DateTime fixedInstant)
inlineexplicit

Installs a clock that always returns fixedInstant.

Parameters
fixedInstantThe constant instant DateTime::now() should report for this scope.

Definition at line 344 of file datetime.hpp.

◆ ScopedNowOverride() [2/2]

morph::time::ScopedNowOverride::ScopedNowOverride ( std::function< DateTime()> clock)
inlineexplicit

Installs an arbitrary clock callable, saving whatever override was there before.

Warning
See setNowOverride's warning: clock must not throw and must not call DateTime::now()/Timestamp::now() or install another override.
Parameters
clockCallable returning the instant DateTime::now() should report for this scope.

Definition at line 351 of file datetime.hpp.

◆ ~ScopedNowOverride()

morph::time::ScopedNowOverride::~ScopedNowOverride ( )
inline

Restores the saved override (or "no override", if none was installed before).

Definition at line 359 of file datetime.hpp.


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