From: Frederic Suter Date: Fri, 19 Apr 2019 11:35:00 +0000 (+0200) Subject: Allow for callback disconnection X-Git-Tag: v3.22.2~74 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/22639e2bf15d31eabcd2224b9e850a108ae3ca57?hp=bf6e09ad4a0ea12c2a007bb19670bbb0710a7c6c;ds=inline 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. --- 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(); } };