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