-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClockGuard.cpp
More file actions
34 lines (28 loc) · 767 Bytes
/
ClockGuard.cpp
File metadata and controls
34 lines (28 loc) · 767 Bytes
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
#ifndef CLOCK
#define CLOCK
#include <sys/time.h>
#include <ncurses.h>
#include <cassert>
class ClockGuard
{
timeval _m_last_refresh_time;
int _m_threshold__microseconds;
public:
ClockGuard(int threshold__microseconds) : _m_threshold__microseconds{ threshold__microseconds } {
assert(threshold__microseconds < 1000000000);
gettimeofday(&_m_last_refresh_time, nullptr);
}
bool tick()
{
return true;
timeval now; gettimeofday(&now, nullptr);
int duration__microseconds = now.tv_usec - _m_last_refresh_time.tv_usec + 1000000 * (now.tv_sec - _m_last_refresh_time.tv_sec);
if(duration__microseconds >= _m_threshold__microseconds)
{
_m_last_refresh_time = now;
return true;
}
return false;
}
};
#endif