1313#include < utility>
1414#include < vector>
1515
16- #include " Observer.h"
17- #include " react/detail/ReactiveBase.h"
18- #include " react/detail/ReactiveDomain.h"
19- #include " react/detail/ReactiveInput.h"
20- #include " react/detail/graph/EventStreamNodes.h"
16+ #include " react/Observer.h"
17+ #include " react/detail/EventBase.h"
2118
2219/* ****************************************/ REACT_BEGIN /* ****************************************/
2320
@@ -31,38 +28,39 @@ template
3128 typename D,
3229 typename E = EventToken
3330>
34- class Events : public Reactive < REACT_IMPL ::EventStreamNode <D,E> >
31+ class Events : public REACT_IMPL ::EventStreamBase <D,E>
3532{
3633protected:
37- using NodeT = REACT_IMPL ::EventStreamNode <D, E>;
34+ using BaseT = REACT_IMPL ::EventStreamBase <D,E>;
3835
39- public:
40- using ValueT = E;
36+ private:
37+ using NodeT = REACT_IMPL ::EventStreamNode<D,E>;
38+ using NodePtrT = REACT_IMPL ::SharedPtrT<NodeT>;
4139
42- Events () :
43- Reactive ()
44- {}
40+ public :
41+ Events () = default ;
42+ Events ( const Events&) = default ;
4543
46- explicit Events (const std::shared_ptr<NodeT>& ptr ) :
47- Reactive(ptr)
44+ explicit Events (NodePtrT&& nodePtr ) :
45+ EventStreamBase{ std::move (nodePtr) }
4846 {}
4947
5048 template <typename F>
5149 Events Filter (F&& f)
5250 {
53- return react ::Filter (*this , std::forward<F>(f));
51+ return REACT ::Filter (*this , std::forward<F>(f));
5452 }
5553
5654 template <typename F>
5755 Events Transform (F&& f)
5856 {
59- return react ::Transform (*this , std::forward<F>(f));
57+ return REACT ::Transform (*this , std::forward<F>(f));
6058 }
6159
6260 template <typename F>
6361 Observer<D> Observe (F&& f)
6462 {
65- return react ::Observe (*this , std::forward<F>(f));
63+ return REACT ::Observe (*this , std::forward<F>(f));
6664 }
6765};
6866
@@ -81,7 +79,7 @@ bool Equals(const Events<D,L>& lhs, const Events<D,R>& rhs)
8179/* ****************************************/ REACT_BEGIN /* ****************************************/
8280
8381// /////////////////////////////////////////////////////////////////////////////////////////////////
84- // / Eventsource
82+ // / EventSource
8583// /////////////////////////////////////////////////////////////////////////////////////////////////
8684template
8785<
@@ -91,33 +89,35 @@ template
9189class EventSource : public Events <D,E>
9290{
9391private:
94- using NodeT = REACT_IMPL ::EventSourceNode<D, E>;
92+ using NodeT = REACT_IMPL ::EventSourceNode<D,E>;
93+ using NodePtrT = REACT_IMPL ::SharedPtrT<NodeT>;
9594
9695public:
97- EventSource () :
98- Events ()
99- {}
96+ using ValueT = E;
97+
98+ EventSource () = default ;
99+ EventSource (const EventSource&) = default ;
100100
101- explicit EventSource (const std::shared_ptr<NodeT>& ptr ) :
102- Events(ptr)
101+ explicit EventSource (NodePtrT&& nodePtr ) :
102+ Events{ std::move (nodePtr) }
103103 {}
104104
105- template <typename V >
106- void Emit (V && v ) const
105+ template <typename T >
106+ void Emit (T && e ) const
107107 {
108- REACT_IMPL ::InputManager<D>::AddInput (
109- *std::static_pointer_cast<NodeT>(ptr_), std::forward<V>(v));
108+ BaseT::emit (std::forward<T>(e));
110109 }
111110
112- template <typename = std::enable_if<std::is_same<E,EventToken>::value>::type>
111+ template <class = std::enable_if<std::is_same<E,EventToken>::value>::type>
113112 void Emit () const
114113 {
115- Emit (EventToken::token);
114+ BaseT::emit (EventToken::token);
116115 }
117116
118- const EventSource& operator <<(const E& e) const
117+ template <typename T>
118+ const EventSource& operator <<(T&& e) const
119119 {
120- Emit (e );
120+ BaseT::emit (std::forward<T>(e) );
121121 return *this ;
122122 }
123123};
@@ -134,19 +134,15 @@ template
134134class TempEvents : public Events <D,E>
135135{
136136protected:
137- using NodeT = REACT_IMPL ::EventOpNode<D,E,TOp>;
137+ using NodeT = REACT_IMPL ::EventOpNode<D,E,TOp>;
138+ using NodePtrT = REACT_IMPL ::SharedPtrT<NodeT>;
138139
139140public:
140- TempEvents () :
141- Events ()
142- {}
143-
144- explicit TempEvents (const std::shared_ptr<NodeT>& ptr) :
145- Events(ptr)
146- {}
141+ TempEvents () = default ;
142+ TempEvents (const TempEvents&) = default ;
147143
148- explicit TempEvents (std::shared_ptr<NodeT> && ptr ) :
149- Events( std::move(ptr))
144+ explicit TempEvents (NodePtrT && nodePtr ) :
145+ Events{ std::move (nodePtr) }
150146 {}
151147
152148 TOp StealOp ()
@@ -184,8 +180,8 @@ template
184180 typename ... TArgs,
185181 typename E = TArg1,
186182 typename TOp = REACT_IMPL ::EventMergeOp<E,
187- REACT_IMPL ::EventStreamNodePtr <D,TArg1>,
188- REACT_IMPL ::EventStreamNodePtr <D,TArgs> ...>
183+ REACT_IMPL ::EventStreamNodePtrT <D,TArg1>,
184+ REACT_IMPL ::EventStreamNodePtrT <D,TArgs> ...>
189185>
190186auto Merge (const Events<D,TArg1>& arg1, const Events<D,TArgs>& ... args)
191187 -> TempEvents<D,E,TOp>
@@ -207,8 +203,8 @@ template
207203 typename TRightVal = TRightEvents::ValueT,
208204 typename E = TLeftVal,
209205 typename TOp = REACT_IMPL ::EventMergeOp<E,
210- REACT_IMPL ::EventStreamNodePtr <D,TLeftVal>,
211- REACT_IMPL ::EventStreamNodePtr <D,TRightVal>>,
206+ REACT_IMPL ::EventStreamNodePtrT <D,TLeftVal>,
207+ REACT_IMPL ::EventStreamNodePtrT <D,TRightVal>>,
212208 class = std::enable_if<
213209 IsEvent<TLeftEvents>::value>::type,
214210 class = std::enable_if<
@@ -250,7 +246,7 @@ template
250246 typename E = TLeftVal,
251247 typename TOp = REACT_IMPL ::EventMergeOp<E,
252248 TLeftOp,
253- REACT_IMPL ::EventStreamNodePtr <D,TRightVal>>,
249+ REACT_IMPL ::EventStreamNodePtrT <D,TRightVal>>,
254250 class = std::enable_if<
255251 IsEvent<TRightEvents>::value>::type
256252>
@@ -271,7 +267,7 @@ template
271267 typename TLeftVal = TLeftEvents::ValueT,
272268 typename E = TLeftVal,
273269 typename TOp = REACT_IMPL ::EventMergeOp<E,
274- REACT_IMPL ::EventStreamNodePtr <D,TRightVal>,
270+ REACT_IMPL ::EventStreamNodePtrT <D,TRightVal>,
275271 TRightOp>,
276272 class = std::enable_if<
277273 IsEvent<TLeftEvents>::value>::type
@@ -294,7 +290,7 @@ template
294290 typename FIn,
295291 typename F = std::decay<FIn>::type,
296292 typename TOp = REACT_IMPL ::EventFilterOp<E,F,
297- REACT_IMPL ::EventStreamNodePtr <D,E>>
293+ REACT_IMPL ::EventStreamNodePtrT <D,E>>
298294>
299295auto Filter (const Events<D,E>& src, FIn&& filter)
300296 -> TempEvents<D,E,TOp>
@@ -344,7 +340,7 @@ template
344340 typename FIn,
345341 typename F = std::decay<FIn>::type,
346342 typename TOp = REACT_IMPL ::EventTransformOp<E,F,
347- REACT_IMPL ::EventStreamNodePtr <D,E>>
343+ REACT_IMPL ::EventStreamNodePtrT <D,E>>
348344>
349345auto Transform (const Events<D,E>& src, FIn&& func)
350346 -> TempEvents<D,E,TOp>
@@ -384,4 +380,61 @@ auto operator->*(TEvents&& src, F&& func)
384380 return Transform (std::forward<TEvents>(src), std::forward<F>(func));
385381}
386382
383+ // /////////////////////////////////////////////////////////////////////////////////////////////////
384+ // / Flatten
385+ // /////////////////////////////////////////////////////////////////////////////////////////////////
386+ template
387+ <
388+ typename D,
389+ typename TInnerValue
390+ >
391+ auto Flatten (const Signal<D,Events<D,TInnerValue>>& node)
392+ -> Events<D,TInnerValue>
393+ {
394+ return Events<D,TInnerValue>(
395+ std::make_shared<REACT_IMPL ::EventFlattenNode<D, Events<D,TInnerValue>, TInnerValue>>(
396+ node.GetPtr (), node ().GetPtr ()));
397+ }
398+
399+ // /////////////////////////////////////////////////////////////////////////////////////////////////
400+ // / Observe
401+ // /////////////////////////////////////////////////////////////////////////////////////////////////
402+ template
403+ <
404+ typename D,
405+ typename FIn,
406+ typename TArg,
407+ class = std::enable_if<
408+ ! std::is_same<TArg,EventToken>::value>::type
409+ >
410+ auto Observe (const Events<D,TArg>& subject, FIn&& func)
411+ -> Observer<D>
412+ {
413+ using F = std::decay<FIn>::type;
414+ using TNode = REACT_IMPL ::EventObserverNode<D,TArg,F>;
415+
416+ auto * raw = REACT_IMPL ::DomainSpecificObserverRegistry<D>::Instance ().
417+ template Register<TNode>(subject, std::forward<FIn>(func));
418+
419+ return Observer<D>(raw, subject.GetPtr ());
420+ }
421+
422+ template
423+ <
424+ typename D,
425+ typename FIn
426+ >
427+ auto Observe (const Events<D,EventToken>& subject, FIn&& func)
428+ -> Observer<D>
429+ {
430+ auto wrapper = [func] (EventToken _) { func (); };
431+
432+ using TNode = REACT_IMPL ::EventObserverNode<D,EventToken,decltype (wrapper)>;
433+
434+ auto * raw = REACT_IMPL ::DomainSpecificObserverRegistry<D>::Instance ().
435+ template Register<TNode>(subject, std::move (wrapper));
436+
437+ return Observer<D>(raw, subject.GetPtr ());
438+ }
439+
387440/* *****************************************/ REACT_END /* *****************************************/
0 commit comments