-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtimer.h
150 lines (124 loc) · 4.16 KB
/
timer.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef TIMER_H_INCLUDED
#define TIMER_H_INCLUDED
#include <thread>
#include <chrono>
//Timer.h deals with time!!
class Timer
{
//Threads are new to C++
//this will allow us to run asynchronous code execution
//without blocking the main thread in main.cpp (the while loop)
std::thread Thread;
bool Alive = false; //will check if timer is running
long CallNumber = -1L; //counts how many times we would like to run a function (ie: send mail funciton should be called 5x)
//L stands for long so the number doesn't become an int
long repeat_count = -1L; //counts how many times a certain function has been called (ie: send mail function has been called 2x so far)
std::chrono::milliseconds interval = std::chrono::milliseconds(0);
//this is also new to c++ 11: std::function is a new type that can contain functions.
//this part says: void(void) it takes a function that takes in nothing and returns nothing
//nullptr is also new to c++ 11; before we would use zero but that would conflict with stuff
//so now we use nullptr which means this function is not pointing to anything
std::function<void(void)> funct = nullptr;
void SleepAndRun()
{
std::this_thread::sleep_for(interval);
if(Alive)
{
Function()(); //double parentheses: 1st pair calls the function to return a function
//second pair calls the function the 1st function returns
}
}
void ThreadFunc()
{
if(CallNumber == Infinite)
{
while(Alive)
{
SleepAndRun();
}
}
else
{
while(repeat_count--)
{
SleepAndRun();
}
}
}
public:
static const long Infinite = -1L;
//constructors
Timer(){}
Timer(const std::function<void(void)> &f) : funct(f) {}
Timer(const std::function<void(void)> &f,
const unsigned long &i,
const long repeat = Timer::Infinite) : funct(f),
interval(std::chrono::milliseconds(i)),
CallNumber(repeat) {}
//starts the timer
void Start(bool Async = true)
{
if(IsAlive())
{
return;
}
Alive = true;
repeat_count = CallNumber;
if(Async)
{
//this is creating the Thread we initialized above
//ThreadFunc is the function we created
//it is being called to run on this thread
//[this] is the parameter needed for the ThreadFunc function
Thread = std::thread(ThreadFunc, this);
}
else
{
//call the ThreadFunc()
this->ThreadFunc();
}
}
//manually stops the timer
void Stop()
{
Alive = false;
//joining means the invoked Thread will finish execution before joining the main thread
//so basically stop the timer
Thread.join();
}
//sets the function to be executed
void SetFunction(const std::function<void(void)> &f)
{
funct = f;
}
//is the timer running or not?
bool IsAlive() const {return Alive;}
//sets how many times it is going to repeat
void RepeatCount(const long r)
{
if(Alive)
{
return;
}
CallNumber = r;
}
//returns how many iterations are left
long GetLeftCount() const {return repeat_count;}
//returns how many iterations we had originally requested
long RepeatCount() const {return CallNumber;}
void SetInterval(const unsigned long &i)
{
if(Alive)
{
return;
}
interval = std::chrono::milliseconds(i);
}
//.count() converts from chrono milliseconds to long
unsigned long Interval() const {return interval.count();}
const std::function<void(void)> &Function() const
{
return funct;
}
};
#endif // TIMER_H_INCLUDED