Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
IntervalTimer.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
6
7 /*
8 ********************************************************************************
9 *************************** IntervalTimer::Manager *****************************
10 ********************************************************************************
11 */
12 inline IntervalTimer::Manager::Manager (const shared_ptr<IRep>& rep)
13 : fRep_{rep}
14 {
15 }
16 inline void IntervalTimer::Manager::AddOneShot (const TimerCallback& intervalTimer, const Time::Duration& when)
17 {
18 RequireNotNull (intervalTimer);
19 Require (when >= 0s);
20 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
21 Require (not fRep_->GetAllRegisteredTasks ().Contains (intervalTimer));
22 fRep_->AddOneShot (intervalTimer, when);
23 }
24 inline void IntervalTimer::Manager ::AddRepeating (const TimerCallback& intervalTimer, const Time::Duration& repeatInterval,
25 const optional<Time::Duration>& hysteresis)
26 {
27 RequireNotNull (intervalTimer);
28 Require (repeatInterval >= 0s);
29 Require (hysteresis == nullopt or hysteresis >= 0s);
30 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
31 Require (not fRep_->GetAllRegisteredTasks ().Contains (intervalTimer));
32 fRep_->AddRepeating (intervalTimer, repeatInterval, hysteresis);
33 }
34 inline void IntervalTimer::Manager::RemoveRepeating (const TimerCallback& intervalTimer) noexcept
35 {
36 RequireNotNull (intervalTimer);
37 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
38 Require (fRep_->GetAllRegisteredTasks ().Contains (intervalTimer));
39 fRep_->RemoveRepeating (intervalTimer);
40 }
41 inline auto IntervalTimer::Manager::GetAllRegisteredTasks () const -> RegisteredTaskCollection
42 {
43 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
44 return fRep_->GetAllRegisteredTasks ();
45 }
46 inline IntervalTimer::Manager IntervalTimer::Manager::sThe{nullptr};
47
48 /*
49 ********************************************************************************
50 ***************************** IntervalTimer::Adder *****************************
51 ********************************************************************************
52 */
53 inline IntervalTimer::Adder::Adder (const Function<void (void)>& f, const Time::Duration& repeatInterval,
54 RunImmediatelyFlag runImmediately, const optional<Time::Duration>& hysteresis)
55 : Adder{Manager::sThe, f, repeatInterval, runImmediately, hysteresis}
56 {
57 }
58 inline IntervalTimer::Adder::Adder (IntervalTimer::Manager& manager, const Function<void (void)>& f,
59 const Time::Duration& repeatInterval, const optional<Time::Duration>& hysteresis)
60 : Adder{manager, f, repeatInterval, RunImmediatelyFlag::eDontRunImmediately, hysteresis}
61 {
62 }
63 inline IntervalTimer::Adder::Adder (const Function<void (void)>& f, const Time::Duration& repeatInterval, const optional<Time::Duration>& hysteresis)
64 : Adder{Manager::sThe, f, repeatInterval, RunImmediatelyFlag::eDontRunImmediately, hysteresis}
65 {
66 }
67 inline IntervalTimer::Adder::Adder (Adder&& src) noexcept
68 : fRepeatInterval_{move (src.fRepeatInterval_)}
69 , fHysteresis_{move (src.fHysteresis_)}
70 , fManager_{src.fManager_}
71 , fFunction_{move (src.fFunction_)}
72 {
73 // Move does not trigger re-add to manager
74 src.fManager_ = nullptr; // so its DTOR does nothing
75 }
76 inline IntervalTimer::Adder::~Adder ()
77 {
78 if (fManager_ != nullptr) { // null check cuz Adder can be moved
79 fManager_->RemoveRepeating (fFunction_);
80 }
81 }
82 inline IntervalTimer::Adder& IntervalTimer::Adder::operator= (Adder&& rhs) noexcept
83 {
84 if (this != &rhs) {
85 if (fManager_ != nullptr) { // null check cuz Adder can be moved
86 fManager_->RemoveRepeating (fFunction_);
87 }
88 fManager_ = rhs.fManager_;
89 rhs.fManager_ = nullptr; // so its DTOR doesnt remove
90 fFunction_ = move (rhs.fFunction_);
91 Manager::sThe.AddRepeating (fFunction_, fRepeatInterval_, fHysteresis_);
92 }
93 return *this;
94 }
95 inline Function<void (void)> IntervalTimer::Adder::GetCallback () const
96 {
97 return fFunction_;
98 }
99
100}
#define RequireNotNull(p)
Definition Assertions.h:347
a cross between Mapping<KEY, T> and Collection<T> and Set<T>
nonvirtual void AddRepeating(const TimerCallback &intervalTimer, const Time::Duration &repeatInterval, const optional< Time::Duration > &hysteresis=nullopt)
Add a timer to be called repeatedly after duration repeatInterval.
Duration is a chrono::duration<double> (=.
Definition Duration.h:96