signal: reduce templating complexity

This commit is contained in:
Danny Robson 2019-03-13 12:08:57 +11:00
parent fe031378fe
commit b1076d07ce
3 changed files with 84 additions and 87 deletions

View File

@ -8,6 +8,3 @@
#include "signal.hpp"
// Instance something probably useful here so that we generate early compile/link errors.
template class cruft::signal <void(void)>;

View File

@ -18,45 +18,41 @@
namespace cruft {
namespace reduce {
template <typename FunctionT>
/// Returns the short-circuited logical-and of the results of all
/// connected callbacks.
struct logical_and {
using result = typename func_traits<FunctionT>::return_type;
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
decltype(auto)
operator() (InputT first, InputT last, Args&&... args)
{
while (first != last)
if (!(*first++)(args...))
if (!(std::invoke (*first++, args...)))
return false;
return true;
}
};
template <typename FunctionT>
/// Returns the short-circuited logical-or of the results of all
/// connected callbacks.
struct logical_or {
using result = typename func_traits<FunctionT>::return_type;
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
decltype(auto)
operator() (InputT first, InputT last, Args&&... args)
{
while (first != last)
if ((*first++)(args...))
if (std::invoke (*first++, args...))
return true;
return false;
}
};
template <typename FunctionT>
/// Unconditionally evaluates all connected callbacks. Returns nothing.
struct noop {
using result = void;
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
void operator() (InputT first, InputT last, Args&&... args)
{
while (first != last) {
(*first++)(args...);
std::invoke (*first++, args...);
}
}
};
@ -64,14 +60,57 @@ namespace cruft {
template <
typename FunctionT,
template <typename> class ReductionT = reduce::noop
typename ReductionT = reduce::noop
>
class signal {
public:
using result = typename ReductionT<FunctionT>::result;
using callback = std::function<FunctionT>;
using reduction_type = ReductionT;
using function_type = FunctionT;
struct cookie;
typedef std::list<FunctionT> group;
///////////////////////////////////////////////////////////////////////
struct cookie {
cookie (cookie const&) = delete;
cookie& operator= (cookie const&) = delete;
cookie (typename group::iterator _position,
signal<FunctionT,ReductionT> &_parent):
m_position (_position),
m_parent (_parent)
{ ; }
cookie (cookie &&rhs) noexcept:
m_position (rhs.m_position),
m_parent (rhs.m_parent)
{
rhs.m_position = rhs.m_parent.m_children.end ();
}
cookie& operator= (cookie &&rhs) noexcept
{
CHECK_EQ (&m_parent, &rhs.m_parent);
std::swap (m_position, rhs.m_position);
return *this;
}
~cookie ()
{
if (m_parent.m_children.end () != m_position)
m_parent.disconnect (*this);
}
void reset (FunctionT &&cb)
{
*m_position = std::move (cb);
}
typename group::iterator m_position;
signal<FunctionT,ReductionT> &m_parent;
};
public:
signal () = default;
@ -81,7 +120,7 @@ namespace cruft {
}
/// Add a callback to list.
cookie connect [[nodiscard]] (callback &&_cb)
cookie connect [[nodiscard]] (FunctionT &&_cb)
{
return cookie (
m_children.insert (
@ -92,17 +131,15 @@ namespace cruft {
);
}
cookie connect [[nodiscard]] (const callback &_cb)
cookie connect [[nodiscard]] (FunctionT const &_cb)
{
{
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
void disconnect (cookie &c)
@ -130,14 +167,10 @@ namespace cruft {
/// Execute all callbacks
template <typename ...ArgsT>
result
decltype(auto)
operator() (ArgsT&&... tail)
{
if (m_children.empty ())
return result();
ReductionT<FunctionT> reducer;
return reducer (
return ReductionT {} (
m_children.begin (),
m_children.end (),
std::forward<ArgsT> (tail)...
@ -145,49 +178,15 @@ namespace cruft {
}
private:
typedef std::list<callback> group;
group m_children;
};
///////////////////////////////////////////////////////////////////////////
template <typename FunctionT, template <typename> class ReductionT>
struct signal<FunctionT,ReductionT>::cookie : public nocopy {
cookie (typename group::iterator _position,
signal<FunctionT,ReductionT> &_parent):
m_position (_position),
m_parent (_parent)
{ ; }
cookie (cookie &&rhs):
m_position (rhs.m_position),
m_parent (rhs.m_parent)
{
rhs.m_position = rhs.m_parent.m_children.end ();
}
~cookie ()
{
if (m_parent.m_children.end () != m_position)
m_parent.disconnect (*this);
}
void reset (callback &&cb)
{
*m_position = std::move (cb);
}
typename group::iterator m_position;
signal<FunctionT,ReductionT> &m_parent;
};
///////////////////////////////////////////////////////////////////////////
// wrap a value in a signal and trigger on assignment
//template <typename T, template <typename> class ReductionT>
template <typename ValueT>
class value_signal : public signal<void(ValueT)> {
template <typename ValueT, typename FunctionT = std::function<void(ValueT)>>
class value_signal : public signal<FunctionT> {
public:
explicit value_signal (ValueT _value): m_value (_value) { ; }
value_signal () = default;

View File

@ -9,7 +9,7 @@ void
test_null (cruft::TAP::logger &tap)
{
tap.expect_nothrow ([] {
cruft::signal<void(void)> void_signal;
cruft::signal<void(*)(void)> void_signal;
void_signal ();
}, "void signal");
}
@ -28,7 +28,7 @@ void
test_single (cruft::TAP::logger &tap)
{
unsigned int val = 0;
cruft::signal<void(unsigned int&)> void_signal;
cruft::signal<void(*)(unsigned int&)> void_signal;
auto cookie = void_signal.connect (increment_uint);
void_signal (val);
@ -42,7 +42,7 @@ void
test_double (cruft::TAP::logger &tap)
{
unsigned int val = 0;
cruft::signal<void(unsigned int&)> void_signal;
cruft::signal<void(*)(unsigned int&)> void_signal;
auto cookie0 = void_signal.connect (increment_uint);
auto cookie1 = void_signal.connect (increment_uint);
@ -75,7 +75,7 @@ void
test_combiner (cruft::TAP::logger &tap)
{
{
cruft::signal<bool(void), cruft::reduce::logical_and> sig;
cruft::signal<std::function<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::reduce::logical_and> sig;
cruft::signal<std::function<bool(void)>, cruft::reduce::logical_and> sig;
unsigned count = 0;
auto cookie0 = sig.connect ([&] (void) { ++count; return true; });
@ -109,12 +109,13 @@ void
test_disconnect (cruft::TAP::logger &tap)
{
tap.expect_nothrow ([] {
cruft::signal<void(void)> sig;
using function_t = std::function<void(void)>;
cruft::signal<function_t> sig;
cruft::signal<void(void)>::cookie a = sig.connect ([&] (void) { sig.disconnect (a); });
cruft::signal<void(void)>::cookie b = sig.connect ([&] (void) { sig.disconnect (b); });
cruft::signal<void(void)>::cookie c = sig.connect ([&] (void) { sig.disconnect (c); });
cruft::signal<void(void)>::cookie d = sig.connect ([&] (void) { sig.disconnect (d); });
cruft::signal<function_t>::cookie a = sig.connect ([&] (void) { sig.disconnect (a); });
cruft::signal<function_t>::cookie b = sig.connect ([&] (void) { sig.disconnect (b); });
cruft::signal<function_t>::cookie c = sig.connect ([&] (void) { sig.disconnect (c); });
cruft::signal<function_t>::cookie d = sig.connect ([&] (void) { sig.disconnect (d); });
sig ();
}, "parallel disconnect in invocation");