2011-06-27 15:31:41 +10:00
|
|
|
/*
|
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/.
|
2011-06-27 15:31:41 +10:00
|
|
|
*
|
2015-02-18 02:34:10 +11:00
|
|
|
* Copyright 2011-2015 Danny Robson <danny@nerdcruft.net>
|
2011-06-27 15:31:41 +10:00
|
|
|
*/
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#ifndef CRUFT_UTIL_SIGNAL_HPP
|
|
|
|
#define CRUFT_UTIL_SIGNAL_HPP
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2015-03-10 22:52:38 +11:00
|
|
|
#include "types/traits.hpp"
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "debug.hpp"
|
|
|
|
#include "nocopy.hpp"
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#include <algorithm>
|
2011-06-27 15:31:41 +10:00
|
|
|
#include <functional>
|
2015-02-18 02:34:10 +11:00
|
|
|
#include <list>
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2015-03-10 22:52:38 +11:00
|
|
|
namespace combine {
|
|
|
|
template <typename F>
|
|
|
|
struct logical_and {
|
|
|
|
using R = typename func_traits<F>::return_type;
|
|
|
|
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
R operator() (T first, T last, Args&&... args)
|
|
|
|
{
|
|
|
|
while (first != last)
|
2017-08-08 13:54:45 +10:00
|
|
|
if (!(*first++)(args...))
|
2015-03-10 22:52:38 +11:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
struct logical_or {
|
|
|
|
using R = typename func_traits<F>::return_type;
|
|
|
|
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
R operator() (T first, T last, Args&&... args)
|
|
|
|
{
|
|
|
|
while (first != last)
|
2017-08-08 13:54:45 +10:00
|
|
|
if ((*first++)(args...))
|
2015-03-10 22:52:38 +11:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename F>
|
|
|
|
struct noop {
|
|
|
|
using R = void;
|
|
|
|
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
R operator() (T first, T last, Args&&... args)
|
|
|
|
{
|
|
|
|
while (first != last) {
|
2017-08-08 13:54:45 +10:00
|
|
|
(*first++)(args...);
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename F, template <typename> class C = combine::noop>
|
2011-07-16 14:47:56 +10:00
|
|
|
class signal {
|
|
|
|
public:
|
2015-03-10 22:52:38 +11:00
|
|
|
using R = typename C<F>::R;
|
2015-02-18 02:34:10 +11:00
|
|
|
using callback = std::function<F>;
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
struct cookie;
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
public:
|
2018-02-28 11:49:13 +11:00
|
|
|
signal () = default;
|
|
|
|
~signal ()
|
|
|
|
{
|
|
|
|
CHECK (empty ());
|
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Add a callback to list.
|
2018-02-28 11:49:13 +11:00
|
|
|
cookie connect [[nodiscard]] (callback &&_cb)
|
|
|
|
{
|
|
|
|
return cookie (
|
|
|
|
m_children.insert (
|
|
|
|
m_children.end (),
|
|
|
|
std::move (_cb)
|
|
|
|
),
|
|
|
|
*this
|
|
|
|
);
|
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
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 ();
|
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Disconnect all callbacks
|
2018-02-28 11:49:13 +11:00
|
|
|
void clear (void)
|
|
|
|
{
|
|
|
|
m_children.clear ();
|
|
|
|
}
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Returns the number of callbacks connected.
|
2018-02-28 11:49:13 +11:00
|
|
|
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
|
2015-02-18 02:34:10 +11:00
|
|
|
template <typename ...Args>
|
2015-03-10 22:52:38 +11:00
|
|
|
R
|
2018-02-28 11:49:13 +11:00
|
|
|
operator() (Args&&... tail)
|
|
|
|
{
|
|
|
|
if (m_children.empty ())
|
|
|
|
return R();
|
|
|
|
|
|
|
|
C<F> combiner;
|
|
|
|
return combiner (
|
|
|
|
m_children.begin (),
|
|
|
|
m_children.end (),
|
|
|
|
std::forward<Args> (tail)...
|
|
|
|
);
|
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::list<callback> group;
|
|
|
|
group m_children;
|
2011-07-16 14:47:56 +10:00
|
|
|
};
|
2015-02-19 13:27:47 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename F, template <typename> class C>
|
|
|
|
struct signal<F,C>::cookie : public nocopy {
|
|
|
|
cookie (typename group::iterator _position,
|
|
|
|
signal<F,C> &_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<F,C> &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 C>
|
2015-02-19 13:27:47 +11:00
|
|
|
template <typename T>
|
|
|
|
class value_signal : public signal<void(T)> {
|
|
|
|
public:
|
2018-02-28 11:49:13 +11:00
|
|
|
explicit value_signal (T t): m_value (t) { ; }
|
2015-02-20 15:27:18 +11:00
|
|
|
value_signal () = default;
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
operator const T&() const { return m_value; }
|
2015-02-19 13:27:47 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
value_signal<T>&
|
|
|
|
operator= (const T &t)
|
|
|
|
{
|
|
|
|
m_value = t;
|
|
|
|
(*this) (m_value);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-19 13:27:47 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
};
|
2011-07-16 14:47:56 +10:00
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#endif
|