-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.hxx
84 lines (68 loc) · 1.64 KB
/
index.hxx
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
#ifndef HYPER_UTIL_TIMERS_H
#define HYPER_UTIL_TIMERS_H
#include <thread>
#include <functional>
#include <future>
#include <chrono>
#include <thread>
namespace Hyper {
namespace Util {
using namespace std::literals;
typedef std::function<void()> Callback;
inline void sleep (int n) {
auto ms = std::chrono::milliseconds(n);
std::this_thread::sleep_for(ms);
}
class Timer {
public:
using clock_t = std::chrono::high_resolution_clock;
using duration_t = clock_t::duration;
private:
clock_t::time_point start;
public:
Timer() :
start(clock_t::now()) {}
int ms() const {
return (clock_t::now() - start) / 1ms;
}
void reset() {
start = clock_t::now();
}
};
class Timeout {
std::thread th;
bool active = true;
public:
bool isInterval;
void start (Callback cb, int ms) {
auto t = std::chrono::milliseconds(ms);
th = std::thread([&, t]() -> void {
while (active == true) {
std::this_thread::sleep_for(t);
if (active) cb();
if (!isInterval) {
active = false;
return;
}
}
});
}
void clear () {
active = false;
th.join();
}
~Timeout () {
if (th.joinable()) {
th.join();
}
}
};
class Interval : public Timeout {
public:
Interval () {
isInterval = true;
}
};
} // namespace Util
} // namespace Hyper
#endif