22#include "util/Mutex.hpp"
24#include <boost/signals2.hpp>
25#include <boost/signals2/connection.hpp>
26#include <boost/signals2/variadic_signal.hpp>
32#include <unordered_map>
44template <
typename Session,
typename... Args>
46 using ConnectionPtr = Session*;
47 using ConnectionSharedPtr = std::shared_ptr<Session>;
51 using ConnectionsMap = std::unordered_map<ConnectionPtr, boost::signals2::connection>;
54 using SignalType = boost::signals2::signal<void(Args...)>;
71 auto connections = connections_.template lock<std::scoped_lock>();
72 if (connections->contains(trackable.get())) {
85 signal_.connect([slot, weakTrackable = std::weak_ptr(trackable)](Args&&... args) {
86 if (auto lifeExtender = weakTrackable.lock(); lifeExtender)
87 std::invoke(slot, std::forward<Args...>(args)...);
105 if (
auto connections = connections_.template lock<std::scoped_lock>();
106 connections->contains(trackablePtr)) {
107 connections->operator[](trackablePtr).
disconnect();
108 connections->erase(trackablePtr);
120 emit(Args
const&... args)
const
131 return connections_.template lock<std::scoped_lock>()->size();
A thread-safe class to manage a signal and its tracking connections.
Definition TrackableSignal.hpp:45
bool disconnect(ConnectionPtr trackablePtr)
Disconnect a slot to the signal.
Definition TrackableSignal.hpp:103
std::size_t count() const
Get the number of connections.
Definition TrackableSignal.hpp:129
void emit(Args const &... args) const
Calling all slots.
Definition TrackableSignal.hpp:120
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:69
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:101