Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
Sustain.h
Go to the documentation of this file.
1#pragma once
2#include <limits>
4#include <misc/gen/TVector.h>
5#include <misc/gen/target.h>
6#include <misc/global.h>
7
8namespace sf
9{
10
36{
37 public:
39
44 {
46 spDefault = 100,
48 spTimer = std::numeric_limits<int>::max()
49 };
50
54 SustainBase(const SustainBase&) = delete;
55
60
61 protected:
68 SustainBase(PtrVector* vector, int priority);
69
74
75 public:
79 [[nodiscard]] int getPriority() const;
80
86 void setInterval(const timespec& interval);
87
92 [[nodiscard]] const timespec& getInterval() const;
93
97 void enable();
98
102 void disable();
103
108 [[nodiscard]] bool isEnabled() const;
109
115 virtual inline bool call(const timespec& time);
116
120 static void flushVector(PtrVector* vector);
121
127 static void callSustain(PtrVector* vector = nullptr);
128
129 protected:
146};
147
148inline void SustainBase::setInterval(const timespec& interval)
149{
150 _timer.set(interval);
151}
152
153inline const timespec& SustainBase::getInterval() const
154{
155 return _timer.getInterval();
156}
157
159{
160 _timer.enable();
161}
162
164{
165 _timer.disable();
166}
167
168inline bool SustainBase::isEnabled() const
169{
170 return _timer.isEnabled();
171}
172
173inline bool SustainBase::call(const timespec& time)
174{
175 return false;
176}
177
182template<typename T>
183class TSustain : public SustainBase
184{
185 public:
189 typedef bool (T::*Pmf)(const timespec& t);
190
194 virtual ~TSustain() = default;
195
203 TSustain(T* self, Pmf pmf, int priority = spDefault, PtrVector* vector = nullptr);
204
208 TSustain(const TSustain&) = delete;
209
213 TSustain& operator=(const TSustain&) = delete;
214
215 private:
219 Pmf _pmf;
223 T* _self;
224
230 inline bool call(const timespec& tm) override
231 {
232 return (_self->*_pmf)(tm);
233 }
234};
235
236template<typename T>
237TSustain<T>::TSustain(T* self, Pmf pmf, int priority, PtrVector* vector)
238 : SustainBase(vector, priority)
239 , _self(self)
240 , _pmf(pmf)
241{}
242
247{
248 public:
252 typedef bool (*Pf)(const timespec&);
253
261 explicit StaticSustain(Pf pf, int priority = spDefault, PtrVector* vector = _defaultVector)
262 : SustainBase(vector, priority)
263 , _pf(pf)
264 {}
265
269 StaticSustain(const StaticSustain&) = delete;
270
275
276 private:
280 Pf _pf{};
281
285 bool call(const timespec& time) override;
286};
287
288inline int SustainBase::getPriority() const
289{
290 return _priority;
291}
292
293inline bool StaticSustain::call(const timespec& time)
294{
295 return _pf(time);
296}
297
307
313
314}// namespace sf
Timer class that has fixed time intervals at which it becomes true.
Definition IntervalTimer.h:14
void enable()
Enables the timer with the current interval time.
Definition IntervalTimer.h:155
bool isEnabled() const
Gets timer enabled.
Definition IntervalTimer.h:165
const TimeSpec & getInterval() const
Gets the set interval time in TimeSpec format.
Definition IntervalTimer.h:170
void disable()
Disables the timer.
Definition IntervalTimer.h:160
void set(const timespec &t)
Sets the timers interval time.
Implements a class to hook a static function to the global main thread sustain call.
Definition Sustain.h:247
StaticSustain & operator=(const StaticSustain &)=delete
Prevent copying.
StaticSustain(const StaticSustain &)=delete
Prevent copying.
StaticSustain(Pf pf, int priority=spDefault, PtrVector *vector=_defaultVector)
One and only constructor.
Definition Sustain.h:261
Base class for template TSustain which enables repetitive calls from the main thread with a set frequ...
Definition Sustain.h:36
void disable()
Disables this entry.
Definition Sustain.h:163
static void flushVector(PtrVector *vector)
Flushes the vector and clears all Vector member of all entries first.
~SustainBase()
oes not have to be virtual because this base class is not used must always be derived.
const timespec & getInterval() const
Gets the interval at which the hooked function is called.
Definition Sustain.h:153
virtual bool call(const timespec &time)
Must be overloaded to be able to call the sustain member function.
Definition Sustain.h:173
int _priority
Data member holing priority.
Definition Sustain.h:137
bool isEnabled() const
Returns if the entry is enabled or not.
Definition Sustain.h:168
TVector< SustainBase * > PtrVector
Definition Sustain.h:38
IntervalTimer _timer
Timer for when priority spTimer has been set.
Definition Sustain.h:145
PtrVector * _list
Vector where this entry is part of.
Definition Sustain.h:141
static void callSustain(PtrVector *vector=nullptr)
Calls all sustain table entry functions in the vector passed. The default is the static default vecto...
SustainBase(PtrVector *vector, int priority)
Default Constructor adding itself to the passed vector. If the passed vector is NULL the default vect...
void setInterval(const timespec &interval)
Sets the interval at which the hooked function is called. This is only valid when the priority for th...
Definition Sustain.h:148
int getPriority() const
Gets the priority value of this instance;.
Definition Sustain.h:288
static PtrVector * _defaultVector
Pointer to the default vector which is automatically created when.
Definition Sustain.h:133
void enable()
Enables this entry.
Definition Sustain.h:158
ESustainPriority
Do not use an iterator because the sustain function could affect the vector itself.
Definition Sustain.h:44
@ spDefault
Definition Sustain.h:46
SustainBase & operator=(const SustainBase &)=delete
Prevent copying.
SustainBase(const SustainBase &)=delete
Prevent copying.
Template to make the sustain system call a class method regularly.
Definition Sustain.h:184
TSustain(const TSustain &)=delete
Prevent copying.
virtual ~TSustain()=default
Virtual destructor.
TSustain(T *self, Pmf pmf, int priority=spDefault, PtrVector *vector=nullptr)
One and only initializing constructor.
Definition Sustain.h:237
TSustain & operator=(const TSustain &)=delete
Prevent copying.
bool(T::* Pmf)(const timespec &t)
Required type.
Definition Sustain.h:189
Counted vector having additional methods and operators for ease of usage. This template class extends...
Definition TVector.h:20
#define _MISC_FUNC
Definition misc/global.h:39
#define _MISC_CLASS
Definition misc/global.h:40
Definition Application.h:10
_MISC_FUNC bool setSustainTimer(int msec)
This function will enable a timer that call all Sustain functions in the _defaultVector member of the...
_MISC_FUNC int getSustainTimer()
Gets the current sustain timer interval in milliseconds.