Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow for callback disconnection
[simgrid.git] / include / xbt / signal.hpp
index 08fbff6..a5e6544 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2014-2019. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -7,7 +7,8 @@
 #define SIMGRID_XBT_SIGNAL_HPP
 
 #include <functional>
-#include <vector>
+#include <map>
+#include <utility>
 
 namespace simgrid {
 namespace xbt {
@@ -22,25 +23,24 @@ namespace xbt {
   */
   template<class R, class... P>
   class signal<R(P...)> {
-  private:
     typedef std::function<R(P...)> callback_type;
-    std::vector<callback_type> handlers_;
+    std::map<unsigned int, callback_type> handlers_;
+    unsigned int callback_sequence_id = 0;
+
   public:
-    template<class U> XBT_ALWAYS_INLINE
-    void connect(U slot)
+    template <class U> unsigned int connect(U slot)
     {
-      handlers_.push_back(std::move(slot));
+      handlers_.insert({callback_sequence_id, std::move(slot)});
+      return callback_sequence_id++;
     }
-    XBT_ALWAYS_INLINE
     R operator()(P... args) const
     {
-      for (auto& handler : handlers_)
-        handler(args...);
-    }
-    void disconnect_all_slots()
-    {
-      handlers_.clear();
+      for (auto const& handler : handlers_)
+        handler.second(args...);
     }
+    void disconnect(unsigned int id) { handlers_.erase(id); }
+    void disconnect_slots() { handlers_.clear(); }
+    int get_slot_count() { return handlers_.size(); }
   };
 
 }