GranOO  3.0
A robust and versatile workbench to build 3D dynamic simulations based on the Discrete Element Method
Signal.hpp
Go to the documentation of this file.
1 // This file is part of GranOO, a workbench for DEM simulation.
2 //
3 // Author(s) : - Damien Andre IRCER/UNILIM, Limoges France
4 // <damien.andre@unilim.fr>
5 // - Jean-luc Charles Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
6 // <jean-luc.charles@ensam.eu>
7 // - Jeremie Girardot Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
8 // <jeremie.girardot@ensam.eu>
9 // - Cedric Hubert LAMIH/UPHF, Valenciennes France
10 // <cedric.hubert@uphf.fr>
11 // - Ivan Iordanoff Arts et Metiers ParisTech, CNRS, I2M, Bordeaux France
12 // <ivan.iordanoff@ensam.eu>
13 //
14 // Copyright (C) 2008-2019 D. Andre, JL. Charles, J. Girardot, C. Hubert, I. Iordanoff
15 //
16 // This program is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with this program. If not, see <http://www.gnu.org/licenses/>.
28 
29 
30 // File from http://simmesimme.github.io/tutorials/2015/09/20/signal-slot
31 // No licence was found
32 
33 #ifndef _Core_Signal_hpp_
34 #define _Core_Signal_hpp_
35 
36 #include <functional>
37 #include <map>
38 
39 
40 namespace GranOO3
41 {
42  namespace Core
43  {
44 
45  // A signal object may call multiple slots with the
46  // same signature. You can connect functions to the signal
47  // which will be called when the emit_signal() method on the
48  // signal object is invoked. Any argument passed to emit_signal()
49  // will be passed to the given functions.
50 
51  template <typename ... Args>
52  class Signal
53  {
54  public:
56  }
57 
58  // copy creates new signal
59  Signal(Signal const& other) : _current_ID(0) {
60  }
61 
62  // connects a member function to this Signal
63  template <typename T>
64  int connect_member(T* inst, void (T::*func)(Args...)) {
65  return connect([ = ](Args... args) {
66  (inst->*func)(args ...);
67  });
68  }
69 
70  // connects a const member function to this Signal
71  template <typename T>
72  int connect_member(T* inst, void (T::*func)(Args...) const) {
73  return connect([ = ](Args... args) {
74  (inst->*func)(args ...);
75  });
76  }
77 
78  // connects a std::function to the signal. The returned
79  // value can be used to disconnect the function again
80  int connect(std::function<void(Args...)> const& slot) const {
81  slots_.insert(std::make_pair(++_current_ID, slot));
82  return _current_ID;
83  }
84 
85  // disconnects a previously connected function
86  void disconnect(int id) const {
87  slots_.erase(id);
88  }
89 
90  // disconnects all previously connected functions
91  void disconnect_all() const {
92  slots_.clear();
93  }
94 
95  // calls all connected functions
96  void emit_signal(Args... p) {
97  for (auto it : slots_) {
98  it.second(p ...);
99  }
100  }
101 
102  private:
103  mutable std::map<int, std::function<void(Args...)> > slots_;
104  mutable int _current_ID;
105  };
106 
107 
108 
109 
110  }
111 }
112 
113 #endif
114 
115 
116 
Definition: Signal.hpp:53
Signal()
Definition: Signal.hpp:55
int connect_member(T *inst, void(T::*func)(Args...))
Definition: Signal.hpp:64
std::map< int, std::function< void(Args...)> > slots_
Definition: Signal.hpp:103
int _current_ID
Definition: Signal.hpp:104
void disconnect(int id) const
Definition: Signal.hpp:86
Signal(Signal const &other)
Definition: Signal.hpp:59
void disconnect_all() const
Definition: Signal.hpp:91
int connect_member(T *inst, void(T::*func)(Args...) const)
Definition: Signal.hpp:72
int connect(std::function< void(Args...)> const &slot) const
Definition: Signal.hpp:80
void emit_signal(Args... p)
Definition: Signal.hpp:96
Definition: Common.hpp:198