-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathAlarmInputAdapter.h
More file actions
70 lines (57 loc) · 2.14 KB
/
Copy pathAlarmInputAdapter.h
File metadata and controls
70 lines (57 loc) · 2.14 KB
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
#ifndef _IN_CSP_ENGINE_ALARMINPUTADAPTER_H
#define _IN_CSP_ENGINE_ALARMINPUTADAPTER_H
#include <csp/core/Platform.h>
#include <csp/engine/InputAdapter.h>
#include <unordered_set>
namespace csp
{
template<typename T>
class CSPIMPL_EXPORT AlarmInputAdapter final : public InputAdapter
{
public:
AlarmInputAdapter( Engine * engine, CspTypePtr & type ) : InputAdapter( engine, type, PushMode::NON_COLLAPSING )
{
}
void start( DateTime start, DateTime end ) override {}
void stop() override
{
for( auto & handle : m_pendingHandles )
rootEngine() -> cancelCallback( handle );
m_pendingHandles.clear();
}
Scheduler::Handle scheduleAlarm( TimeDelta delta, const T & value )
{
return scheduleAlarm( rootEngine() -> now() + delta, value );
}
Scheduler::Handle scheduleAlarm( DateTime time, const T & value )
{
auto handle = rootEngine() -> reserveSchedulerHandle();
auto it = m_pendingHandles.insert( m_pendingHandles.end(), handle );
handle = rootEngine() -> scheduleCallback( handle, time,
[this, value, it]() -> const InputAdapter *
{
if( !this -> consumeTick<T>( value ) )
return this;
m_pendingHandles.erase( it );
return nullptr;
} );
(*it) = handle;
return handle;
}
Scheduler::Handle rescheduleAlarm( Scheduler::Handle handle, TimeDelta delta )
{
return rescheduleAlarm( handle, rootEngine() -> now() + delta );
}
Scheduler::Handle rescheduleAlarm( Scheduler::Handle handle, DateTime time )
{
return rootEngine() -> rescheduleCallback( handle, time );
}
void cancelAlarm( Scheduler::Handle handle )
{
rootEngine() -> cancelCallback( handle );
}
private:
std::list<Scheduler::Handle> m_pendingHandles;
};
};
#endif