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:
43 {
45 spDefault = 100,
47 spTimer = std::numeric_limits<int>::max()
48 };
49
53 SustainBase(const SustainBase&) = delete;
54
59
60 protected:
67 SustainBase(PtrVector* vector, int priority);
68
73
74 public:
78 [[nodiscard]] int getPriority() const;
79
85 void setInterval(const timespec& interval);
86
91 [[nodiscard]] const timespec& getInterval() const;
92
96 void enable();
97
101 void disable();
102
107 [[nodiscard]] bool isEnabled() const;
108
114 virtual inline bool call(const timespec& time);
115
119 static void flushVector(PtrVector* vector);
120
126 static void callSustain(PtrVector* vector = nullptr);
127
128 protected:
145};
146
147inline void SustainBase::setInterval(const timespec& interval)
148{
149 _timer.set(interval);
150}
151
152inline const timespec& SustainBase::getInterval() const
153{
154 return _timer.getInterval();
155}
156
158{
159 _timer.enable();
160}
161
163{
164 _timer.disable();
165}
166
167inline bool SustainBase::isEnabled() const
168{
169 return _timer.isEnabled();
170}
171
172inline bool SustainBase::call(const timespec& time)
173{
174 return false;
175}
176
182template<typename T>
183class TSustain : public SustainBase
184{
185 public:
189 typedef bool (T::*Pmf)(const timespec& t);
190
198 TSustain(T* self, Pmf pmf, int priority = spDefault, PtrVector* vector = nullptr);
199
203 TSustain(const TSustain&) = delete;
204
208 TSustain& operator=(const TSustain&) = delete;
209
210 private:
214 Pmf _pmf;
218 T* _self;
219
226 inline bool call(const timespec& t) override
227 {
228 return (_self->*_pmf)(t);
229 }
230};
231
232template<typename T>
233TSustain<T>::TSustain(T* self, Pmf pmf, int priority, PtrVector* vector)
234 : SustainBase(vector, priority)
235 , _self(self)
236 , _pmf(pmf)
237{}
238
243{
244 public:
248 typedef bool (*Pf)(const timespec&);
249
257 explicit StaticSustain(Pf pf, int priority = spDefault, PtrVector* vector = _defaultVector)
258 : SustainBase(vector, priority)
259 , _pf(pf)
260 {}
261
265 StaticSustain(const StaticSustain&) = delete;
266
271
272 private:
276 Pf _pf{};
277
281 bool call(const timespec& time) override;
282};
283
284inline int SustainBase::getPriority() const
285{
286 return _priority;
287}
288
289inline bool StaticSustain::call(const timespec& time)
290{
291 return _pf(time);
292}
293
303
310
311}// 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:243
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:257
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:162
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:152
virtual bool call(const timespec &time)
Must be overloaded to be able to call the sustain member function.
Definition Sustain.h:172
int _priority
Data member holing priority.
Definition Sustain.h:136
bool isEnabled() const
Returns if the entry is enabled or not.
Definition Sustain.h:167
TVector< SustainBase * > PtrVector
Definition Sustain.h:38
IntervalTimer _timer
Timer for when priority spTimer has been set.
Definition Sustain.h:144
PtrVector * _list
Vector where this entry is part of.
Definition Sustain.h:140
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:147
int getPriority() const
Gets the priority value of this instance;.
Definition Sustain.h:284
static PtrVector * _defaultVector
Pointer to the default vector which is automatically created when.
Definition Sustain.h:132
void enable()
Enables this entry.
Definition Sustain.h:157
ESustainPriority
Do not use an iterator because the sustain function could affect the vector itself.
Definition Sustain.h:43
@ spDefault
Definition Sustain.h:45
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.
TSustain(T *self, Pmf pmf, int priority=spDefault, PtrVector *vector=nullptr)
One and only initializing constructor.
Definition Sustain.h:233
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.
Definition TVector.h:25
#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.