Stroika Library 3.0d25
 
Loading...
Searching...
No Matches
IntervalTimer.inl
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2026. 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 auto IntervalTimer::Manager::AddOneShot (const TimerCallback& intervalTimer, const Time::Duration& when) -> TimerID
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 TimerID timer = TimerID::New ();
22 fRep_->AddOneShot (timer, intervalTimer, when);
23 return timer;
24 }
25 inline auto IntervalTimer::Manager ::AddRepeating (const TimerCallback& intervalTimer, const Time::Duration& repeatInterval,
26 const optional<Time::Duration>& hysteresis) -> TimerID
27 {
28 RequireNotNull (intervalTimer);
29 Require (repeatInterval >= 0s);
30 Require (hysteresis == nullopt or hysteresis >= 0s);
31 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
32 TimerID timer = TimerID::New ();
33 fRep_->AddRepeating (timer, intervalTimer, repeatInterval, hysteresis);
34 return timer;
35 }
36 inline bool IntervalTimer::Manager::Remove (TimerID timer) noexcept
37 {
38 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
39 return fRep_->Remove (timer);
40 }
41 inline void IntervalTimer::Manager::RemoveRepeating (TimerID timer) noexcept
42 {
43 Remove (timer);
44 }
45 inline auto IntervalTimer::Manager::GetAllRegisteredTasks () const -> RegisteredTaskCollection
46 {
47 RequireNotNull (fRep_); // If this fails, and its accessed through IntervalTimer::Manager::sThe, its probably because of lack of construction of IntervalTimer::Manager::Activator object.
48 return fRep_->GetAllRegisteredTasks ();
49 }
50 inline IntervalTimer::Manager IntervalTimer::Manager::sThe{nullptr};
51
52 /*
53 ********************************************************************************
54 ***************************** IntervalTimer::Adder *****************************
55 ********************************************************************************
56 */
57 inline IntervalTimer::Adder::Adder (const TimerCallback& f, const Time::Duration& repeatInterval, RunImmediatelyFlag runImmediately,
58 const optional<Time::Duration>& hysteresis)
59 : Adder{Manager::sThe, f, repeatInterval, runImmediately, hysteresis}
60 {
61 }
62 inline IntervalTimer::Adder::Adder (IntervalTimer::Manager& manager, const TimerCallback& f, const Time::Duration& repeatInterval,
63 const optional<Time::Duration>& hysteresis)
64 : Adder{manager, f, repeatInterval, RunImmediatelyFlag::eDontRunImmediately, hysteresis}
65 {
66 }
67 inline IntervalTimer::Adder::Adder (const TimerCallback& f, const Time::Duration& repeatInterval, const optional<Time::Duration>& hysteresis)
68 : Adder{Manager::sThe, f, repeatInterval, RunImmediatelyFlag::eDontRunImmediately, hysteresis}
69 {
70 }
71 inline IntervalTimer::Adder::Adder (Adder&& src) noexcept
72 : fManager_{src.fManager_}
73 , fFunction_{move (src.fFunction_)}
74 , fTimer_{src.fTimer_}
75 {
76 src.fManager_ = nullptr; // so its DTOR does nothing (and a move adds nothing)
77 }
78 inline IntervalTimer::Adder::~Adder ()
79 {
80 if (fManager_ != nullptr) { // null if moved from
81 fManager_->Remove (fTimer_);
82 }
83 }
85 {
86 if (this != &rhs) {
87 if (fManager_ != nullptr) { // null if moved from
88 fManager_->Remove (fTimer_);
89 }
90 fManager_ = rhs.fManager_;
91 fFunction_ = move (rhs.fFunction_);
92 fTimer_ = rhs.fTimer_;
93 rhs.fManager_ = nullptr; // so its DTOR does not remove the timer, now this one's
94 }
95 return *this;
96 }
97 inline auto IntervalTimer::Adder::GetCallback () const -> TimerCallback
98 {
99 return fFunction_;
100 }
101
102}
#define RequireNotNull(p)
Definition Assertions.h:350
Adder adds the given function object to the (for now default; later optionally explicit) IntervalTime...
nonvirtual Adder & operator=(Adder &&rhs) noexcept
Removes this Adder's own timer, and takes over rhs's (not adding it again: rhs, moved from,...
Duration is a chrono::duration<double> (=.
Definition Duration.h:96