forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactorNodes.h
More file actions
215 lines (167 loc) · 5.57 KB
/
ReactorNodes.h
File metadata and controls
215 lines (167 loc) · 5.57 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#ifndef REACT_DISABLE_REACTORS
#include "react/detail/Defs.h"
#include <cstdint>
#include <functional>
#include <memory>
#include <unordered_map>
#include <utility>
#include <boost/coroutine/all.hpp>
#include "GraphBase.h"
#include "EventStreamNodes.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ReactorNode
///////////////////////////////////////////////////////////////////////////////////////////////////
template
<
typename D,
typename TContext
>
class ReactorNode :
public ReactiveNode<D,void,void>
{
public:
using NodeBasePtrT = NodeBase<D>::PtrT;
using CoroutineT = boost::coroutines::coroutine<const NodeBasePtrT*>;
using LoopT = typename CoroutineT::pull_type;
using OutT = typename CoroutineT::push_type;
using TurnT = typename D::Engine::TurnT;
template <typename F>
ReactorNode(F&& func) :
ReactiveNode<D,void,void>(),
func_{ std::forward<F>(func) }
{
Engine::OnNodeCreate(*this);
}
~ReactorNode()
{
Engine::OnNodeDestroy(*this);
}
void StartLoop()
{
// Could already have started it in ctor,
// but lets make sure node is fully constructed before calls to
// context in coroutine can happen.
mainLoop_ = LoopT
(
[&] (OutT& out)
{
curOutPtr_ = &out;
TContext ctx{ *this };
while (true)
{
func_(ctx);
}
}
);
// First blocking event is not initiated by Tick() but after loop creation.
const auto* p = mainLoop_.get();
REACT_ASSERT(p != nullptr, "StartLoop: first depPtr was null");
Engine::OnNodeAttach(*this, **p);
++depCount_;
}
virtual const char* GetNodeType() const override { return "ReactorNode"; }
virtual bool IsDynamicNode() const override { return true; }
virtual bool IsOutputNode() const override { return true; }
virtual void Tick(void* turnPtr) override
{
turnPtr_ = static_cast<TurnT*>(turnPtr);
REACT_SCOPE_EXIT{ turnPtr_ = nullptr; };
mainLoop_();
if (mainLoop_.get() != nullptr)
{
const auto& depPtr = *mainLoop_.get();
Engine::OnDynamicNodeAttach(*this, *depPtr, *turnPtr_);
++depCount_;
return;
}
offsets_.clear();
}
virtual int DependencyCount() const override
{
return depCount_;
}
template <typename E>
E& Await(const EventStreamNodePtr<D,E>& events)
{
// First attach to target event node
(*curOutPtr_)(&std::static_pointer_cast<NodeBase<D>>(events));
while (! checkEvent<E>(events))
(*curOutPtr_)(nullptr);
REACT_ASSERT(turnPtr_ != nullptr, "Take: turnPtr_ was null");
Engine::OnDynamicNodeDetach(*this, *events, *turnPtr_);
--depCount_;
return events->Events()[offsets_[reinterpret_cast<uintptr_t>(&events)]++];
}
template <typename E, typename F>
void RepeatUntil(const EventStreamNodePtr<D,E>& events, F func)
{
// First attach to target event node
if (turnPtr_ != nullptr)
{
(*curOutPtr_)(&std::static_pointer_cast<NodeBase<D>>(events));
}
else
{
// Non-dynamic attach in case first loop until is encountered before the loop was
// suspended for the first time.
Engine::OnNodeAttach(*this, *events);
++depCount_;
}
// Detach when this function is exited
REACT_SCOPE_EXIT
{
if (turnPtr_ != nullptr)
Engine::OnDynamicNodeDetach(*this, *events, *turnPtr_);
else
Engine::OnNodeDetach(*this, *events);
--depCount_;
};
// Don't enter loop if event already present
if (checkEvent<E>(events))
return;
auto* parentOutPtr = curOutPtr_;
REACT_SCOPE_EXIT{ curOutPtr_ = parentOutPtr; };
// Create and start loop
LoopT nestedLoop_
{
[&] (OutT& out)
{
curOutPtr_ = &out;
while (true)
func();
}
};
// First suspend from initial loop run
(*parentOutPtr)(nestedLoop_.get());
// Further iterations
while (! checkEvent<E>(events))
{
// Advance loop, forward blocking event to parent, and suspend
nestedLoop_();
(*parentOutPtr)(nestedLoop_.get());
}
}
private:
template <typename E>
bool checkEvent(const EventStreamNodePtr<D,E>& events)
{
if (turnPtr_ == nullptr)
return false;
events->SetCurrentTurn(*turnPtr_);
return offsets_[reinterpret_cast<uintptr_t>(&events)] < events->Events().size();
}
std::function<void(TContext&)> func_;
LoopT mainLoop_;
TurnT* turnPtr_;
OutT* curOutPtr_ = nullptr;
int depCount_ = 0;
std::unordered_map<std::uintptr_t, std::size_t> offsets_;
};
/****************************************/ REACT_IMPL_END /***************************************/
#endif //REACT_DISABLE_REACTORS