signal: use more descriptive template parameter names

This commit is contained in:
Danny Robson 2019-03-12 15:38:42 +11:00
parent f58390de06
commit fe031378fe
2 changed files with 42 additions and 42 deletions

View File

@ -3,11 +3,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net> * Copyright 2011-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_SIGNAL_HPP #pragma once
#define CRUFT_UTIL_SIGNAL_HPP
#include "types/traits.hpp" #include "types/traits.hpp"
#include "debug.hpp" #include "debug.hpp"
@ -18,13 +17,13 @@
#include <list> #include <list>
namespace cruft { namespace cruft {
namespace combine { namespace reduce {
template <typename F> template <typename FunctionT>
struct logical_and { struct logical_and {
using R = typename func_traits<F>::return_type; using result = typename func_traits<FunctionT>::return_type;
template <typename T, typename ...Args> template <typename InputT, typename ...Args>
R operator() (T first, T last, Args&&... args) result operator() (InputT first, InputT last, Args&&... args)
{ {
while (first != last) while (first != last)
if (!(*first++)(args...)) if (!(*first++)(args...))
@ -34,12 +33,12 @@ namespace cruft {
} }
}; };
template <typename F> template <typename FunctionT>
struct logical_or { struct logical_or {
using R = typename func_traits<F>::return_type; using result = typename func_traits<FunctionT>::return_type;
template <typename T, typename ...Args> template <typename InputT, typename ...Args>
R operator() (T first, T last, Args&&... args) result operator() (InputT first, InputT last, Args&&... args)
{ {
while (first != last) while (first != last)
if ((*first++)(args...)) if ((*first++)(args...))
@ -49,12 +48,12 @@ namespace cruft {
} }
}; };
template <typename F> template <typename FunctionT>
struct noop { struct noop {
using R = void; using result = void;
template <typename T, typename ...Args> template <typename InputT, typename ...Args>
R operator() (T first, T last, Args&&... args) result operator() (InputT first, InputT last, Args&&... args)
{ {
while (first != last) { while (first != last) {
(*first++)(args...); (*first++)(args...);
@ -63,11 +62,14 @@ namespace cruft {
}; };
} }
template <typename F, template <typename> class C = combine::noop> template <
typename FunctionT,
template <typename> class ReductionT = reduce::noop
>
class signal { class signal {
public: public:
using R = typename C<F>::R; using result = typename ReductionT<FunctionT>::result;
using callback = std::function<F>; using callback = std::function<FunctionT>;
struct cookie; struct cookie;
@ -127,18 +129,18 @@ namespace cruft {
} }
/// Execute all callbacks /// Execute all callbacks
template <typename ...Args> template <typename ...ArgsT>
R result
operator() (Args&&... tail) operator() (ArgsT&&... tail)
{ {
if (m_children.empty ()) if (m_children.empty ())
return R(); return result();
C<F> combiner; ReductionT<FunctionT> reducer;
return combiner ( return reducer (
m_children.begin (), m_children.begin (),
m_children.end (), m_children.end (),
std::forward<Args> (tail)... std::forward<ArgsT> (tail)...
); );
} }
@ -149,10 +151,10 @@ namespace cruft {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template <typename F, template <typename> class C> template <typename FunctionT, template <typename> class ReductionT>
struct signal<F,C>::cookie : public nocopy { struct signal<FunctionT,ReductionT>::cookie : public nocopy {
cookie (typename group::iterator _position, cookie (typename group::iterator _position,
signal<F,C> &_parent): signal<FunctionT,ReductionT> &_parent):
m_position (_position), m_position (_position),
m_parent (_parent) m_parent (_parent)
{ ; } { ; }
@ -176,24 +178,24 @@ namespace cruft {
} }
typename group::iterator m_position; typename group::iterator m_position;
signal<F,C> &m_parent; signal<FunctionT,ReductionT> &m_parent;
}; };
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// wrap a value in a signal and trigger on assignment // wrap a value in a signal and trigger on assignment
//template <typename T, template <typename> class C> //template <typename T, template <typename> class ReductionT>
template <typename T> template <typename ValueT>
class value_signal : public signal<void(T)> { class value_signal : public signal<void(ValueT)> {
public: public:
explicit value_signal (T t): m_value (t) { ; } explicit value_signal (ValueT _value): m_value (_value) { ; }
value_signal () = default; value_signal () = default;
operator const T&() const { return m_value; } operator const ValueT&() const { return m_value; }
value_signal<T>& value_signal<ValueT>&
operator= (const T &t) operator= (const ValueT &t)
{ {
m_value = t; m_value = t;
(*this) (m_value); (*this) (m_value);
@ -201,8 +203,6 @@ namespace cruft {
} }
private: private:
T m_value; ValueT m_value;
}; };
} }
#endif

View File

@ -75,7 +75,7 @@ void
test_combiner (cruft::TAP::logger &tap) test_combiner (cruft::TAP::logger &tap)
{ {
{ {
cruft::signal<bool(void), cruft::combine::logical_and> sig; cruft::signal<bool(void), cruft::reduce::logical_and> sig;
unsigned count = 0; unsigned count = 0;
auto cookie0 = sig.connect ([&] (void) { ++count; return true; }); auto cookie0 = sig.connect ([&] (void) { ++count; return true; });
@ -87,7 +87,7 @@ test_combiner (cruft::TAP::logger &tap)
} }
{ {
cruft::signal<bool(void), cruft::combine::logical_and> sig; cruft::signal<bool(void), cruft::reduce::logical_and> sig;
unsigned count = 0; unsigned count = 0;
auto cookie0 = sig.connect ([&] (void) { ++count; return true; }); auto cookie0 = sig.connect ([&] (void) { ++count; return true; });