-
Notifications
You must be signed in to change notification settings - Fork 0
/
TickTask.h
88 lines (76 loc) · 2.13 KB
/
TickTask.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef __TICKTASK_H__
#define __TICKTASK_H__
/**
* TickTask.h
*
* Informaatioluokka, jonka avulla ticker pitää kirjaa
* kohteista, joita sen tulee kutsua määritetyin ajoin.
*
* TickTask sisältää tiedon mitä tickerin tulee kutsua,
* miten pitkän ajan päästä sen tulee kutsua jne.
*/
#include "TickListener.h"
class CTickTask {
public:
enum TICK_RETURN_VALUE {
TICK_REMOVE = -1,
TICK_KEEPDELAY = 0,
};
inline CTickTask(VTickListener *listener, int delay) {
subject = listener;
m_lastDelay = delay;
m_delayToNext = delay;
m_skips = 0;
}
/**
* Kutsuu subjektia, jos sen asettama viive on ylittynyt.
* Saa subjektilta uuden viiveen.
* Jos subjekti ei halua enää tickejä (palauttaa negatiivisen),
* tämä palauttaa false, jolloin STickerin pitäisi poistaa tämä
* tehtävä.
*
* @return false, jos ei haluta enään tickejä ja tämä task tulee poistaa
*/
inline bool tick(int sleepTime) {
if(m_delayToNext > 0) {
m_delayToNext -= sleepTime;
return true;
}
if(m_skips > 0) {
m_skips--;
} else {
int ret = subject->handleTick();
if(ret == TICK_REMOVE) {
// poistetaan ajastustehtävä
return false;
} else if(ret == TICK_KEEPDELAY) {
// käytetään edellistä delayn arvoa
// m_delayToNext = m_lastDelay;
m_delayToNext = m_lastDelay - sleepTime;
} else {
if(ret > 0) {
// seuraava delay on palautuksen arvo
m_lastDelay = ret;
// m_delayToNext = ret;
m_delayToNext = ret - sleepTime;
} else {
// tuntematon vastaus: poistetaan ajastustehtävä
return false;
}
}
}
return true;
}
inline void resetTick(void) {
m_delayToNext = m_lastDelay;
}
inline void skip(unsigned int count) {
m_skips = count;
}
private:
VTickListener *subject; // tickin kohde
int m_lastDelay;
int m_delayToNext; //
int m_skips; // ohittaa kutsun kohteeseen m_skips tickin ajan, vähenee joka tick
};
#endif // __TICKTASK_H__