From 22639e2bf15d31eabcd2224b9e850a108ae3ca57 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Fri, 19 Apr 2019 13:35:00 +0200 Subject: [PATCH 1/1] Allow for callback disconnection xbt::signal::connect now returns an unsigned int id and callbacks are stored in a map . a new xbt::signal::disconnect(unsigned int id) method has been added. --- include/xbt/signal.hpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/include/xbt/signal.hpp b/include/xbt/signal.hpp index 2900ad45a7..a5e654494a 100644 --- a/include/xbt/signal.hpp +++ b/include/xbt/signal.hpp @@ -7,8 +7,8 @@ #define SIMGRID_XBT_SIGNAL_HPP #include +#include #include -#include namespace simgrid { namespace xbt { @@ -23,20 +23,22 @@ namespace xbt { */ template class signal { - private: typedef std::function callback_type; - std::vector handlers_; + std::map handlers_; + unsigned int callback_sequence_id = 0; + public: - template - void connect(U slot) + template unsigned int connect(U slot) { - handlers_.push_back(std::move(slot)); + handlers_.insert({callback_sequence_id, std::move(slot)}); + return callback_sequence_id++; } R operator()(P... args) const { for (auto const& handler : handlers_) - handler(args...); + handler.second(args...); } + void disconnect(unsigned int id) { handlers_.erase(id); } void disconnect_slots() { handlers_.clear(); } int get_slot_count() { return handlers_.size(); } }; -- 2.20.1