Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use our own minimal signal implementation
[simgrid.git] / include / xbt / signal.hpp
1 /* Copyright (c) 2014-2015. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_XBT_SIGNAL_HPP
7 #define SIMGRID_XBT_SIGNAL_HPP
8
9 #include <functional>
10 #include <vector>
11
12 namespace simgrid {
13 namespace xbt {
14
15   template<class S> class signal;
16
17   /** A signal/slot mechanism
18   *
19   *  S is expected to be the function signature of the signal.
20   *  I'm not sure we need a return value (it is currently ignored).
21   *  If we don't we might use `signal<P1, P2, ...>` instead.
22   */
23   template<class R, class... P>
24   class signal<R(P...)> {
25   private:
26     typedef std::function<R(P...)> callback_type;
27     std::vector<callback_type> handlers_;
28   public:
29     template<class U> XBT_ALWAYS_INLINE
30     void connect(U slot)
31     {
32       handlers_.push_back(std::move(slot));
33     }
34     XBT_ALWAYS_INLINE
35     R operator()(P... args) const
36     {
37       for (auto& handler : handlers_)
38         handler(args...);
39     }
40     void disconnect_all_slots()
41     {
42       handlers_.clear();
43     }
44   };
45
46 }
47 }
48
49 #endif