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>
|
|
|
|
|
2020-07-01 17:02:44 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2020-07-23 15:16:15 +10:00
|
|
|
/// A signal object whose clients can listen for invocations.
|
|
|
|
///
|
|
|
|
/// The arguments supplied to the invocation of the parent are supplied
|
|
|
|
/// to the callback of each child.
|
|
|
|
///
|
|
|
|
/// Every client must store the cookie from `connect`. When this goes out
|
|
|
|
/// of scope it will disconnect the callback from the signal object.
|
|
|
|
/// It is permissible to release a cookie while it is being invoked, but
|
|
|
|
/// no other cookie for the duration.
|
|
|
|
///
|
|
|
|
/// All cookies _should_ be destroyed or released before the signal is
|
|
|
|
/// destroyed.
|
|
|
|
///
|
|
|
|
/// \tparam FunctionT The type of the callback
|
2019-03-12 15:38:42 +11:00
|
|
|
template <
|
2020-07-23 15:16:15 +10:00
|
|
|
typename FunctionT
|
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 function_type = FunctionT;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2020-07-23 15:16:15 +10:00
|
|
|
/// A single node in a doubly linked list of callbacks to invoke when
|
|
|
|
/// a signal is invoked.
|
|
|
|
///
|
|
|
|
/// They perform no direct processing by themselves. Instead they are
|
|
|
|
/// entirely focused on managing lifetimes of, and pointers to, the
|
|
|
|
/// callbacks that will be invoked.
|
2019-03-13 12:08:57 +11:00
|
|
|
struct cookie {
|
2020-07-23 15:16:15 +10:00
|
|
|
cookie (FunctionT &&_callback)
|
|
|
|
: callback (std::move (_callback))
|
|
|
|
, next (nullptr)
|
|
|
|
, prev (nullptr)
|
2019-03-13 12:08:57 +11:00
|
|
|
{ ; }
|
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
cookie (cookie &&rhs) noexcept
|
|
|
|
: cookie (std::move (rhs.callback))
|
2019-03-13 12:08:57 +11:00
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
replace (rhs);
|
2019-03-13 12:08:57 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
cookie& operator= (cookie &&rhs) noexcept
|
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
replace (rhs);
|
|
|
|
callback = std::move (rhs.callback);
|
2019-03-13 12:08:57 +11:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
cookie (cookie const&) = delete;
|
|
|
|
cookie& operator= (cookie const&) = delete;
|
|
|
|
|
2019-03-13 12:08:57 +11:00
|
|
|
~cookie ()
|
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
release ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Take the position of `rhs` in the linked list.
|
|
|
|
void replace (cookie &rhs)
|
|
|
|
{
|
|
|
|
// It could be more efficient to special case these
|
|
|
|
// operations but this shouldn't be an operation that
|
|
|
|
// occurs in a hot loop anyway.
|
|
|
|
release ();
|
|
|
|
rhs.append (*this);
|
|
|
|
rhs.release ();
|
2019-03-13 12:08:57 +11:00
|
|
|
}
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
/// Link the provided cookie into the linked list as our
|
|
|
|
/// successor.
|
|
|
|
///
|
|
|
|
/// The provided cookie must not currently be part of any
|
|
|
|
/// linked list.
|
|
|
|
void append (cookie &rhs)
|
|
|
|
{
|
|
|
|
CHECK (rhs.next == nullptr);
|
|
|
|
CHECK (rhs.prev == nullptr);
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
CHECK_EQ (next->prev, this);
|
|
|
|
next->prev = &rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
rhs.next = next;
|
|
|
|
rhs.prev = this;
|
2019-03-13 12:08:57 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
next = &rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If this cookie is part of a linked list then remove it
|
|
|
|
/// from the linked list. Otherwise this is a noop.
|
|
|
|
void release (void)
|
2019-03-13 12:08:57 +11:00
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
if (next) next->prev = prev;
|
|
|
|
if (prev) prev->next = next;
|
|
|
|
|
|
|
|
next = nullptr;
|
|
|
|
prev = nullptr;
|
2019-03-13 12:08:57 +11:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
FunctionT callback;
|
2019-03-13 12:08:57 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
/// The next node in the linked list. Or nullptr if this is the end.
|
|
|
|
cookie *next;
|
|
|
|
/// The previous node in the linked list. Or nullptr if this is the start.
|
|
|
|
cookie *prev;
|
2019-03-13 12:08:57 +11:00
|
|
|
};
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
public:
|
2020-07-23 15:16:15 +10:00
|
|
|
signal ()
|
|
|
|
: m_head (FunctionT{})
|
|
|
|
{ ; }
|
2019-03-13 14:07:51 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
signal (signal const&) = delete;
|
|
|
|
signal& operator= (signal const&) = delete;
|
2019-03-13 14:07:51 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
signal (signal &&rhs) noexcept = default;
|
|
|
|
signal& operator= (signal &&rhs) noexcept = default;
|
2019-03-13 14:07:51 +11:00
|
|
|
|
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.
|
2020-07-23 15:16:15 +10:00
|
|
|
cookie
|
|
|
|
connect [[nodiscard]] (FunctionT &&_callback)
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
cookie res (std::move (_callback));
|
|
|
|
m_head.append (res);
|
|
|
|
return res;
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
2011-06-27 15:31:41 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Disconnect all callbacks
|
2020-07-23 15:16:15 +10:00
|
|
|
void clear (void);
|
|
|
|
|
2011-08-10 21:31:24 +10:00
|
|
|
|
2011-07-16 14:47:56 +10:00
|
|
|
/// Returns the number of callbacks connected.
|
2020-07-23 15:16:15 +10:00
|
|
|
std::size_t size (void) const
|
2018-02-28 11:49:13 +11:00
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
std::size_t accum = 0;
|
|
|
|
for (auto cursor = m_head.next; cursor; cursor = cursor->next)
|
|
|
|
++accum;
|
|
|
|
return accum;
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
bool empty (void) const
|
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
return m_head.next == nullptr;
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2011-08-10 21:30:03 +10:00
|
|
|
|
2020-07-23 15:16:15 +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
|
|
|
{
|
2020-07-23 15:16:15 +10:00
|
|
|
// We need to cache the cursor and advance _before_ we invoke
|
|
|
|
// the cookie so that we have saved a pointer to the next
|
|
|
|
// child in case it gets released on us during the call.
|
|
|
|
cookie *cursor = m_head.next;
|
|
|
|
|
|
|
|
while (cursor) {
|
|
|
|
auto now = cursor;
|
|
|
|
cursor = cursor->next;
|
|
|
|
std::invoke (now->callback, tail...);
|
|
|
|
}
|
2018-02-28 11:49:13 +11:00
|
|
|
}
|
2015-02-18 02:34:10 +11:00
|
|
|
|
2020-07-23 15:16:15 +10:00
|
|
|
|
2015-02-18 02:34:10 +11:00
|
|
|
private:
|
2020-07-23 15:16:15 +10:00
|
|
|
/// The first node in the linked list of callbacks. We
|
|
|
|
/// unconditionally store this node to simplify anchoring the
|
|
|
|
/// linked list to the signal object and removing nodes via their
|
|
|
|
/// destructors.
|
|
|
|
cookie m_head;
|
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
|
|
|
}
|