Skip to content

Commit 73cb0ad

Browse files
committed
Misc cleanup.
1 parent bf989c5 commit 73cb0ad

11 files changed

Lines changed: 298 additions & 146 deletions

File tree

include/react/Algorithm.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ auto Fold(V&& init, const Events<D,E>& events, FIn&& func)
3636
{
3737
return Signal<D,S>(
3838
std::make_shared<REACT_IMPL::FoldNode<D,S,E,F>>(
39-
std::forward<V>(init), events.GetPtr(), std::forward<FIn>(func)));
39+
std::forward<V>(init), events.NodePtr(), std::forward<FIn>(func)));
4040
}
4141

4242
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -56,7 +56,7 @@ auto Iterate(V&& init, const Events<D,E>& events, FIn&& func)
5656
{
5757
return Signal<D,S>(
5858
std::make_shared<REACT_IMPL::IterateNode<D,S,E,F>>(
59-
std::forward<V>(init), events.GetPtr(), std::forward<FIn>(func)));
59+
std::forward<V>(init), events.NodePtr(), std::forward<FIn>(func)));
6060
}
6161

6262
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -73,7 +73,7 @@ auto Hold(V&& init, const Events<D,T>& events)
7373
{
7474
return Signal<D,T>(
7575
std::make_shared<REACT_IMPL::HoldNode<D,T>>(
76-
std::forward<V>(init), events.GetPtr()));
76+
std::forward<V>(init), events.NodePtr()));
7777
}
7878

7979
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -90,7 +90,7 @@ auto Snapshot(const Signal<D,S>& target, const Events<D,E>& trigger)
9090
{
9191
return Signal<D,S>(
9292
std::make_shared<REACT_IMPL::SnapshotNode<D,S,E>>(
93-
target.GetPtr(), trigger.GetPtr()));
93+
target.NodePtr(), trigger.NodePtr()));
9494
}
9595

9696
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -106,7 +106,7 @@ auto Monitor(const Signal<D,S>& target)
106106
{
107107
return Events<D,S>(
108108
std::make_shared<REACT_IMPL::MonitorNode<D, S>>(
109-
target.GetPtr()));
109+
target.NodePtr()));
110110
}
111111

112112
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -155,14 +155,14 @@ auto Pulse(const Signal<D,S>& target, const Events<D,E>& trigger)
155155
{
156156
return Events<D,S>(
157157
std::make_shared<REACT_IMPL::PulseNode<D,S,E>>(
158-
target.GetPtr(), trigger.GetPtr()));
158+
target.NodePtr(), trigger.NodePtr()));
159159
}
160160

161161
///////////////////////////////////////////////////////////////////////////////////////////////////
162162
/// Incrementer
163163
///////////////////////////////////////////////////////////////////////////////////////////////////
164164
template <typename T>
165-
struct Incrementer : public std::unary_function<T,T>
165+
struct Incrementer
166166
{
167167
T operator() (T v) const { return v+1; }
168168
};
@@ -171,7 +171,7 @@ struct Incrementer : public std::unary_function<T,T>
171171
/// Decrementer
172172
///////////////////////////////////////////////////////////////////////////////////////////////////
173173
template <typename T>
174-
struct Decrementer : public std::unary_function<T,T>
174+
struct Decrementer
175175
{
176176
T operator() (T v) const { return v-1; }
177177
};

include/react/Event.h

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,15 @@ class Events : public REACT_IMPL::EventStreamBase<D,E>
3838
using NodePtrT = REACT_IMPL::SharedPtrT<NodeT>;
3939

4040
public:
41+
using ValueT = E;
42+
4143
Events() = default;
4244
Events(const Events&) = default;
4345

46+
Events(Events&& other) :
47+
EventStreamBase{ std::move(other) }
48+
{}
49+
4450
explicit Events(NodePtrT&& nodePtr) :
4551
EventStreamBase{ std::move(nodePtr) }
4652
{}
@@ -64,19 +70,53 @@ class Events : public REACT_IMPL::EventStreamBase<D,E>
6470
}
6571
};
6672

67-
/******************************************/ REACT_END /******************************************/
73+
// Specialize for references
74+
template
75+
<
76+
typename D,
77+
typename E
78+
>
79+
class Events<D,E&> : public REACT_IMPL::EventStreamBase<D,std::reference_wrapper<E>>
80+
{
81+
protected:
82+
using BaseT = REACT_IMPL::EventStreamBase<D,std::reference_wrapper<E>>;
6883

69-
/***************************************/ REACT_IMPL_BEGIN /**************************************/
84+
private:
85+
using NodeT = REACT_IMPL::EventStreamNode<D,std::reference_wrapper<E>>;
86+
using NodePtrT = REACT_IMPL::SharedPtrT<NodeT>;
7087

71-
template <typename D, typename L, typename R>
72-
bool Equals(const Events<D,L>& lhs, const Events<D,R>& rhs)
73-
{
74-
return lhs.Equals(rhs);
75-
}
88+
public:
89+
using ValueT = std::reference_wrapper<E>;
7690

77-
/****************************************/ REACT_IMPL_END /***************************************/
91+
Events() = default;
92+
Events(const Events&) = default;
7893

79-
/*****************************************/ REACT_BEGIN /*****************************************/
94+
Events(Events&& other) :
95+
EventStreamBase{ std::move(other) }
96+
{}
97+
98+
explicit Events(NodePtrT&& nodePtr) :
99+
EventStreamBase{ std::move(nodePtr) }
100+
{}
101+
102+
template <typename F>
103+
Events Filter(F&& f)
104+
{
105+
return REACT::Filter(*this, std::forward<F>(f));
106+
}
107+
108+
template <typename F>
109+
Events Transform(F&& f)
110+
{
111+
return REACT::Transform(*this, std::forward<F>(f));
112+
}
113+
114+
template <typename F>
115+
Observer<D> Observe(F&& f)
116+
{
117+
return REACT::Observe(*this, std::forward<F>(f));
118+
}
119+
};
80120

81121
///////////////////////////////////////////////////////////////////////////////////////////////////
82122
/// EventSource
@@ -93,11 +133,13 @@ class EventSource : public Events<D,E>
93133
using NodePtrT = REACT_IMPL::SharedPtrT<NodeT>;
94134

95135
public:
96-
using ValueT = E;
97-
98136
EventSource() = default;
99137
EventSource(const EventSource&) = default;
100138

139+
EventSource(EventSource&& other) :
140+
Events{ std::move(other) }
141+
{}
142+
101143
explicit EventSource(NodePtrT&& nodePtr) :
102144
Events{ std::move(nodePtr) }
103145
{}
@@ -122,6 +164,43 @@ class EventSource : public Events<D,E>
122164
}
123165
};
124166

167+
// Specialize for references
168+
template
169+
<
170+
typename D,
171+
typename E
172+
>
173+
class EventSource<D,E&> : public Events<D,std::reference_wrapper<E>>
174+
{
175+
private:
176+
using NodeT = REACT_IMPL::EventSourceNode<D,std::reference_wrapper<E>>;
177+
using NodePtrT = REACT_IMPL::SharedPtrT<NodeT>;
178+
179+
public:
180+
EventSource() = default;
181+
EventSource(const EventSource&) = default;
182+
183+
EventSource(EventSource&& other) :
184+
Events{ std::move(other) }
185+
{}
186+
187+
explicit EventSource(NodePtrT&& nodePtr) :
188+
Events{ std::move(nodePtr) }
189+
{}
190+
191+
void Emit(std::reference_wrapper<E> e) const
192+
{
193+
BaseT::emit(e);
194+
}
195+
196+
template <typename T>
197+
const EventSource& operator<<(std::reference_wrapper<E> e) const
198+
{
199+
BaseT::emit(e);
200+
return *this;
201+
}
202+
};
203+
125204
///////////////////////////////////////////////////////////////////////////////////////////////////
126205
/// TempEvents
127206
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -141,6 +220,10 @@ class TempEvents : public Events<D,E>
141220
TempEvents() = default;
142221
TempEvents(const TempEvents&) = default;
143222

223+
TempEvents(TempEvents&& other) :
224+
Events{ std::move(other) }
225+
{}
226+
144227
explicit TempEvents(NodePtrT&& nodePtr) :
145228
Events{ std::move(nodePtr) }
146229
{}
@@ -151,6 +234,20 @@ class TempEvents : public Events<D,E>
151234
}
152235
};
153236

237+
/******************************************/ REACT_END /******************************************/
238+
239+
/***************************************/ REACT_IMPL_BEGIN /**************************************/
240+
241+
template <typename D, typename L, typename R>
242+
bool Equals(const Events<D,L>& lhs, const Events<D,R>& rhs)
243+
{
244+
return lhs.Equals(rhs);
245+
}
246+
247+
/****************************************/ REACT_IMPL_END /***************************************/
248+
249+
/*****************************************/ REACT_BEGIN /*****************************************/
250+
154251
///////////////////////////////////////////////////////////////////////////////////////////////////
155252
/// MakeEventSource
156253
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -191,7 +288,7 @@ auto Merge(const Events<D,TArg1>& arg1, const Events<D,TArgs>& ... args)
191288

192289
return TempEvents<D,E,TOp>(
193290
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
194-
arg1.GetPtr(), args.GetPtr() ...));
291+
arg1.NodePtr(), args.NodePtr() ...));
195292
}
196293

197294
template
@@ -215,7 +312,7 @@ auto operator|(const TLeftEvents& lhs, const TRightEvents& rhs)
215312
{
216313
return TempEvents<D,E,TOp>(
217314
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
218-
lhs.GetPtr(), rhs.GetPtr()));
315+
lhs.NodePtr(), rhs.NodePtr()));
219316
}
220317

221318
template
@@ -255,7 +352,7 @@ auto operator|(TempEvents<D,TLeftVal,TLeftOp>&& lhs, const TRightEvents& rhs)
255352
{
256353
return TempEvents<D,E,TOp>(
257354
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
258-
lhs.StealOp(), rhs.GetPtr()));
355+
lhs.StealOp(), rhs.NodePtr()));
259356
}
260357

261358
template
@@ -277,7 +374,7 @@ auto operator|(const TLeftEvents& lhs, TempEvents<D,TRightVal,TRightOp>&& rhs)
277374
{
278375
return TempEvents<D,E,TOp>(
279376
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
280-
lhs.GetPtr(), rhs.StealOp()));
377+
lhs.NodePtr(), rhs.StealOp()));
281378
}
282379

283380
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -297,7 +394,7 @@ auto Filter(const Events<D,E>& src, FIn&& filter)
297394
{
298395
return TempEvents<D,E,TOp>(
299396
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
300-
std::forward<FIn>(filter), src.GetPtr()));
397+
std::forward<FIn>(filter), src.NodePtr()));
301398
}
302399

303400
template
@@ -347,7 +444,7 @@ auto Transform(const Events<D,E>& src, FIn&& func)
347444
{
348445
return TempEvents<D,E,TOp>(
349446
std::make_shared<REACT_IMPL::EventOpNode<D,E,TOp>>(
350-
std::forward<FIn>(func), src.GetPtr()));
447+
std::forward<FIn>(func), src.NodePtr()));
351448
}
352449

353450
template
@@ -393,7 +490,7 @@ auto Flatten(const Signal<D,Events<D,TInnerValue>>& node)
393490
{
394491
return Events<D,TInnerValue>(
395492
std::make_shared<REACT_IMPL::EventFlattenNode<D, Events<D,TInnerValue>, TInnerValue>>(
396-
node.GetPtr(), node().GetPtr()));
493+
node.NodePtr(), node().NodePtr()));
397494
}
398495

399496
///////////////////////////////////////////////////////////////////////////////////////////////////
@@ -416,7 +513,7 @@ auto Observe(const Events<D,TArg>& subject, FIn&& func)
416513
auto* raw = REACT_IMPL::DomainSpecificObserverRegistry<D>::Instance().
417514
template Register<TNode>(subject, std::forward<FIn>(func));
418515

419-
return Observer<D>(raw, subject.GetPtr());
516+
return Observer<D>(raw, subject.NodePtr());
420517
}
421518

422519
template
@@ -434,7 +531,7 @@ auto Observe(const Events<D,EventToken>& subject, FIn&& func)
434531
auto* raw = REACT_IMPL::DomainSpecificObserverRegistry<D>::Instance().
435532
template Register<TNode>(subject, std::move(wrapper));
436533

437-
return Observer<D>(raw, subject.GetPtr());
534+
return Observer<D>(raw, subject.NodePtr());
438535
}
439536

440537
/******************************************/ REACT_END /******************************************/

include/react/Observer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void DetachAllObservers(const TSubject& subject)
6060
{
6161
using D = typename TSubject::DomainT;
6262
REACT_IMPL::DomainSpecificObserverRegistry<D>::Instance().UnregisterFrom(
63-
subject.GetPtr().get());
63+
subject.NodePtr().get());
6464
}
6565

6666
///////////////////////////////////////////////////////////////////////////////////////////////////

include/react/ReactiveObject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ class ReactiveObject
5959
return REACT::MakeVar<D>(std::forward<V>(value));
6060
}
6161

62+
template
63+
<
64+
typename S
65+
>
66+
static auto MakeVar(std::reference_wrapper<S> value)
67+
-> VarSignalT<S&>
68+
{
69+
return REACT::MakeVar<D>(value);
70+
}
71+
6272
//////////////////////////////////////////////////////////////////////////////////////////////////////////
6373
// MakeVar (higher order)
6474
//////////////////////////////////////////////////////////////////////////////////////////////////////////

include/react/Reactor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#pragma once
88

9-
#ifndef REACT_DISABLE_REACTORS
9+
#ifdef REACT_ENABLE_REACTORS
1010

1111
#include "react/detail/Defs.h"
1212

@@ -39,13 +39,13 @@ class ReactiveLoop
3939
template <typename E>
4040
E& Await(const Events<D,E>& evn)
4141
{
42-
return node_.Await<E>(evn.GetPtr());
42+
return node_.Await<E>(evn.NodePtr());
4343
}
4444

4545
template <typename E, typename F>
4646
void RepeatUntil(const Events<D,E>& evn, F func)
4747
{
48-
node_.RepeatUntil<E>(evn.GetPtr(), func);
48+
node_.RepeatUntil<E>(evn.NodePtr(), func);
4949
}
5050

5151
private:
@@ -65,4 +65,4 @@ class ReactiveLoop
6565

6666
/******************************************/ REACT_END /******************************************/
6767

68-
#endif //REACT_DISABLE_REACTORS
68+
#endif //REACT_ENABLE_REACTORS

0 commit comments

Comments
 (0)