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