ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
trigger.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_threadmodel of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace threadmodel {
9
10// forward declaration
11class Trigger;
12
13//==================================================================================================
14/// This class declares a simple virtual interface for objects that allow to be triggered
15/// periodically.
16/// The use of this class is recommended to avoid to either creating a dedicated thread for
17/// simple, periodic tasks, or to add invocations to perform such tasks to other threads of the
18/// application.
19///
20/// Instead, class #"Trigger" will be the only responsible entity to
21/// trigger such actions within its own execution thread.
22///
23/// @see Chapter #"alib_thrmod_trigger" of this module's Programmer's Manual
24/// provides a quick source code sample that demonstrates the use this class.
25//==================================================================================================
27{
28 protected:
29 #if !DOXYGEN
30 // Class Trigger is the only one that will trigger us and hence has access to
31 // our protected functions.
32 friend class Trigger;
33 #endif
34
35 #if ALIB_STRINGS
36 /// The name of the triggered object. This is mainly used for log output and some
37 /// convenience methods.<br>
38 /// \par Availability
39 /// This field is available only if the module \alib_strings is included in the
40 /// \alibbuild.
41 const String Name;
42
43 /// Constructor. Stores the name in constant member #".Name". Usually these names are
44 /// hard-coded C++ character arrays. If programmatically created, it has to be assured that
45 /// the life-cycle of the string supersedes the lifecycle of the instnace of this class.
46 /// @param pName The name of this object.
47 Triggered(const String& pName) : Name( pName ) {}
48
49 #else
50 Triggered() = default;
51 #endif
52
53 /// Virtual empty destructor. Needed with any virtual class.
54 virtual ~Triggered() {}
55
56 /// Implementations need to return the sleep time, between two trigger events.
57 /// Precisely, this method is called after #".trigger" has been executed and defines the
58 /// next sleep time.
59 /// @return The desired sleep time between two trigger events.
60 virtual Ticks::Duration triggerPeriod() =0;
61
62 /// Implementations need to implement this function and perform their trigger actions
63 /// here.
64 virtual void trigger() =0;
65
66}; // interface class Triggered
67
68//==================================================================================================
69/// The \b %Trigger class provides a mechanism to periodically "trigger" actions on objects
70/// that implement the `Triggered` interface without requiring dedicated threads per object
71/// or manual additions of actions in existing threads.
72///
73/// The class manages a collection of `Triggered` objects and ensures that their virtual
74/// \b trigger() methods are called periodically based on the time interval given with their
75/// respective method \b triggerPeriod().
76///
77/// Internally, \b %Trigger operates its own thread to handle the timing and execution of
78/// the triggers, adhering to the specified conditions.
79///
80/// This design helps in simplifying periodic task management within an application,
81/// avoiding thread proliferation and minimizing resource overhead.
82///
83/// \par Key responsibilities of the class:
84/// - Maintain and manage a list of `Triggered` objects.
85/// - Schedule and execute periodic triggers for the registered objects.
86/// - Provide the ability to add or remove `Triggered` objects dynamically.
87///
88/// \par Usage:
89/// - Users register their implementations of the \b %Triggered interface with the #"Add()" method
90/// to begin periodic triggers.
91/// - Objects can be unregistered using the `Remove()` method.
92/// - The #"Stop()" method terminates the execution of the internal thread.
93///
94/// Intended for scenarios where lightweight, periodic task scheduling is needed
95/// without creating additional complexity or significant overhead.
96//==================================================================================================
97class Trigger : protected Thread,
98 protected TCondition<Trigger>
99{
100 friend struct threads::TCondition<Trigger>;
101 friend class lang::Owner<Trigger&>;
102
103 protected:
104 /// The entry type used with field #"triggerList".
106 {
107 /// Constructor.
108 /// @param target Assigned to #".Target".
109 /// @param nextWakeup Assigned to #".NextWakeup".
110 TriggerEntry(Triggered* target, const Ticks& nextWakeup )
111 : Target(target), NextWakeup(nextWakeup) {}
112
113 /// Deleted copy constructor (to avoid working on copies accidentally).
115
116 Triggered* Target; ///< The triggered object.
117 Ticks NextWakeup; ///< The next wakeup time.
118 };
119
120 /// The list of registered triggered objects.
122
123 /// The condition requested by parent class #"TCondition" via a call to
124 /// #".isConditionMet".
125 bool wakeUpCondition = false;
126
127 /// Denotes whether or not the trigger is currently used in internal thread mode.
128 bool internalThreadMode = false;
129
130 /// Implementation of the interface needed by parent #"TCondition".
131 /// @return Member #"wakeUpCondition"
132 bool isConditionMet() const noexcept { return wakeUpCondition; }
133
134 public:
135 /// Constructor.
137
138 /// Destructor.
139 ALIB_DLL virtual ~Trigger() override;
140
141 using Thread::Start;
142
143 /// Implementation of the parent interface (virtual abstract).
144 ALIB_DLL virtual void Run() override;
145
146 /// Executes the processing of triggers for up to a given maximum time.
147 /// If the internal thread is not used, this method may be called manually inside an external
148 /// loop to execute triggering operations within the specified timeframe.
149 ///
150 /// If the internal thread was created and is running, with debug-compilations, an \alib
151 /// error will be raised.
152 /// @param until Defines the future point in time until triggering is performed.
153 ALIB_DLL void Do( Ticks until );
154
155 /// Invokes #"Do(Ticks)" by adding the given \p{duration} to the current point in time.
156 /// @param until Defines the maximum duration for which this method will execute the trigger
157 /// logic.
158 void Do( Ticks::Duration until ) { Do( Ticks::Now() + until ); }
159
160 #if !DOXYGEN
161 void Do( Ticks::Duration::TDuration until ) { Do( Ticks::Now() + until ); }
162 #endif
163
164 /// Stops the trigger thread and joins it.
165 ALIB_DLL virtual void Stop();
166
167 /// Add an object to be triggered.
168 /// @param triggered The object to be triggered.
169 /// @param initialWakeup If \c true, the first wakeup is scheduled right away.
170 /// Defaults to \c false.
171 ALIB_DLL void Add( Triggered& triggered, bool initialWakeup= false);
172
173 /// Remove a previously added triggered object.
174 /// @param triggered The object to be removed from the list.
175 ALIB_DLL void Remove( Triggered& triggered);
176
177}; // Trigger
178
179
180} // namespace alib[::threadmodel]
181
182/// Type alias in namespace \b alib.
184
185/// Type alias in namespace \b alib.
187
188} // namespace [alib]
#define ALIB_DLL
Definition alib.inl:573
#define ALIB_EXPORT
Definition alib.inl:562
void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:45
bool isConditionMet() const noexcept
Definition trigger.inl:132
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.inl:128
ListMA< TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.inl:121
virtual void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:96
virtual void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:108
void Do(Ticks until)
Definition trigger.cpp:118
void Do(Ticks::Duration until)
Definition trigger.inl:158
void Remove(Triggered &triggered)
Definition trigger.cpp:75
virtual ~Trigger() override
Destructor.
Definition trigger.cpp:36
Triggered(const String &pName)
Definition trigger.inl:47
virtual Ticks::Duration triggerPeriod()=0
virtual ~Triggered()
Virtual empty destructor. Needed with any virtual class.
Definition trigger.inl:54
Thread(const character *pName=A_CHAR(""))
Definition thread.inl:181
virtual void Start()
Definition thread.cpp:285
containers::List< T, MonoAllocator, TRecycling > ListMA
Type alias in namespace alib.
Definition list.inl:697
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
threadmodel::Triggered Triggered
Type alias in namespace alib.
Definition trigger.inl:186
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.inl:79
threadmodel::Trigger Trigger
Type alias in namespace alib.
Definition trigger.inl:183
Triggered * Target
The triggered object.
Definition trigger.inl:116
TriggerEntry(TriggerEntry &)=delete
Deleted copy constructor (to avoid working on copies accidentally).
TriggerEntry(Triggered *target, const Ticks &nextWakeup)
Definition trigger.inl:110
Ticks NextWakeup
The next wakeup time.
Definition trigger.inl:117
TCondition(const character *dbgName)