signal: use more descriptive template parameter names
This commit is contained in:
parent
f58390de06
commit
fe031378fe
80
signal.hpp
80
signal.hpp
@ -3,11 +3,10 @@
|
||||
* 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/.
|
||||
*
|
||||
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
||||
* Copyright 2011-2019 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef CRUFT_UTIL_SIGNAL_HPP
|
||||
#define CRUFT_UTIL_SIGNAL_HPP
|
||||
#pragma once
|
||||
|
||||
#include "types/traits.hpp"
|
||||
#include "debug.hpp"
|
||||
@ -18,13 +17,13 @@
|
||||
#include <list>
|
||||
|
||||
namespace cruft {
|
||||
namespace combine {
|
||||
template <typename F>
|
||||
namespace reduce {
|
||||
template <typename FunctionT>
|
||||
struct logical_and {
|
||||
using R = typename func_traits<F>::return_type;
|
||||
using result = typename func_traits<FunctionT>::return_type;
|
||||
|
||||
template <typename T, typename ...Args>
|
||||
R operator() (T first, T last, Args&&... args)
|
||||
template <typename InputT, typename ...Args>
|
||||
result operator() (InputT first, InputT last, Args&&... args)
|
||||
{
|
||||
while (first != last)
|
||||
if (!(*first++)(args...))
|
||||
@ -34,12 +33,12 @@ namespace cruft {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
template <typename FunctionT>
|
||||
struct logical_or {
|
||||
using R = typename func_traits<F>::return_type;
|
||||
using result = typename func_traits<FunctionT>::return_type;
|
||||
|
||||
template <typename T, typename ...Args>
|
||||
R operator() (T first, T last, Args&&... args)
|
||||
template <typename InputT, typename ...Args>
|
||||
result operator() (InputT first, InputT last, Args&&... args)
|
||||
{
|
||||
while (first != last)
|
||||
if ((*first++)(args...))
|
||||
@ -49,12 +48,12 @@ namespace cruft {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
template <typename FunctionT>
|
||||
struct noop {
|
||||
using R = void;
|
||||
using result = void;
|
||||
|
||||
template <typename T, typename ...Args>
|
||||
R operator() (T first, T last, Args&&... args)
|
||||
template <typename InputT, typename ...Args>
|
||||
result operator() (InputT first, InputT last, Args&&... args)
|
||||
{
|
||||
while (first != last) {
|
||||
(*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 {
|
||||
public:
|
||||
using R = typename C<F>::R;
|
||||
using callback = std::function<F>;
|
||||
using result = typename ReductionT<FunctionT>::result;
|
||||
using callback = std::function<FunctionT>;
|
||||
|
||||
struct cookie;
|
||||
|
||||
@ -127,18 +129,18 @@ namespace cruft {
|
||||
}
|
||||
|
||||
/// Execute all callbacks
|
||||
template <typename ...Args>
|
||||
R
|
||||
operator() (Args&&... tail)
|
||||
template <typename ...ArgsT>
|
||||
result
|
||||
operator() (ArgsT&&... tail)
|
||||
{
|
||||
if (m_children.empty ())
|
||||
return R();
|
||||
return result();
|
||||
|
||||
C<F> combiner;
|
||||
return combiner (
|
||||
ReductionT<FunctionT> reducer;
|
||||
return reducer (
|
||||
m_children.begin (),
|
||||
m_children.end (),
|
||||
std::forward<Args> (tail)...
|
||||
std::forward<ArgsT> (tail)...
|
||||
);
|
||||
}
|
||||
|
||||
@ -149,10 +151,10 @@ namespace cruft {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
template <typename F, template <typename> class C>
|
||||
struct signal<F,C>::cookie : public nocopy {
|
||||
template <typename FunctionT, template <typename> class ReductionT>
|
||||
struct signal<FunctionT,ReductionT>::cookie : public nocopy {
|
||||
cookie (typename group::iterator _position,
|
||||
signal<F,C> &_parent):
|
||||
signal<FunctionT,ReductionT> &_parent):
|
||||
m_position (_position),
|
||||
m_parent (_parent)
|
||||
{ ; }
|
||||
@ -176,24 +178,24 @@ namespace cruft {
|
||||
}
|
||||
|
||||
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
|
||||
//template <typename T, template <typename> class C>
|
||||
template <typename T>
|
||||
class value_signal : public signal<void(T)> {
|
||||
//template <typename T, template <typename> class ReductionT>
|
||||
template <typename ValueT>
|
||||
class value_signal : public signal<void(ValueT)> {
|
||||
public:
|
||||
explicit value_signal (T t): m_value (t) { ; }
|
||||
explicit value_signal (ValueT _value): m_value (_value) { ; }
|
||||
value_signal () = default;
|
||||
|
||||
operator const T&() const { return m_value; }
|
||||
operator const ValueT&() const { return m_value; }
|
||||
|
||||
value_signal<T>&
|
||||
operator= (const T &t)
|
||||
value_signal<ValueT>&
|
||||
operator= (const ValueT &t)
|
||||
{
|
||||
m_value = t;
|
||||
(*this) (m_value);
|
||||
@ -201,8 +203,6 @@ namespace cruft {
|
||||
}
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
ValueT m_value;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -75,7 +75,7 @@ void
|
||||
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;
|
||||
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;
|
||||
auto cookie0 = sig.connect ([&] (void) { ++count; return true; });
|
||||
|
Loading…
Reference in New Issue
Block a user