Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill unused function.
[simgrid.git] / include / xbt / signal.hpp
index 2068546..8b6cf91 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2014-2015. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2014-2023. 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. */
@@ -6,47 +6,44 @@
 #ifndef SIMGRID_XBT_SIGNAL_HPP
 #define SIMGRID_XBT_SIGNAL_HPP
 
-#ifdef SIMGRID_HAVE_LIBSIG
-#include <sigc++/sigc++.h>
+#include <functional>
+#include <map>
+#include <utility>
 
 namespace simgrid {
 namespace xbt {
-  // Wraps sigc++ signals with the interface of boost::signals2:
-  template<class T> class signal;
-  template<class R, class... P>
-  class signal<R(P...)> {
-  private:
-    sigc::signal<R, P...> sig_;
-  public:
-    template<class U> XBT_ALWAYS_INLINE
-    void connect(U&& slot)
-    {
-      sig_.connect(std::forward<U>(slot));
-    }
-    template<class Res, class... Args> XBT_ALWAYS_INLINE
-    void connect(Res(*slot)(Args...))
-    {
-      sig_.connect(sigc::ptr_fun(slot));
-    }
-    template<class... Args> XBT_ALWAYS_INLINE
-    R operator()(Args&&... args) const
-    {
-      return sig_.emit(std::forward<Args>(args)...);
-    }
-  };
-}
-}
 
-#else
-
-#include <boost/signals2.hpp>
-namespace simgrid {
-namespace xbt {
-  template<class T>
-  using signal = ::boost::signals2::signal<T>;
+template <class S> class signal;
+
+/** @brief
+ * A signal/slot mechanism, where you can attach callbacks to a given signal, and then fire the signal.
+ *
+ * The template parameter is the function signature of the signal (the return value currently ignored).
+ */
+template <class R, class... P> class signal<R(P...)> {
+  using callback_type = std::function<R(P...)>;
+  std::map<unsigned int, callback_type> handlers_;
+  unsigned int callback_sequence_id = 0;
+
+public:
+  /** Add a new callback to this signal */
+  template <class U> unsigned int connect(U slot)
+  {
+    handlers_.insert({callback_sequence_id, std::move(slot)});
+    return callback_sequence_id++;
+  }
+  /** Fire that signal, invoking all callbacks */
+  R operator()(P... args) const
+  {
+    for (auto const& handler : handlers_)
+      handler.second(args...);
+  }
+  /** Remove a callback */
+  void disconnect(unsigned int id) { handlers_.erase(id); }
+  /** Remove all callbacks */
+  void disconnect_slots() { handlers_.clear(); }
+};
 }
 }
 
 #endif
-
-#endif