forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.h
More file actions
424 lines (325 loc) · 15.6 KB
/
Copy pathevent.h
File metadata and controls
424 lines (325 loc) · 15.6 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright Sebastian Jeckel 2017.
// 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)
#ifndef REACT_EVENT_H_INCLUDED
#define REACT_EVENT_H_INCLUDED
#pragma once
#include "react/detail/defs.h"
#include <memory>
#include <type_traits>
#include <utility>
#include "react/api.h"
#include "react/group.h"
#include "react/detail/event_nodes.h"
#include "react/common/ptrcache.h"
/*****************************************/ REACT_BEGIN /*****************************************/
template <typename> class Event;
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <typename E>
Event<E> SameGroupOrLink(const Group& targetGroup, const Event<E>& dep);
/****************************************/ REACT_IMPL_END /***************************************/
/*****************************************/ REACT_BEGIN /*****************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Event
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E>
class Event : protected REACT_IMPL::EventInternals<E>
{
public:
// Construct with explicit group
template <typename F, typename T>
static Event Create(const Group& group, F&& func, const Event<T>& dep)
{ return CreateProcessingNode(group, std::forward<F>(func), dep); }
// Construct with implicit group
template <typename F, typename T>
static Event Create(F&& func, const Event<T>& dep)
{ return CreateProcessingNode(dep.GetGroup(), std::forward<F>(func), dep); }
// Construct with explicit group
template <typename F, typename T, typename ... Us>
static Event Create(const Group& group, F&& func, const Event<T>& dep, const State<Us>& ... states)
{ return CreateSyncedProcessingNode(group, std::forward<F>(func), dep, states ...); }
// Construct with implicit group
template <typename F, typename T, typename ... Us>
static Event Create(F&& func, const Event<T>& dep, const State<Us>& ... states)
{ return CreateSyncedProcessingNode(dep.GetGroup(), std::forward<F>(func), dep, states ...); }
Event() = default;
Event(const Event&) = default;
Event& operator=(const Event&) = default;
Event(Event&&) = default;
Event& operator=(Event&&) = default;
auto GetGroup() const -> const Group&
{ return this->GetNodePtr()->GetGroup(); }
auto GetGroup() -> Group&
{ return this->GetNodePtr()->GetGroup(); }
friend bool operator==(const Event<E>& a, const Event<E>& b)
{ return a.GetNodePtr() == b.GetNodePtr(); }
friend bool operator!=(const Event<E>& a, const Event<E>& b)
{ return !(a == b); }
friend auto GetInternals(Event<E>& e) -> REACT_IMPL::EventInternals<E>&
{ return e; }
friend auto GetInternals(const Event<E>& e) -> const REACT_IMPL::EventInternals<E>&
{ return e; }
protected:
Event(std::shared_ptr<REACT_IMPL::EventNode<E>>&& nodePtr) :
Event::EventInternals( std::move(nodePtr) )
{ }
template <typename F, typename T>
static auto CreateProcessingNode(const Group& group, F&& func, const Event<T>& dep) -> std::shared_ptr<REACT_IMPL::EventNode<E>>
{
using REACT_IMPL::EventProcessingNode;
using REACT_IMPL::SameGroupOrLink;
return std::make_shared<EventProcessingNode<E, T, typename std::decay<F>::type>>(
group, std::forward<F>(func), SameGroupOrLink(group, dep));
}
template <typename F, typename T, typename ... Us>
static auto CreateSyncedProcessingNode(const Group& group, F&& func, const Event<T>& dep, const State<Us>& ... syncs) -> decltype(auto)
{
using REACT_IMPL::SyncedEventProcessingNode;
using REACT_IMPL::SameGroupOrLink;
return std::make_shared<SyncedEventProcessingNode<E, T, typename std::decay<F>::type, Us ...>>(
group, std::forward<F>(func), SameGroupOrLink(group, dep), SameGroupOrLink(group, syncs) ...);
}
template <typename RET, typename NODE, typename ... ARGS>
friend RET impl::CreateWrappedNode(ARGS&& ... args);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EventSource
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E>
class EventSource : public Event<E>
{
public:
// Construct event source
static EventSource Create(const Group& group)
{ return CreateSourceNode(group); }
EventSource() = default;
EventSource(const EventSource&) = default;
EventSource& operator=(const EventSource&) = default;
EventSource(EventSource&& other) = default;
EventSource& operator=(EventSource&& other) = default;
void Emit(const E& value)
{ EmitValue(value); }
void Emit(E&& value)
{ EmitValue(std::move(value)); }
template <typename T = E, typename = std::enable_if_t<std::is_same<T, Token>::value>>
void Emit()
{ EmitValue(Token::value); }
EventSource& operator<<(const E& value)
{ EmitValue(value); return *this; }
EventSource& operator<<(E&& value)
{ EmitValue(std::move(value)); return *this; }
protected:
EventSource(std::shared_ptr<REACT_IMPL::EventNode<E>>&& nodePtr) :
EventSource::Event( std::move(nodePtr) )
{ }
private:
static auto CreateSourceNode(const Group& group) -> std::shared_ptr<REACT_IMPL::EventNode<E>>
{
using REACT_IMPL::EventSourceNode;
return std::make_shared<EventSourceNode<E>>(group);
}
template <typename T>
void EmitValue(T&& value)
{
using REACT_IMPL::NodeId;
using REACT_IMPL::ReactGraph;
using REACT_IMPL::EventSourceNode;
auto* castedPtr = static_cast<EventSourceNode<E>*>(this->GetNodePtr().get());
NodeId nodeId = castedPtr->GetNodeId();
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
graphPtr->PushInput(nodeId, [castedPtr, &value] { castedPtr->EmitValue(std::forward<T>(value)); });
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EventSlotBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E>
class EventSlot : public Event<E>
{
public:
// Construct emtpy slot
static EventSlot Create(const Group& group)
{ return CreateSlotNode(group); }
EventSlot() = default;
EventSlot(const EventSlot&) = default;
EventSlot& operator=(const EventSlot&) = default;
EventSlot(EventSlot&&) = default;
EventSlot& operator=(EventSlot&&) = default;
void Add(const Event<E>& input)
{ AddSlotInput(input); }
void Remove(const Event<E>& input)
{ RemoveSlotInput(input); }
void RemoveAll()
{ RemoveAllSlotInputs(); }
protected:
EventSlot(std::shared_ptr<REACT_IMPL::EventNode<E>>&& nodePtr) :
EventSlot::Event( std::move(nodePtr) )
{ }
private:
static auto CreateSlotNode(const Group& group) -> decltype(auto)
{
using REACT_IMPL::EventSlotNode;
return std::make_shared<EventSlotNode<E>>(group);
}
void AddSlotInput(const Event<E>& input)
{
using REACT_IMPL::NodeId;
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
NodeId nodeId = castedPtr->GetInputNodeId();
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
graphPtr->PushInput(nodeId, [this, castedPtr, &input] { castedPtr->AddSlotInput(SameGroupOrLink(this->GetGroup(), input)); });
}
void RemoveSlotInput(const Event<E>& input)
{
using REACT_IMPL::NodeId;
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
NodeId nodeId = castedPtr->GetInputNodeId();
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
graphPtr->PushInput(nodeId, [this, castedPtr, &input] { castedPtr->RemoveSlotInput(SameGroupOrLink(this->GetGroup(), input)); });
}
void RemoveAllSlotInputs()
{
using REACT_IMPL::NodeId;
using SlotNodeType = REACT_IMPL::EventSlotNode<E>;
SlotNodeType* castedPtr = static_cast<SlotNodeType*>(this->GetNodePtr().get());
NodeId nodeId = castedPtr->GetInputNodeId();
auto& graphPtr = GetInternals(this->GetGroup()).GetGraphPtr();
graphPtr->PushInput(nodeId, [castedPtr] { castedPtr->RemoveAllSlotInputs(); });
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// EventLink
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E>
class EventLink : public Event<E>
{
public:
// Construct with group
static EventLink Create(const Group& group, const Event<E>& input)
{ return GetOrCreateLinkNode(group, input); }
EventLink() = default;
EventLink(const EventLink&) = default;
EventLink& operator=(const EventLink&) = default;
EventLink(EventLink&&) = default;
EventLink& operator=(EventLink&&) = default;
protected:
EventLink(std::shared_ptr<REACT_IMPL::EventNode<E>>&& nodePtr) :
EventLink::Event( std::move(nodePtr) )
{ }
private:
static auto GetOrCreateLinkNode(const Group& group, const Event<E>& input) -> std::shared_ptr<REACT_IMPL::EventNode<E>>
{
using REACT_IMPL::EventLinkNode;
using REACT_IMPL::IReactNode;
using REACT_IMPL::ReactGraph;
IReactNode* k = GetInternals(input).GetNodePtr().get();
ReactGraph::LinkCache& linkCache = GetInternals(group).GetGraphPtr()->GetLinkCache();
std::shared_ptr<IReactNode> nodePtr = linkCache.LookupOrCreate(k, [&]
{
auto nodePtr = std::make_shared<EventLinkNode<E>>(group, input);
nodePtr->SetWeakSelfPtr(std::weak_ptr<EventLinkNode<E>>{ nodePtr });
return std::static_pointer_cast<IReactNode>(nodePtr);
});
return std::static_pointer_cast<EventLinkNode<E>>(nodePtr);
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Merge
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E, typename ... Us>
static auto Merge(const Group& group, const Event<E>& dep1, const Event<Us>& ... deps) -> Event<E>
{
using REACT_IMPL::EventMergeNode;
using REACT_IMPL::SameGroupOrLink;
using REACT_IMPL::CreateWrappedNode;
return CreateWrappedNode<Event<E>, EventMergeNode<E, E, Us ...>>(
group, SameGroupOrLink(group, dep1), SameGroupOrLink(group, deps) ...);
}
template <typename T = void, typename U1, typename ... Us>
static auto Merge(const Event<U1>& dep1, const Event<Us>& ... deps) -> decltype(auto)
{ return Merge(dep1.GetGroup(), dep1, deps ...); }
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Filter
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename F, typename E>
static auto Filter(const Group& group, F&& pred, const Event<E>& dep) -> Event<E>
{
auto filterFunc = [capturedPred = std::forward<F>(pred)] (const EventValueList<E>& events, EventValueSink<E> out)
{ std::copy_if(events.begin(), events.end(), out, capturedPred); };
return Event<E>::Create(group, std::move(filterFunc), dep);
}
template <typename F, typename E>
static auto Filter(F&& pred, const Event<E>& dep) -> Event<E>
{ return Filter(dep.GetGroup(), std::forward<F>(pred), dep); }
template <typename F, typename E, typename ... Ts>
static auto Filter(const Group& group, F&& pred, const Event<E>& dep, const State<Ts>& ... states) -> Event<E>
{
auto filterFunc = [capturedPred = std::forward<F>(pred)] (const EventValueList<E>& evts, EventValueSink<E> out, const Ts& ... values)
{
for (const auto& v : evts)
if (capturedPred(v, values ...))
*out++ = v;
};
return Event<E>::Create(group, std::move(filterFunc), dep, states ...);
}
template <typename F, typename E, typename ... Ts>
static auto Filter(F&& pred, const Event<E>& dep, const State<Ts>& ... states) -> Event<E>
{ return Filter(dep.GetGroup(), std::forward<F>(pred), dep, states ...); }
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Transform
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename E, typename F, typename T>
static auto Transform(const Group& group, F&& op, const Event<T>& dep) -> Event<E>
{
auto transformFunc = [capturedOp = std::forward<F>(op)] (const EventValueList<T>& evts, EventValueSink<E> out)
{ std::transform(evts.begin(), evts.end(), out, capturedOp); };
return Event<E>::Create(group, std::move(transformFunc), dep);
}
template <typename E, typename F, typename T>
static auto Transform(F&& op, const Event<T>& dep) -> Event<E>
{ return Transform<E>(dep.GetGroup(), std::forward<F>(op), dep); }
template <typename E, typename F, typename T, typename ... Us>
static auto Transform(const Group& group, F&& op, const Event<T>& dep, const State<Us>& ... states) -> Event<E>
{
auto transformFunc = [capturedOp = std::forward<F>(op)] (const EventValueList<T>& evts, EventValueSink<E> out, const Us& ... values)
{
for (const auto& v : evts)
*out++ = capturedOp(v, values ...);
};
return Event<E>::Create(group, std::move(transformFunc), dep, states ...);
}
template <typename E, typename F, typename T, typename ... Us>
static auto Transform(F&& op, const Event<T>& dep, const State<Us>& ... states) -> Event<E>
{ return Transform<E>(dep.GetGroup(), std::forward<F>(op), dep, states ...); }
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Join
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename U1, typename ... Us>
static auto Join(const Group& group, const Event<U1>& dep1, const Event<Us>& ... deps) -> Event<std::tuple<U1, Us ...>>
{
using REACT_IMPL::EventJoinNode;
using REACT_IMPL::SameGroupOrLink;
using REACT_IMPL::CreateWrappedNode;
static_assert(sizeof...(Us) > 0, "Join requires at least 2 inputs.");
return CreateWrappedNode<Event<std::tuple<U1, Us ...>>, EventJoinNode<U1, Us ...>>(
group, SameGroupOrLink(group, dep1), SameGroupOrLink(group, deps) ...);
}
template <typename U1, typename ... Us>
static auto Join(const Event<U1>& dep1, const Event<Us>& ... deps) -> Event<std::tuple<U1, Us ...>>
{ return Join(dep1.GetGroup(), dep1, deps ...); }
/******************************************/ REACT_END /******************************************/
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <typename E>
Event<E> SameGroupOrLink(const Group& targetGroup, const Event<E>& dep)
{
if (dep.GetGroup() == targetGroup)
return dep;
else
return EventLink<E>::Create(targetGroup, dep);
}
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_EVENT_H_INCLUDED