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
|
|
|
*
|
2019-03-12 15:38:42 +11:00
|
|
|
* Copyright 2011-2019 Danny Robson <danny@nerdcruft.net>
|
2011-06-27 15:31:41 +10:00
|
|
|
*/
|
|
|
|
|
2019-03-12 15:38:42 +11:00
|
|
|
#pragma once
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2015-03-10 22:52:38 +11:00
|
|
|
#include "types/traits.hpp"
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "debug/assert.hpp"
|
2011-08-10 21:31:24 +10:00
|
|
|
|
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
|
|
|
|
2020-07-01 17:02:44 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2019-03-12 15:38:42 +11:00
|
|
|
namespace reduce {
|
2019-03-13 12:08:57 +11:00
|
|
|
/// Returns the short-circuited logical-and of the results of all
|
|
|
|
/// connected callbacks.
|
2015-03-10 22:52:38 +11:00
|
|
|
struct logical_and {
|
2019-03-12 15:38:42 +11:00
|
|
|
template <typename InputT, typename ...Args>
|
2019-03-13 12:08:57 +11:00
|
|
|
decltype(auto)
|
|
|
|
operator() (InputT first, InputT last, Args&&... args)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
|
|
|
while (first != last)
|
2019-03-13 12:08:57 +11:00
|
|
|
if (!(std::invoke (*first++, args...)))
|
2015-03-10 22:52:38 +11:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-13 12:08:57 +11:00
|
|
|
/// Returns the short-circuited logical-or of the results of all
|
|
|
|
/// connected callbacks.
|
2015-03-10 22:52:38 +11:00
|
|
|
struct logical_or {
|
2019-03-12 15:38:42 +11:00
|
|
|
template <typename InputT, typename ...Args>
|
2019-03-13 12:08:57 +11:00
|
|
|
decltype(auto)
|
|
|
|
operator() (InputT first, InputT last, Args&&... args)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
|
|
|
while (first != last)
|
2019-03-13 12:08:57 +11:00
|
|
|
if (std::invoke (*first++, args...))
|
2015-03-10 22:52:38 +11:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-13 12:08:57 +11:00
|
|
|
/// Unconditionally evaluates all connected callbacks. Returns nothing.
|
2015-03-10 22:52:38 +11:00
|
|
|
struct noop {
|
2019-03-12 15:38:42 +11:00
|
|
|
template <typename InputT, typename ...Args>
|
2019-03-13 12:08:57 +11:00
|
|
|
void operator() (InputT first, InputT last, Args&&... args)
|
2015-03-10 22:52:38 +11:00
|
|
|
{
|
|
|
|
while (first != last) {
|
2019-03-13 12:08:57 +11:00
|
|
|
std::invoke (*first++, args...);
|
2015-03-10 22:52:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:38:42 +11:00
|
|
|
template <
|
|
|
|
typename FunctionT,
|
2019-03-13 12:08:57 +11:00
|
|
|
typename ReductionT = reduce::noop
|
2019-03-12 15:38:42 +11:00
|
|
|
>
|
2011-07-16 14:47:56 +10:00
|
|
|
class signal {
|
|
|
|
public:
|
2019-03-13 12:08:57 +11:00
|
|
|
using reduction_type = ReductionT;
|
|
|
|
using function_type = FunctionT;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2019-03-13 12:08:57 +11:00
|
|
|
|
|
|
|
void reset (FunctionT &&cb)
|
|
|
|
{
|
|
|
|
*m_position = std::move (cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typename group::iterator m_position;
|
|
|
|
signal<FunctionT,ReductionT> &m_parent;
|
|
|
|
};
|
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;
|
2019-03-13 14:07:51 +11:00
|
|
|
|
|
|
|
signal (signal const&) = default;
|
|
|
|
signal& operator= (signal const&) = default;
|
|
|
|
|
|
|
|
signal (signal &&) noexcept = default;
|
|
|
|
signal& operator= (signal &&) noexcept = default;
|
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
~signal ()
|
|
|
|
{
|
|
|
|
CHECK (empty ());
|
|
|
|
}
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Add a callback to list.
|
2019-03-13 12:08:57 +11:00
|
|
|
cookie connect [[nodiscard]] (FunctionT &&_cb)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
|
|
|
return cookie (
|
|
|
|
m_children.insert (
|
|
|
|
m_children.end (),
|
|
|
|
std::move (_cb)
|
|
|
|
),
|
|
|
|
*this
|
|
|
|
);
|
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
2019-03-13 12:08:57 +11:00
|
|
|
cookie connect [[nodiscard]] (FunctionT const &_cb)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2019-03-13 12:08:57 +11:00
|
|
|
return cookie (
|
|
|
|
m_children.insert (
|
|
|
|
m_children.end (),
|
|
|
|
std::move (_cb)
|
|
|
|
),
|
|
|
|
*this
|
|
|
|
);
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-03-12 15:38:42 +11:00
|
|
|
template <typename ...ArgsT>
|
2019-03-13 12:08:57 +11:00
|
|
|
decltype(auto)
|
2019-03-12 15:38:42 +11:00
|
|
|
operator() (ArgsT&&... tail)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2019-03-13 12:08:57 +11:00
|
|
|
return ReductionT {} (
|
2018-02-28 11:49:13 +11:00
|
|
|
m_children.begin (),
|
|
|
|
m_children.end (),
|
2019-03-12 15:38:42 +11:00
|
|
|
std::forward<ArgsT> (tail)...
|
2018-02-28 11:49:13 +11:00
|
|
|
);
|
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
|
|
|
private:
|
|
|
|
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
|
|
|
|
2020-07-21 15:01:33 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
// Function references are _actually_ used as if they are std::function
|
|
|
|
// objects in our libraries.
|
|
|
|
//
|
|
|
|
// TODO: avoid this implicit conversion without need a massive rewrite.
|
|
|
|
template <typename ReturnT, typename ...ArgsT>
|
|
|
|
class signal<ReturnT(ArgsT...)> :
|
|
|
|
public signal<std::function<ReturnT(ArgsT...)>>
|
|
|
|
{ };
|
|
|
|
|
|
|
|
|
2015-03-10 22:52:38 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// wrap a value in a signal and trigger on assignment
|
2019-03-12 15:38:42 +11:00
|
|
|
//template <typename T, template <typename> class ReductionT>
|
2019-03-13 12:08:57 +11:00
|
|
|
template <typename ValueT, typename FunctionT = std::function<void(ValueT)>>
|
|
|
|
class value_signal : public signal<FunctionT> {
|
2015-02-19 13:27:47 +11:00
|
|
|
public:
|
2019-03-12 15:38:42 +11:00
|
|
|
explicit value_signal (ValueT _value): m_value (_value) { ; }
|
2015-02-20 15:27:18 +11:00
|
|
|
value_signal () = default;
|
|
|
|
|
2019-03-12 15:38:42 +11:00
|
|
|
operator const ValueT&() const { return m_value; }
|
2015-02-19 13:27:47 +11:00
|
|
|
|
2019-03-12 15:38:42 +11:00
|
|
|
value_signal<ValueT>&
|
|
|
|
operator= (const ValueT &t)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
|
|
|
m_value = t;
|
|
|
|
(*this) (m_value);
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-19 13:27:47 +11:00
|
|
|
|
|
|
|
private:
|
2019-03-12 15:38:42 +11:00
|
|
|
ValueT m_value;
|
2015-02-19 13:27:47 +11:00
|
|
|
};
|
2011-07-16 14:47:56 +10:00
|
|
|
}
|