-/* 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. */
#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 S> class signal;
+
+ /** A signal/slot mechanism
+ *
+ * S is expected to be the function signature of the signal.
+ * I'm not sure we need a return value (it is currently ignored).
+ * If we don't we might use `signal<P1, P2, ...>` instead.
+ */
template<class R, class... P>
class signal<R(P...)> {
- private:
- sigc::signal<R, P...> sig_;
+ typedef std::function<R(P...)> callback_type;
+ 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)
{
- sig_.connect(std::forward<U>(slot));
+ handlers_.insert({callback_sequence_id, std::move(slot)});
+ return callback_sequence_id++;
}
- template<class Res, class... Args> XBT_ALWAYS_INLINE
- void connect(Res(*slot)(Args...))
+ R operator()(P... args) const
{
- sig_.connect(sigc::ptr_fun(slot));
- }
- template<class... Args> XBT_ALWAYS_INLINE
- R operator()(Args&&... args) const
- {
- return sig_.emit(std::forward<Args>(args)...);
+ 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(); }
};
-}
-}
-
-#else
-#include <boost/signals2.hpp>
-namespace simgrid {
-namespace xbt {
- template<class T>
- using signal = ::boost::signals2::signal<T>;
}
}
#endif
-
-#endif