libcruft-util/signal.hpp

209 lines
5.6 KiB
C++
Raw Normal View History

/*
2018-08-04 15:14:06 +10:00
* This Source Code Form is subject to the terms of the Mozilla Public
* 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-2019 Danny Robson <danny@nerdcruft.net>
*/
#pragma once
2015-03-10 22:52:38 +11:00
#include "types/traits.hpp"
#include "debug.hpp"
#include "nocopy.hpp"
#include <algorithm>
#include <functional>
#include <list>
namespace cruft {
namespace reduce {
template <typename FunctionT>
2015-03-10 22:52:38 +11:00
struct logical_and {
using result = typename func_traits<FunctionT>::return_type;
2015-03-10 22:52:38 +11:00
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
2015-03-10 22:52:38 +11:00
{
while (first != last)
if (!(*first++)(args...))
2015-03-10 22:52:38 +11:00
return false;
return true;
}
};
template <typename FunctionT>
2015-03-10 22:52:38 +11:00
struct logical_or {
using result = typename func_traits<FunctionT>::return_type;
2015-03-10 22:52:38 +11:00
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
2015-03-10 22:52:38 +11:00
{
while (first != last)
if ((*first++)(args...))
2015-03-10 22:52:38 +11:00
return true;
return false;
}
};
template <typename FunctionT>
2015-03-10 22:52:38 +11:00
struct noop {
using result = void;
2015-03-10 22:52:38 +11:00
template <typename InputT, typename ...Args>
result operator() (InputT first, InputT last, Args&&... args)
2015-03-10 22:52:38 +11:00
{
while (first != last) {
(*first++)(args...);
2015-03-10 22:52:38 +11:00
}
}
};
}
template <
typename FunctionT,
template <typename> class ReductionT = reduce::noop
>
class signal {
public:
using result = typename ReductionT<FunctionT>::result;
using callback = std::function<FunctionT>;
struct cookie;
public:
signal () = default;
~signal ()
{
CHECK (empty ());
}
/// Add a callback to list.
cookie connect [[nodiscard]] (callback &&_cb)
{
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
cookie connect [[nodiscard]] (const callback &_cb)
{
{
return cookie (
m_children.insert (
m_children.end (),
std::move (_cb)
),
*this
);
}
}
void disconnect (cookie &c)
{
m_children.erase (c.m_position);
c.m_position = m_children.end ();
}
/// Disconnect all callbacks
void clear (void)
{
m_children.clear ();
}
/// Returns the number of callbacks connected.
size_t size (void) const
{
return m_children.size ();
}
bool empty (void) const
{
return m_children.empty ();
}
2011-08-10 21:30:03 +10:00
2015-03-10 22:52:38 +11:00
/// Execute all callbacks
template <typename ...ArgsT>
result
operator() (ArgsT&&... tail)
{
if (m_children.empty ())
return result();
ReductionT<FunctionT> reducer;
return reducer (
m_children.begin (),
m_children.end (),
std::forward<ArgsT> (tail)...
);
}
private:
typedef std::list<callback> group;
group m_children;
};
2015-02-19 13:27:47 +11:00
///////////////////////////////////////////////////////////////////////////
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;
};
2015-03-10 22:52:38 +11:00
///////////////////////////////////////////////////////////////////////////
// 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)> {
2015-02-19 13:27:47 +11:00
public:
explicit value_signal (ValueT _value): m_value (_value) { ; }
value_signal () = default;
operator const ValueT&() const { return m_value; }
2015-02-19 13:27:47 +11:00
value_signal<ValueT>&
operator= (const ValueT &t)
{
m_value = t;
(*this) (m_value);
return *this;
}
2015-02-19 13:27:47 +11:00
private:
ValueT m_value;
2015-02-19 13:27:47 +11:00
};
}