XMM - Probabilistic Models for Motion Recognition and Mapping

xmmEvents.hpp
Go to the documentation of this file.
1 /*
2  * xmmEvents.hpp
3  *
4  * Template classes for Event generators
5  *
6  * Contact:
7  * - Jules Francoise <jules.francoise@ircam.fr>
8  *
9  * This code has been initially authored by Jules Francoise
10  * <http://julesfrancoise.com> during his PhD thesis, supervised by Frederic
11  * Bevilacqua <href="http://frederic-bevilacqua.net>, in the Sound Music
12  * Movement Interaction team <http://ismm.ircam.fr> of the
13  * STMS Lab - IRCAM, CNRS, UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 UPMC, Ircam-Centre Pompidou.
16  *
17  * This File is part of XMM.
18  *
19  * XMM is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * XMM is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with XMM. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef xmmEvents_h
34 #define xmmEvents_h
35 
36 #include <functional>
37 #include <map>
38 #include <set>
39 
40 namespace xmm {
46 template <typename EventType>
48  public:
49  typedef std::function<void(EventType&)> EventCallBack;
50 
55 
60  virtual ~EventGenerator() {}
61 
70  template <typename U, typename args, class ListenerClass>
71  void addListener(U* owner, void (ListenerClass::*listenerMethod)(args)) {
72  callbacks_.insert(std::pair<void*, EventCallBack>(
73  static_cast<void*>(owner),
74  std::bind(listenerMethod, owner, std::placeholders::_1)));
75  }
76 
85  template <typename U, typename args, class ListenerClass>
86  void removeListener(U* owner, void (ListenerClass::*listenerMethod)(args)) {
87  callbacks_.erase(static_cast<void*>(owner));
88  }
89 
93  void removeListeners() { callbacks_.clear(); }
94 
99  void notifyListeners(EventType& e) const {
100  for (auto& callback : callbacks_) {
101  callback.second(e);
102  }
103  }
104 
105  private:
109  std::map<void*, EventCallBack> callbacks_;
110 };
111 }
112 
113 #endif
EventGenerator()
Default constructor.
Definition: xmmEvents.hpp:54
std::function< void(EventType &)> EventCallBack
Definition: xmmEvents.hpp:49
void notifyListeners(EventType &e) const
Propagates the event to all listeners.
Definition: xmmEvents.hpp:99
virtual ~EventGenerator()
Destructor.
Definition: xmmEvents.hpp:60
void removeListeners()
Removes all listeners.
Definition: xmmEvents.hpp:93
void removeListener(U *owner, void(ListenerClass::*listenerMethod)(args))
Removes a listener object.
Definition: xmmEvents.hpp:86
Generator class for a specific type of events.
Definition: xmmEvents.hpp:47
Definition: xmmAttribute.hpp:42
void addListener(U *owner, void(ListenerClass::*listenerMethod)(args))
Adds a listener object to be notified when events are sent.
Definition: xmmEvents.hpp:71
std::map< void *, EventCallBack > callbacks_
Set of listener objects.
Definition: xmmEvents.hpp:109