X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/79f947c54111b76b2db108fade4d8e4c0e4e703f..bfffef69e1b0554d13eec920f1a32f75b49ceac6:/include/xbt/signal.hpp diff --git a/include/xbt/signal.hpp b/include/xbt/signal.hpp index 26bd2df57f..70c477f6e6 100644 --- a/include/xbt/signal.hpp +++ b/include/xbt/signal.hpp @@ -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,54 +6,42 @@ #ifndef SIMGRID_XBT_SIGNAL_HPP #define SIMGRID_XBT_SIGNAL_HPP -#include "simgrid_config.h" -#if SIMGRID_HAVE_LIBSIG -#include -#else -#include -#endif - -namespace simgrid { -namespace xbt { - -#if SIMGRID_HAVE_LIBSIG - - // Wraps sigc++ signals with the interface of boost::signals2: - template class signal; - template - class signal { - private: - sigc::signal sig_; - public: - template XBT_ALWAYS_INLINE - void connect(U&& slot) - { - sig_.connect(std::forward(slot)); - } - template XBT_ALWAYS_INLINE - void connect(Res(*slot)(Args...)) - { - sig_.connect(sigc::ptr_fun(slot)); - } - template XBT_ALWAYS_INLINE - R operator()(Args&&... args) const - { - return sig_.emit(std::forward(args)...); - } - void disconnect_all_slots() - { - sig_.clear(); - } - }; - -#else - - template - using signal = ::boost::signals2::signal; - -#endif - -} -} +#include +#include +#include + +namespace simgrid::xbt { + +template 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 signal { + using callback_type = std::function; + std::map handlers_; + unsigned int callback_sequence_id = 0; + +public: + /** Add a new callback to this signal */ + template 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& [_, callback] : handlers_) + callback(args...); + } + /** Remove a callback */ + void disconnect(unsigned int id) { handlers_.erase(id); } + /** Remove all callbacks */ + void disconnect_slots() { handlers_.clear(); } +}; +} // namespace simgrid::xbt #endif