Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Convert xbt_cfg_set_as_string -> simgrid::config::set_as_string
[simgrid.git] / include / xbt / signal.hpp
1 /* Copyright (c) 2014-2018. 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 <utility>
11 #include <vector>
12
13 namespace simgrid {
14 namespace xbt {
15
16   template<class S> class signal;
17
18   /** A signal/slot mechanism
19   *
20   *  S is expected to be the function signature of the signal.
21   *  I'm not sure we need a return value (it is currently ignored).
22   *  If we don't we might use `signal<P1, P2, ...>` instead.
23   */
24   template<class R, class... P>
25   class signal<R(P...)> {
26   private:
27     typedef std::function<R(P...)> callback_type;
28     std::vector<callback_type> handlers_;
29   public:
30     template<class U>
31     void connect(U slot)
32     {
33       handlers_.push_back(std::move(slot));
34     }
35     R operator()(P... args) const
36     {
37       for (auto const& handler : handlers_)
38         handler(args...);
39     }
40     void disconnectSlots() { handlers_.clear(); }
41     int getSlotsAmount() { return handlers_.size(); }
42   };
43
44 }
45 }
46
47 #endif