5#ifndef BITCOIN_BTCSIGNALS_H
6#define BITCOIN_BTCSIGNALS_H
44 using result_type = std::conditional_t<std::is_void_v<T>, void, std::optional<T>>;
47template <typename Signature, typename Combiner = optional_last_value<typename std::function<Signature>::result_type>>
57 template <
typename Signature,
typename Combiner>
148template <
typename Signature,
typename Combiner>
153 static_assert(std::is_same_v<Combiner, optional_last_value<typename function_type::result_type>>,
"only the optional_last_value combiner is supported");
159 template <
typename Callable>
202 template <typename... Args>
205 std::vector<std::shared_ptr<connection_holder>> connections;
208 connections = m_connections;
210 if constexpr (std::is_void_v<result_type>) {
231 template <
typename Callable>
237 std::erase_if(m_connections, [](
const auto& holder) {
return !holder->connected(); });
239 const auto& entry = m_connections.emplace_back(std::make_shared<connection_holder>(std::forward<Callable>(func)));
249 return std::ranges::none_of(m_connections, [](
const auto& holder) {
250 return holder->connected();
std::atomic_bool m_connected
void disconnect()
If a callback is associated with this connection, prevent it from being called in the future.
std::shared_ptr< liveness > m_state
connections have shared_ptr-like copy and move semantics.
connection(std::shared_ptr< liveness > &&state)
Only a signal can create an enabled connection.
constexpr connection() noexcept=default
The default constructor creates a connection with no associated signal.
bool connected() const
Returns true if this connection was created by a signal and has not been disabled.
std::conditional_t< std::is_void_v< T >, void, std::optional< T > > result_type
scoped_connection(scoped_connection &&) noexcept=default
scoped_connection(connection rhs) noexcept
std::function< Signature > function_type
std::vector< std::shared_ptr< connection_holder > > m_connections GUARDED_BY(m_mutex)
connection connect(Callable &&func) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Combiner::result_type result_type
constexpr signal() noexcept=default
bool empty() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
btcsignals is a simple mechanism for signaling events to multiple subscribers.
const function_type m_callback
connection_holder(Callable &&callback)
#define EXCLUSIVE_LOCKS_REQUIRED(...)