-
Notifications
You must be signed in to change notification settings - Fork 2
/
WaitingLogic.h
68 lines (53 loc) · 1.56 KB
/
WaitingLogic.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
//
// Created by romanov on 5/30/17.
//
#ifndef EVESTANDALONE_WAITINGLOGIC_H
#define EVESTANDALONE_WAITINGLOGIC_H
#include <atomic>
#include <chrono>
#include <thread>
class WaitingLogic {
public:
WaitingLogic():
_autoplayDuration(std::chrono::seconds(2)),
_isAutoplay(false),
_mustProceedToNextEvent(false),
_mustQuit(false)
{
}
void ProceedToNextEvent(){_mustProceedToNextEvent = true;}
void SetAutoPlay(bool value){_isAutoplay = value;}
bool IsAutoPlay() { return _isAutoplay; }
void Wait()
{
using namespace std::chrono;
// time of waiting start
auto start = high_resolution_clock::now();
// go to semi infinite loop
while(!_mustQuit)
{
std::this_thread::sleep_for(milliseconds(1));
if(_isAutoplay)
{
auto finish = high_resolution_clock::now();
auto microseconds = duration_cast<milliseconds>(finish-start);
if (microseconds > _autoplayDuration)
{
return;
}
}
// User (or something) want proceed to the next event
if(_mustProceedToNextEvent)
{
_mustProceedToNextEvent = false;
return;
}
}
}
private:
std::chrono::seconds _autoplayDuration;
std::atomic<bool> _isAutoplay;
std::atomic<bool> _mustProceedToNextEvent;
std::atomic<bool> _mustQuit;
};
#endif //EVESTANDALONE_WAITINGLOGIC_H