-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.cpp
181 lines (143 loc) · 4.31 KB
/
triggers.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <algorithm>
#include <utility>
#include "core/manager.hpp"
#include "timer.hpp"
#include "triggers.hpp"
using namespace std;
namespace sc {
namespace triggers {
/*
* class Value
*/
Value::Value( ChannelValue const& value, function< bool ( ChannelValue const& other ) > condition )
: value_( value )
, condition_( move( condition ) )
{
}
bool Value::matches( ChannelValue const& value ) const
{
return condition_( value );
}
/*
* class Event
*/
Event::~Event() = default;
/*
* class ChangeEvent
*/
ChangeEvent::ChangeEvent( Value value )
: value_( move( value ) )
{
}
bool ChangeEvent::applies( Context const& context ) const
{
bool old = value_.matches( context.lastInput() );
bool current = value_.matches( context.input() );
return !old && current;
}
/*
* class TimeoutEvent
*/
TimeoutEvent::TimeoutEvent( std::size_t timer )
: timer_( timer )
{
}
bool TimeoutEvent::applies( Context const& context ) const
{
return context.timerExpired( timer_ );
}
/*
* class Outcome
*/
Outcome::~Outcome() = default;
/*
* class SetOutcome
*/
SetOutcome::SetOutcome( Value value )
: value_( move( value ) )
{
}
void SetOutcome::invoke( Context& context ) const
{
context.set( value_.get() );
}
/*
* class StartTimerOutcome
*/
StartTimerOutcome::StartTimerOutcome( size_t timer, chrono::nanoseconds const& timeout )
: timer_( timer )
, timeout_( timeout )
{
}
void StartTimerOutcome::invoke( Context& context ) const
{
context.startTimer( timer_, timeout_ );
}
/*
* class StopTimerOutcome
*/
StopTimerOutcome::StopTimerOutcome( size_t timer )
: timer_( timer )
{
}
void StopTimerOutcome::invoke( Context& context ) const
{
context.stopTimer( timer_ );
}
/**
* class Action
*/
Action::Action( std::unique_ptr< Event >&& event, std::vector< std::unique_ptr< Outcome > >&& outcomes )
: event_( move( event ) )
, outcomes_( move( outcomes ) )
{
}
void Action::invoke( Context& context ) const
{
if ( event_->applies( context ) ) {
for_each(
outcomes_.begin(), outcomes_.end(),
[&]( unique_ptr< Outcome > const& outcome ) { outcome->invoke( context ); } );
}
}
/**
* class Context
*/
Context::Context( Connection& connection, Manager& manager, State& state, ChannelValue& value )
: connection_( connection )
, manager_( manager )
, state_( state )
, value_( value )
{
}
Context::~Context()
{
state_.lastInput = move( value_ );
value_ = state_.output;
}
void Context::set( ChannelValue const& value )
{
state_.output = value;
}
void Context::startTimer( size_t timer, chrono::nanoseconds const& timeout )
{
Connection* connection = &connection_;
State* state = &state_;
auto it = state_.timers.emplace( timer, nullptr ).first;
it->second.reset( new Timer( manager_, timeout, [timer, connection, state, it] {
unique_ptr< Timer > ptr( move( it->second ) );
state->timers.erase( it );
state->expiredTimers.insert( timer );
connection->transfer();
} ) );
}
void Context::stopTimer( size_t timer )
{
state_.timers.erase( timer );
}
bool Context::timerExpired( size_t timer ) const
{
return state_.expiredTimers.erase( timer ) > 0;
}
} // namespace triggers
} // namespace sc