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