SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
signalslot.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
25 
26 #ifndef SHORE_PROCESSING_SIGNALSLOT_HPP__
27 #define SHORE_PROCESSING_SIGNALSLOT_HPP__
28 
29 #include <map>
30 #include <set>
31 
32 #include <boost/function.hpp>
33 
34 namespace shore {
35 
36 template<typename T> class signal;
37 template<typename T> class slot;
38 
40 template<typename T>
41 struct sig_spec
42 {
43  typedef boost::function<void (T)> functor_t;
44 
45  class emitter
46  {
47  private:
48 
49  friend class signal<T>;
50  typedef std::map<slot<T>*,functor_t> map_t;
51  const map_t& map;
52 
53  emitter(const map_t& m): map(m) {}
54 
55  public:
56 
57  void operator()(T value) const;
58  };
59 };
60 
62 template<>
63 struct sig_spec<void>
64 {
65  typedef boost::function<void ()> functor_t;
66 
67  class emitter
68  {
69  private:
70 
71  friend class signal<void>;
72  typedef std::map<slot<void>*,functor_t> map_t;
73  const map_t& map;
74 
75  emitter(const map_t& m): map(m) {}
76 
77  public:
78 
79  void operator()() const;
80  };
81 };
82 
84 template<typename T>
85 class slot
86 {
87  private:
88 
89  friend class signal<T>;
90 
91  typedef typename sig_spec<T>::functor_t functor_t;
92 
93  typedef signal<T> signal_t;
94  typedef std::set<signal_t*> sigset_t;
95 
96  functor_t m_func;
97  sigset_t m_signals;
98 
99  public:
100 
102  slot(functor_t);
103 
105  ~slot();
106 
108  void disconnect_all();
109 
112  slot(const slot& bp);
113 
115  slot& operator=(const slot& bp);
116 
118  void copy_connections(const slot& bp);
119 
121  size_t nsignals() const;
122 };
123 
125 template<typename T>
126 class signal
127 {
128  private:
129 
130  friend class slot<T>;
131 
132  typedef typename sig_spec<T>::functor_t functor_t;
133  typedef std::map<slot<T>*,functor_t> slotmap_t;
134 
135  typedef typename sig_spec<T>::emitter emitter_type;
136 
137  slotmap_t m_slots;
138 
139  public:
140 
144  const emitter_type emit;
145 
146 
147  signal();
148 
150  ~signal();
151 
153  void disconnect_all();
154 
155  signal(const signal& bp);
156 
157  signal& operator=(const signal& bp);
158 
160  void connect(slot<T>& s);
161 
163  void disconnect(slot<T>& s);
164 
165  size_t nslots() const;
166 };
167 
168 } //namespace shore
169 
170 #include "signalslot.t.hpp"
171 
172 #endif // SHORE_PROCESSING_SIGNALSLOT_HPP__
173