3#include "util/Mutex.hpp"
5#include <boost/signals2.hpp>
6#include <boost/signals2/connection.hpp>
7#include <boost/signals2/variadic_signal.hpp>
13#include <unordered_map>
25template <
typename Session,
typename... Args>
27 using ConnectionPtr = Session*;
28 using ConnectionSharedPtr = std::shared_ptr<Session>;
32 using ConnectionsMap = std::unordered_map<ConnectionPtr, boost::signals2::connection>;
35 using SignalType = boost::signals2::signal<void(Args...)>;
52 auto connections = connections_.template lock<std::scoped_lock>();
53 if (connections->contains(trackable.get())) {
66 signal_.connect([slot, weakTrackable = std::weak_ptr(trackable)](Args&&... args) {
67 if (auto lifeExtender = weakTrackable.lock(); lifeExtender)
68 std::invoke(slot, std::forward<Args...>(args)...);
86 if (
auto connections = connections_.template lock<std::scoped_lock>();
87 connections->contains(trackablePtr)) {
88 connections->operator[](trackablePtr).
disconnect();
89 connections->erase(trackablePtr);
101 emit(Args
const&... args)
const
112 return connections_.template lock<std::scoped_lock>()->size();
A thread-safe class to manage a signal and its tracking connections.
Definition TrackableSignal.hpp:26
bool disconnect(ConnectionPtr trackablePtr)
Disconnect a slot to the signal.
Definition TrackableSignal.hpp:84
std::size_t count() const
Get the number of connections.
Definition TrackableSignal.hpp:110
void emit(Args const &... args) const
Calling all slots.
Definition TrackableSignal.hpp:101
bool connectTrackableSlot(ConnectionSharedPtr const &trackable, std::function< void(Args...)> slot)
Connect a slot to the signal, the slot will be called when the signal is emitted and trackable is sti...
Definition TrackableSignal.hpp:50
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:82