Skip to content

Commit 4a51f24

Browse files
committed
Added member variant of react::Merge and replaced universal reference in some signatures with explicit reference types.
1 parent 93680f4 commit 4a51f24

2 files changed

Lines changed: 56 additions & 25 deletions

File tree

‎include/react/Event.h‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ class Events<D,E&> : public REACT_IMPL::EventStreamBase<D,std::reference_wrapper
102102
EventStreamBase{ std::move(nodePtr) }
103103
{}
104104

105+
template <typename ... TArgs>
106+
auto Merge(const Events<D,TArgs>& ... args)
107+
-> decltype(REACT::Merge(std::declval<Events>(), std::forward<TArgs>(args) ...))
108+
{
109+
return REACT::Merge(*this, std::forward<TArgs>(args) ...);
110+
}
111+
105112
template <typename F>
106113
auto Filter(F&& f) const
107114
-> decltype(REACT::Filter(std::declval<Events>(), std::forward<F>(f)))
@@ -149,10 +156,14 @@ class EventSource : public Events<D,E>
149156
Events{ std::move(nodePtr) }
150157
{}
151158

152-
template <typename T>
153-
void Emit(T&& e) const
159+
void Emit(const E& e) const
160+
{
161+
BaseT::emit(e);
162+
}
163+
164+
void Emit(E&& e) const
154165
{
155-
BaseT::emit(std::forward<T>(e));
166+
BaseT::emit(std::move(e));
156167
}
157168

158169
template <class = std::enable_if<std::is_same<E,EventToken>::value>::type>
@@ -161,10 +172,15 @@ class EventSource : public Events<D,E>
161172
BaseT::emit(EventToken::token);
162173
}
163174

164-
template <typename T>
165-
const EventSource& operator<<(T&& e) const
175+
const EventSource& operator<<(const E& e) const
176+
{
177+
BaseT::emit(e);
178+
return *this;
179+
}
180+
181+
const EventSource& operator<<(E&& e) const
166182
{
167-
BaseT::emit(std::forward<T>(e));
183+
BaseT::emit(std::move(e));
168184
return *this;
169185
}
170186
};
@@ -198,7 +214,6 @@ class EventSource<D,E&> : public Events<D,std::reference_wrapper<E>>
198214
BaseT::emit(e);
199215
}
200216

201-
template <typename T>
202217
const EventSource& operator<<(std::reference_wrapper<E> e) const
203218
{
204219
BaseT::emit(e);
@@ -238,6 +253,13 @@ class TempEvents : public Events<D,E>
238253
return std::move(std::static_pointer_cast<NodeT>(ptr_)->StealOp());
239254
}
240255

256+
template <typename ... TArgs>
257+
auto Merge(const Events<D,TArgs>& ... args)
258+
-> decltype(REACT::Merge(std::declval<TempEvents>(), std::forward<TArgs>(args) ...))
259+
{
260+
return REACT::Merge(*this, std::forward<TArgs>(args) ...);
261+
}
262+
241263
template <typename F>
242264
auto Filter(F&& f) const
243265
-> decltype(REACT::Filter(std::declval<TempEvents>(), std::forward<F>(f)))
@@ -521,15 +543,15 @@ template
521543
<
522544
typename D,
523545
typename FIn,
524-
typename TArg,
546+
typename E,
525547
class = std::enable_if<
526-
! std::is_same<TArg,EventToken>::value>::type
548+
! std::is_same<E,EventToken>::value>::type
527549
>
528-
auto Observe(const Events<D,TArg>& subject, FIn&& func)
550+
auto Observe(const Events<D,E>& subject, FIn&& func)
529551
-> Observer<D>
530552
{
531553
using F = std::decay<FIn>::type;
532-
using TNode = REACT_IMPL::EventObserverNode<D,TArg,F>;
554+
using TNode = REACT_IMPL::EventObserverNode<D,E,F>;
533555

534556
auto* raw = REACT_IMPL::DomainSpecificObserverRegistry<D>::Instance().
535557
template Register<TNode>(subject, std::forward<FIn>(func));
@@ -545,7 +567,7 @@ template
545567
auto Observe(const Events<D,EventToken>& subject, FIn&& func)
546568
-> Observer<D>
547569
{
548-
auto wrapper = [func] (EventToken _) { func(); };
570+
auto wrapper = [func] (EventToken) { func(); };
549571

550572
using TNode = REACT_IMPL::EventObserverNode<D,EventToken,decltype(wrapper)>;
551573

‎include/react/Signal.h‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Signal : public REACT_IMPL::SignalBase<D,S>
4040
using ValueT = S;
4141

4242
Signal() = default;
43-
Signal(const Signal& other) = default;
43+
Signal(const Signal&) = default;
4444

4545
Signal(Signal&& other) :
4646
SignalBase{ std::move(other) }
@@ -59,7 +59,7 @@ class Signal : public REACT_IMPL::SignalBase<D,S>
5959
return REACT::Observe(*this, std::forward<F>(f));
6060
}
6161

62-
template <class = std::enable_if<IsSignal<S>::value>::type>
62+
template <class = std::enable_if<IsReactive<S>::value>::type>
6363
S Flatten() const
6464
{
6565
return REACT::Flatten(*this);
@@ -131,16 +131,25 @@ class VarSignal : public Signal<D,S>
131131
Signal{ std::move(nodePtr) }
132132
{}
133133

134-
template <typename T>
135-
void Set(T&& newValue) const
134+
void Set(const S& newValue) const
135+
{
136+
BaseT::setValue(newValue);
137+
}
138+
139+
void Set(S&& newValue) const
136140
{
137-
BaseT::setValue(std::forward<T>(newValue));
141+
BaseT::setValue(std::move(newValue));
142+
}
143+
144+
const VarSignal& operator<<=(const S& newValue) const
145+
{
146+
BaseT::setValue(newValue);
147+
return *this;
138148
}
139149

140-
template <typename T>
141-
const VarSignal& operator<<=(T&& newValue) const
150+
const VarSignal& operator<<=(S&& newValue) const
142151
{
143-
BaseT::setValue(std::forward<T>(newValue));
152+
BaseT::setValue(std::move(newValue));
144153
return *this;
145154
}
146155
};
@@ -305,15 +314,15 @@ template
305314
<
306315
typename D,
307316
typename FIn,
308-
typename ... TArgs,
317+
typename ... TValues,
309318
typename F = std::decay<FIn>::type,
310-
typename S = std::result_of<F(TArgs...)>::type,
311-
typename TOp = REACT_IMPL::FunctionOp<S,F, REACT_IMPL::SignalNodePtrT<D,TArgs> ...>
319+
typename S = std::result_of<F(TValues...)>::type,
320+
typename TOp = REACT_IMPL::FunctionOp<S,F, REACT_IMPL::SignalNodePtrT<D,TValues> ...>
312321
>
313-
auto MakeSignal(FIn&& func, const Signal<D,TArgs>& ... args)
322+
auto MakeSignal(FIn&& func, const Signal<D,TValues>& ... args)
314323
-> TempSignal<D,S,TOp>
315324
{
316-
static_assert(sizeof...(TArgs) > 0,
325+
static_assert(sizeof...(TValues) > 0,
317326
"react::MakeSignal requires at least 1 signal dependency.");
318327

319328
return TempSignal<D,S,TOp>(

0 commit comments

Comments
 (0)