ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
trigger.hpp
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/// Copyright 2013-2026 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//==================================================================================================
26class Triggered {
27 protected:
28 #if !DOXYGEN
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 #endif
33
34 #if ALIB_STRINGS
35 /// The name of the triggered object. This is mainly used for log output and some
36 /// convenience methods.<br>
37 /// \par Availability
38 /// This field is available only if the module \alib_strings is included in the
39 /// \alibbuild.
40 const String Name;
41
42 /// Constructor. Stores the name in constant member #".Name". Usually these names are
43 /// hard-coded C++ character arrays. If programmatically created, it has to be assured that
44 /// the life-cycle of the string supersedes the lifecycle of the instnace of this class.
45 /// @param pName The name of this object.
46 Triggered(const String& pName) : Name( pName ) {}
47
48 #else
49 Triggered() = default;
50 #endif
51
52 /// Virtual empty destructor. Needed with any virtual class.
53 virtual ~Triggered() {}
54
55 /// Implementations need to return the sleep time, between two trigger events.
56 /// Precisely, this method is called after #".trigger" has been executed and defines the
57 /// next sleep time.
58 /// @return The desired sleep time between two trigger events.
59 virtual Ticks::Duration triggerPeriod() =0;
60
61 /// Implementations need to implement this function and perform their trigger actions
62 /// here.
63 virtual void trigger() =0;
64
65}; // interface class Triggered
66
67//==================================================================================================
68/// The #"%Trigger" class provides a mechanism to periodically "trigger" actions on objects
69/// that implement the `Triggered` interface without requiring dedicated threads per object
70/// or manual additions of actions in existing threads.
71///
72/// The class manages a collection of #"Triggered" objects and ensures that their virtual
73/// #"%Triggered:trigger" methods are called periodically based on the time interval given with
74/// their respective method #"Triggered::triggerPeriod".
75///
76/// Internally, #"%Trigger" operates its own thread to handle the timing and execution of
77/// the triggers, adhering to the specified conditions.
78///
79/// This design helps in simplifying periodic task management within an application,
80/// avoiding thread proliferation and minimizing resource overhead.
81///
82/// \par Key responsibilities of the class:
83/// - Maintain and manage a list of #"%Triggered" objects.
84/// - Schedule and execute periodic triggers for the registered objects.
85/// - Provide the ability to add or remove #"%Triggered" objects dynamically.
86///
87/// \par Usage:
88/// - Users register their implementations of the #"%Triggered" interface with the #"Add()" method
89/// to begin periodic triggers.
90/// - Objects can be unregistered using the #".Remove" method.
91/// - Method #".Stop" method terminates the execution of the internal thread.
92///
93/// Intended for scenarios where lightweight, periodic task scheduling is needed
94/// without creating additional complexity or significant overhead.
95//==================================================================================================
96class Trigger : protected Thread,
97 protected TCondition<Trigger>
98{
99 friend struct threads::TCondition<Trigger>;
100 friend class lang::Owner<Trigger&>;
101
102 protected:
103 /// The entry type used with field #"triggerList".
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 #"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 #"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 #"%alib".
182
183/// Type alias in namespace #"%alib".
185
186} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
void Add(Triggered &triggered, bool initialWakeup=false)
Definition trigger.cpp:19
bool isConditionMet() const noexcept
Definition trigger.hpp:130
bool internalThreadMode
Denotes whether or not the trigger is currently used in internal thread mode.
Definition trigger.hpp:126
ListMA< TriggerEntry > triggerList
The list of registered triggered objects.
Definition trigger.hpp:119
virtual void Run() override
Implementation of the parent interface (virtual abstract).
Definition trigger.cpp:70
virtual void Stop()
Stops the trigger thread and joins it.
Definition trigger.cpp:82
void Do(Ticks until)
Definition trigger.cpp:92
void Do(Ticks::Duration until)
Definition trigger.hpp:156
void Remove(Triggered &triggered)
Definition trigger.cpp:49
Trigger()
Constructor.
Definition trigger.cpp:5
virtual ~Trigger() override
Destructor.
Definition trigger.cpp:10
Triggered(const String &pName)
Definition trigger.hpp:46
virtual Ticks::Duration triggerPeriod()=0
virtual ~Triggered()
Virtual empty destructor. Needed with any virtual class.
Definition trigger.hpp:53
Thread(const character *pName=A_CHAR(""))
Definition thread.hpp:178
virtual void Start()
Definition thread.cpp:224
Definition alox.cpp:14
containers::List< T, MonoAllocator, TRecycling > ListMA
Type alias in namespace #"%alib".
Definition list.hpp:689
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
threadmodel::Triggered Triggered
Type alias in namespace #"%alib".
Definition trigger.hpp:184
time::Ticks Ticks
Type alias in namespace #"%alib".
Definition ticks.hpp:86
threadmodel::Trigger Trigger
Type alias in namespace #"%alib".
Definition trigger.hpp:181
Triggered * Target
The triggered object.
Definition trigger.hpp:114
TriggerEntry(TriggerEntry &)=delete
Deleted copy constructor (to avoid working on copies accidentally).
TriggerEntry(Triggered *target, const Ticks &nextWakeup)
Definition trigger.hpp:108
Ticks NextWakeup
The next wakeup time.
Definition trigger.hpp:115
TCondition(const character *dbgName)