Skip to content

Commit 62b6228

Browse files
committed
Added EventToken as default value type for event streams (+ some refactoring).
1 parent 438639c commit 62b6228

9 files changed

Lines changed: 110 additions & 47 deletions

File tree

include/react/EventStream.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <vector>
44
#include <thread>
5+
#include <type_traits>
56

67
#include "ReactiveBase.h"
78
#include "ReactiveDomain.h"
@@ -13,13 +14,19 @@ namespace react {
1314
template <typename T>
1415
class Reactive;
1516

17+
enum class EventToken
18+
{
19+
token
20+
};
21+
22+
1623
////////////////////////////////////////////////////////////////////////////////////////
1724
/// REvents
1825
////////////////////////////////////////////////////////////////////////////////////////
1926
template
2027
<
2128
typename D,
22-
typename E
29+
typename E = EventToken
2330
>
2431
class REvents : public Reactive<EventStreamNode<D,E>>
2532
{
@@ -56,7 +63,7 @@ bool Equals(const REvents<D,L>& lhs, const REvents<D,R>& rhs)
5663
template
5764
<
5865
typename D,
59-
typename E
66+
typename E = EventToken
6067
>
6168
class REventSource : public REvents<D,E>
6269
{
@@ -74,7 +81,7 @@ class REventSource : public REvents<D,E>
7481
{
7582
}
7683

77-
REventSource& operator<<(const E& e)
84+
void Emit(const E& e) const
7885
{
7986
if (! Domain::TransactionInputContinuation::IsNull())
8087
{
@@ -93,6 +100,23 @@ class REventSource : public REvents<D,E>
93100
t.Data().Input().AddEventInput(*sourceNode, e);
94101
t.Commit();
95102
}
103+
}
104+
105+
template <typename = std::enable_if<std::is_same<E,EventToken>::value>::type>
106+
void Emit() const
107+
{
108+
Emit(EventToken::token);
109+
}
110+
111+
REventSource& operator<<(const E& e)
112+
{
113+
Emit(e);
114+
return *this;
115+
}
116+
117+
const REventSource& operator<<(const E& e) const
118+
{
119+
Emit(e);
96120
return *this;
97121
}
98122
};
@@ -108,6 +132,14 @@ inline auto MakeEventSource()
108132
std::make_shared<EventSourceNode<D,E>>(false));
109133
}
110134

135+
template <typename D>
136+
inline auto MakeEventSource()
137+
-> REventSource<D,EventToken>
138+
{
139+
return REventSource<D,EventToken>(
140+
std::make_shared<EventSourceNode<D,EventToken>>(false));
141+
}
142+
111143
////////////////////////////////////////////////////////////////////////////////////////
112144
/// Merge
113145
////////////////////////////////////////////////////////////////////////////////////////

include/react/Observer.h

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ namespace react {
1111
template <typename T>
1212
class Reactive;
1313

14-
template <typename D, typename TValue>
14+
template <typename D, typename S>
1515
class RSignal;
1616

17-
template <typename D, typename TValue>
17+
template <typename D, typename E>
1818
class REvents;
1919

20+
enum class EventToken;
21+
2022
////////////////////////////////////////////////////////////////////////////////////////
21-
/// Observer_
23+
/// RObserver
2224
////////////////////////////////////////////////////////////////////////////////////////
2325
template <typename D>
24-
class Observer_
26+
class RObserver
2527
{
2628
public:
2729
typedef NodeBase<D> SubjectT;
2830

29-
Observer_() :
31+
RObserver() :
3032
ptr_{ nullptr },
3133
subject_{ nullptr }
3234
{
3335
}
3436

35-
Observer_(ObserverNode<D>* ptr, const std::shared_ptr<SubjectT>& subject) :
37+
RObserver(ObserverNode<D>* ptr, const std::shared_ptr<SubjectT>& subject) :
3638
ptr_{ ptr },
3739
subject_{ subject }
3840
{
@@ -136,7 +138,7 @@ template
136138
typename TArg
137139
>
138140
inline auto Observe(const RSignal<D,TArg>& subject, const TFunc& func)
139-
-> Observer_<D>
141+
-> RObserver<D>
140142
{
141143
std::unique_ptr<ObserverNode<D>> pUnique(
142144
new SignalObserverNode<D,TArg>(subject.GetPtr(), func, false));
@@ -145,17 +147,19 @@ inline auto Observe(const RSignal<D,TArg>& subject, const TFunc& func)
145147

146148
D::Observers().Register(std::move(pUnique), subject.GetPtr().get());
147149

148-
return Observer_<D>(raw, subject.GetPtr());
150+
return RObserver<D>(raw, subject.GetPtr());
149151
}
150152

151153
template
152154
<
153155
typename D,
154156
typename TFunc,
155-
typename TArg
157+
typename TArg,
158+
typename = std::enable_if<
159+
! std::is_same<TArg,EventToken>::value>::type
156160
>
157161
inline auto Observe(const REvents<D,TArg>& subject, const TFunc& func)
158-
-> Observer_<D>
162+
-> RObserver<D>
159163
{
160164
std::unique_ptr<ObserverNode<D>> pUnique(
161165
new EventObserverNode<D,TArg>(subject.GetPtr(), func, false));
@@ -164,7 +168,26 @@ inline auto Observe(const REvents<D,TArg>& subject, const TFunc& func)
164168

165169
D::Observers().Register(std::move(pUnique), subject.GetPtr().get());
166170

167-
return Observer_<D>(raw, subject.GetPtr());
171+
return RObserver<D>(raw, subject.GetPtr());
172+
}
173+
174+
template
175+
<
176+
typename D,
177+
typename TFunc
178+
>
179+
inline auto Observe(const REvents<D,EventToken>& subject, const TFunc& func)
180+
-> RObserver<D>
181+
{
182+
std::unique_ptr<ObserverNode<D>> pUnique(
183+
new EventObserverNode<D,EventToken>(
184+
subject.GetPtr(), [func] (EventToken _) { func(); }, false));
185+
186+
auto* raw = pUnique.get();
187+
188+
D::Observers().Register(std::move(pUnique), subject.GetPtr().get());
189+
190+
return RObserver<D>(raw, subject.GetPtr());
168191
}
169192

170193
////////////////////////////////////////////////////////////////////////////////////////

include/react/ReactiveDomain.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class REvents;
2929
template <typename D, typename E>
3030
class REventSource;
3131

32+
enum class EventToken;
33+
3234
template
3335
<
3436
typename D,
@@ -425,13 +427,13 @@ class DomainBase
425427
template <typename S>
426428
using VarSignal = RVarSignal<D,S>;
427429

428-
template <typename E>
430+
template <typename E = EventToken>
429431
using Events = REvents<D,E>;
430432

431-
template <typename E>
433+
template <typename E = EventToken>
432434
using EventSource = REventSource<D,E>;
433435

434-
using Observer = Observer_<D>;
436+
using Observer = RObserver<D>;
435437

436438
////////////////////////////////////////////////////////////////////////////////////////
437439
/// ObserverRegistry
@@ -511,6 +513,12 @@ class DomainBase
511513
return react::MakeEventSource<D,E>();
512514
}
513515

516+
static inline auto MakeEventSource()
517+
-> EventSource<EventToken>
518+
{
519+
return react::MakeEventSource<D>();
520+
}
521+
514522
////////////////////////////////////////////////////////////////////////////////////////
515523
/// Aliases for transactions
516524
////////////////////////////////////////////////////////////////////////////////////////
@@ -535,7 +543,7 @@ class DomainBase
535543

536544
void Commit()
537545
{
538-
ASSERT_(committed_ == false, "Transaction already committed.");
546+
REACT_ASSERT(committed_ == false, "Transaction already committed.");
539547

540548
if (!committed_)
541549
{
@@ -577,16 +585,16 @@ class DomainBase
577585
ScopedTransaction() :
578586
transaction_{ defaultCommitFlags_ }
579587
{
580-
ASSERT_(ScopedTransactionInput::IsNull(), "Nested scoped transactions are not supported.");
581-
ASSERT_(TransactionInputContinuation::IsNull(), "Scoped transactions are not supported inside of observer functions.");
588+
REACT_ASSERT(ScopedTransactionInput::IsNull(), "Nested scoped transactions are not supported.");
589+
REACT_ASSERT(TransactionInputContinuation::IsNull(), "Scoped transactions are not supported inside of observer functions.");
582590
ScopedTransactionInput::Set(&transaction_.Data().Input());
583591
}
584592

585593
ScopedTransaction(int flags) :
586594
transaction_{ flags }
587595
{
588-
ASSERT_(ScopedTransactionInput::IsNull(), "Nested scoped transactions are not supported.");
589-
ASSERT_(TransactionInputContinuation::IsNull(), "Scoped transactions are not supported inside of observer functions.");
596+
REACT_ASSERT(ScopedTransactionInput::IsNull(), "Nested scoped transactions are not supported.");
597+
REACT_ASSERT(TransactionInputContinuation::IsNull(), "Scoped transactions are not supported inside of observer functions.");
590598
ScopedTransactionInput::Set(&transaction_.Data().Input());
591599
}
592600

include/react/ReactiveObject.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class ReactiveObject
1919
template <typename S>
2020
using VarSignal = RVarSignal<D,S>;
2121

22-
template <typename E>
22+
template <typename E = EventToken>
2323
using Events = REvents<D,E>;
2424

25-
template <typename E>
25+
template <typename E = EventToken>
2626
using EventSource = REventSource<D,E>;
2727

28-
using Observer = Observer_<D>;
28+
using Observer = RObserver<D>;
2929

3030
////////////////////////////////////////////////////////////////////////////////////////
3131
/// MakeVar (higher order signal)

include/react/common/Util.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
namespace react {
1515

1616
#ifdef _DEBUG
17-
#define MESSAGE_(...) printf(__VA_ARGS__ ## "\n")
17+
#define REACT_MESSAGE(...) printf(__VA_ARGS__ ## "\n")
1818
#else
1919
#define MESSAGE
2020
#endif
2121

2222
// Assert with message
23-
#define ASSERT_(condition, ...) for (; !(condition); assert(condition)) printf(__VA_ARGS__ ## "\n")
23+
#define REACT_ASSERT(condition, ...) for (; !(condition); assert(condition)) printf(__VA_ARGS__ ## "\n")
2424

2525
////////////////////////////////////////////////////////////////////////////////////////
2626
/// NonCopyable
@@ -101,7 +101,7 @@ inline void pass(TArgs&& ...) {}
101101

102102
// Expand args by wrapping them in a dummy function
103103
// Use comma operator to replace potential void return value with 0
104-
#define EXPAND_PACK(...) pass((__VA_ARGS__ , 0) ...)
104+
#define REACT_EXPAND_PACK(...) pass((__VA_ARGS__ , 0) ...)
105105

106106
////////////////////////////////////////////////////////////////////////////////////////
107107
/// Format print bits

include/react/graph/EventStreamNodes.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class EventSourceNode : public EventStreamNode<D,E>
105105

106106
virtual ETickResult Tick(void* turnPtr) override
107107
{
108-
ASSERT_(false, "Don't tick EventSourceNode\n");
108+
REACT_ASSERT(false, "Don't tick EventSourceNode\n");
109109
return ETickResult::none;
110110
}
111111

@@ -148,19 +148,19 @@ class EventMergeNode : public EventStreamNode<D, E>
148148
if (!registered)
149149
registerNode();
150150

151-
EXPAND_PACK(Engine::OnNodeAttach(*this, *args));
151+
REACT_EXPAND_PACK(Engine::OnNodeAttach(*this, *args));
152152
}
153153

154154
~EventMergeNode()
155155
{
156156
apply
157-
(
158-
[this](const EventStreamNodePtr<D, TArgs>& ... args)
159-
{
160-
EXPAND_PACK(Engine::OnNodeDetach(*this, *args));
161-
},
157+
(
158+
[this] (const EventStreamNodePtr<D, TArgs>& ... args)
159+
{
160+
REACT_EXPAND_PACK(Engine::OnNodeDetach(*this, *args));
161+
},
162162
deps_
163-
);
163+
);
164164
}
165165

166166
virtual const char* GetNodeType() const override { return "EventMergeNode"; }
@@ -190,15 +190,15 @@ class EventMergeNode : public EventStreamNode<D, E>
190190
}
191191
}
192192

193-
virtual int DependencyCount() const override { return sizeof ... (TArgs); }
193+
virtual int DependencyCount() const override { return sizeof... (TArgs); }
194194

195195
private:
196196
std::tuple<EventStreamNodePtr<D, TArgs> ...> deps_;
197-
std::function<void(const TurnInterface&)> func_;
197+
std::function<void(const TurnInterface&)> func_;
198198

199199
inline void expand(const TurnInterface& turn, const EventStreamNodePtr<D, TArgs>& ... args)
200200
{
201-
EXPAND_PACK(processArgs<TArgs>(turn, args));
201+
REACT_EXPAND_PACK(processArgs<TArgs>(turn, args));
202202
}
203203

204204
template <typename TArg>

0 commit comments

Comments
 (0)