Choreonoid  1.5
Signal.h
Go to the documentation of this file.
1 
7 #ifndef CNOID_UTIL_SIGNAL_H
8 #define CNOID_UTIL_SIGNAL_H
9 
10 #include "Referenced.h"
11 #include <boost/type_traits/function_traits.hpp>
12 
13 #define CNOID_SIGNAL_CONCAT( X, Y ) CNOID_SIGNAL_DO_CONCAT( X, Y )
14 #define CNOID_SIGNAL_DO_CONCAT( X, Y ) CNOID_SIGNAL_DO_CONCAT2(X,Y)
15 #define CNOID_SIGNAL_DO_CONCAT2( X, Y ) X##Y
16 
17 namespace cnoid {
18 
19 class Connection;
20 
21 namespace signal_private {
22 
23 template<typename T>
24 struct last_value {
25  typedef T result_type;
26 
27  template<typename InputIterator>
28  T operator()(InputIterator first, InputIterator last) const {
29  T value;
30  while (first != last)
31  value = *first++;
32  return value;
33  }
34 };
35 
36 
37 template<>
38 struct last_value<void> {
39  public:
40  template<typename InputIterator>
41  void operator()(InputIterator first, InputIterator last) const{
42  while (first != last)
43  *first++;
44  }
45 };
46 
47 
48 class SlotHolderBase : public Referenced
49 {
50 public:
51  SlotHolderBase() : isBlocked(false) { }
52  virtual ~SlotHolderBase() { }
53  virtual void disconnect() = 0;
54  virtual bool connected() const = 0;
55  virtual void changeOrder(int orderId) = 0;
56 
57  bool isBlocked;
58 };
59 
60 
61 template<typename SlotType, typename ArgSetType>
63 {
64  typedef typename SlotType::result_type result_type;
65 
66  SlotType* currentSlot;
67  ArgSetType& args;
68 
69 public:
71  while(currentSlot && currentSlot->isBlocked){
72  currentSlot = currentSlot->next;
73  }
74  }
75 
76  SlotCallIterator(SlotType* firstSlot, ArgSetType& args)
77  : currentSlot(firstSlot), args(args) {
78  seekUnblockedSlot();
79  }
80 
82  : currentSlot(org.currentSlot), args(org.args) {
83  seekUnblockedSlot();
84  }
85 
86  bool operator==(const SlotCallIterator& rhs) const {
87  return (currentSlot == rhs.currentSlot);
88  }
89 
90  bool operator!=(const SlotCallIterator& rhs) const {
91  return (currentSlot != rhs.currentSlot);
92  }
93 
95  SlotCallIterator iter(*this);
96  currentSlot = currentSlot->next;
97  seekUnblockedSlot();
98  return iter;
99  }
100 
101  result_type operator*() const { return args.call(currentSlot); }
102 };
103 
104 
105 } // namespace signal_private
106 
108 {
110 
111 public:
113 
115 
116  Connection(const Connection& org) : slot(org.slot) { }
117 
119  slot = rhs.slot;
120  return *this;
121  }
122 
123  void disconnect() {
124  if(slot) {
125  slot->disconnect();
126  slot = 0;
127  }
128  }
129 
130  bool connected() {
131  return slot && slot->connected();
132  }
133 
134  void block() {
135  if(slot){
136  slot->isBlocked = true;
137  }
138  }
139 
140  void unblock() {
141  if(slot){
142  slot->isBlocked = false;
143  }
144  }
145 
146  enum Order { FIRST = 0, LAST };
147 
149  if(slot){
150  slot->changeOrder(order);
151  }
152  return *this;
153  }
154 };
155 
157 {
158 public:
160  ScopedConnection(const Connection& org) : Connection(org) { }
164  bool connected() { return Connection::connected(); }
165  void block() { Connection::block(); }
167  ScopedConnection& changeOrder(Order order) { Connection::changeOrder(order); return *this; }
168 
169 private:
171  ScopedConnection& operator=(const ScopedConnection& rhs);
172 };
173 
174 }
175 
176 
177 #define CNOID_SIGNAL_NUM_ARGS 0
178 #define CNOID_SIGNAL_TEMPLATE_PARMS
179 #define CNOID_SIGNAL_TEMPLATE_ARGS
180 #define CNOID_SIGNAL_PARMS
181 #define CNOID_SIGNAL_ARGS
182 #define CNOID_SIGNAL_ARGS_AS_MEMBERS
183 #define CNOID_SIGNAL_COPY_PARMS
184 #define CNOID_SIGNAL_INIT_ARGS
185 
186 #include "SignalTemplate.h"
187 
188 #undef CNOID_SIGNAL_INIT_ARGS
189 #undef CNOID_SIGNAL_COPY_PARMS
190 #undef CNOID_SIGNAL_ARGS_AS_MEMBERS
191 #undef CNOID_SIGNAL_ARGS
192 #undef CNOID_SIGNAL_PARMS
193 #undef CNOID_SIGNAL_TEMPLATE_ARGS
194 #undef CNOID_SIGNAL_TEMPLATE_PARMS
195 #undef CNOID_SIGNAL_NUM_ARGS
196 
197 #define CNOID_SIGNAL_NUM_ARGS 1
198 #define CNOID_SIGNAL_TEMPLATE_PARMS typename T1
199 #define CNOID_SIGNAL_TEMPLATE_ARGS T1
200 #define CNOID_SIGNAL_PARMS T1 a1
201 #define CNOID_SIGNAL_ARGS a1
202 #define CNOID_SIGNAL_ARGS_AS_MEMBERS T1 a1;
203 #define CNOID_SIGNAL_COPY_PARMS T1 ia1
204 #define CNOID_SIGNAL_INIT_ARGS :a1(ia1)
205 
206 #include "SignalTemplate.h"
207 
208 #undef CNOID_SIGNAL_INIT_ARGS
209 #undef CNOID_SIGNAL_COPY_PARMS
210 #undef CNOID_SIGNAL_ARGS_AS_MEMBERS
211 #undef CNOID_SIGNAL_ARGS
212 #undef CNOID_SIGNAL_PARMS
213 #undef CNOID_SIGNAL_TEMPLATE_ARGS
214 #undef CNOID_SIGNAL_TEMPLATE_PARMS
215 #undef CNOID_SIGNAL_NUM_ARGS
216 
217 #define CNOID_SIGNAL_NUM_ARGS 2
218 #define CNOID_SIGNAL_TEMPLATE_PARMS typename T1, typename T2
219 #define CNOID_SIGNAL_TEMPLATE_ARGS T1, T2
220 #define CNOID_SIGNAL_PARMS T1 a1, T2 a2
221 #define CNOID_SIGNAL_ARGS a1, a2
222 #define CNOID_SIGNAL_ARGS_AS_MEMBERS T1 a1;T2 a2;
223 #define CNOID_SIGNAL_COPY_PARMS T1 ia1, T2 ia2
224 #define CNOID_SIGNAL_INIT_ARGS :a1(ia1), a2(ia2)
225 
226 #include "SignalTemplate.h"
227 
228 #undef CNOID_SIGNAL_INIT_ARGS
229 #undef CNOID_SIGNAL_COPY_PARMS
230 #undef CNOID_SIGNAL_ARGS_AS_MEMBERS
231 #undef CNOID_SIGNAL_ARGS
232 #undef CNOID_SIGNAL_PARMS
233 #undef CNOID_SIGNAL_TEMPLATE_ARGS
234 #undef CNOID_SIGNAL_TEMPLATE_PARMS
235 #undef CNOID_SIGNAL_NUM_ARGS
236 
237 #define CNOID_SIGNAL_NUM_ARGS 3
238 #define CNOID_SIGNAL_TEMPLATE_PARMS typename T1, typename T2, typename T3
239 #define CNOID_SIGNAL_TEMPLATE_ARGS T1, T2, T3
240 #define CNOID_SIGNAL_PARMS T1 a1, T2 a2, T3 a3
241 #define CNOID_SIGNAL_ARGS a1, a2, a3
242 #define CNOID_SIGNAL_ARGS_AS_MEMBERS T1 a1;T2 a2;T3 a3;
243 #define CNOID_SIGNAL_COPY_PARMS T1 ia1, T2 ia2, T3 ia3
244 #define CNOID_SIGNAL_INIT_ARGS :a1(ia1), a2(ia2), a3(ia3)
245 
246 #include "SignalTemplate.h"
247 
248 #undef CNOID_SIGNAL_INIT_ARGS
249 #undef CNOID_SIGNAL_COPY_PARMS
250 #undef CNOID_SIGNAL_ARGS_AS_MEMBERS
251 #undef CNOID_SIGNAL_ARGS
252 #undef CNOID_SIGNAL_PARMS
253 #undef CNOID_SIGNAL_TEMPLATE_ARGS
254 #undef CNOID_SIGNAL_TEMPLATE_PARMS
255 #undef CNOID_SIGNAL_NUM_ARGS
256 
257 
258 namespace cnoid {
259 
260 namespace signal_private {
261 
262 template<int Arity, typename Signature, typename Combiner>
264 
265 template<typename Signature, typename Combiner>
266 class real_get_signal_impl<0, Signature, Combiner>
267 {
268  typedef boost::function_traits<Signature> traits;
269 public:
270  typedef Signal0<typename traits::result_type, Combiner> type;
271 };
272 
273 template<typename Signature,typename Combiner>
274 class real_get_signal_impl<1, Signature, Combiner>
275 {
276  typedef boost::function_traits<Signature> traits;
277 public:
278  typedef Signal1<typename traits::result_type,
279  typename traits::arg1_type,
280  Combiner> type;
281 };
282 
283 template<typename Signature,typename Combiner>
284 class real_get_signal_impl<2, Signature, Combiner>
285 {
286  typedef boost::function_traits<Signature> traits;
287 public:
288  typedef Signal2<typename traits::result_type,
289  typename traits::arg1_type,
290  typename traits::arg2_type,
291  Combiner> type;
292 };
293 
294 
295 template<typename Signature, typename Combiner>
296 class real_get_signal_impl<3, Signature, Combiner>
297 {
298  typedef boost::function_traits<Signature> traits;
299 public:
300  typedef Signal3<typename traits::result_type,
301  typename traits::arg1_type,
302  typename traits::arg2_type,
303  typename traits::arg3_type,
304  Combiner> type;
305 };
306 
307 template<typename Signature, typename Combiner>
309  (boost::function_traits<Signature>::arity), Signature, Combiner>
310 {
311 
312 };
313 
314 } // namespace signal_private
315 
316 template<
317  typename TSignature,
319  >
320 class Signal : public signal_private::get_signal_impl<TSignature, Combiner>::type
321 {
322 public:
323  typedef TSignature Signature;
324 };
325 
326 
327 template<typename Signal> struct signal_traits { };
328 
329 template<typename Signature, typename Combiner>
330 struct signal_traits< Signal<Signature, Combiner> >
331 {
333 };
334 
335 
336 // for boost.signals
337 /*
338 template<typename Signature, typename Combiner, typename Group, typename GroupCompare, typename SlotFunction>
339 struct signal_traits< boost::signal<Signature, Combiner, Group, GroupCompare, SlotFunction> >
340 {
341  typedef boost::signals::connection connection_type;
342 };
343 */
344 
345 
347 {
348 public:
349  typedef bool result_type;
350  template<typename InputIterator>
351  bool operator()(InputIterator first, InputIterator last) const {
352  bool result = true;
353  while(first != last){
354  result &= *first++;
355  }
356  return result;
357  }
358 };
359 
360 
362 {
363 public:
364  typedef bool result_type;
365  template<typename InputIterator>
366  bool operator()(InputIterator first, InputIterator last) const {
367  bool result = false;
368  while(first != last){
369  result |= *first++;
370  }
371  return result;
372  }
373 };
374 
375 
376 template<
377  typename Signature,
379  >
381 {
382 public:
384 
385  SignalProxy() : signal(0) { }
386  SignalProxy(SignalType& signal) : signal(&signal) { }
387  SignalProxy(const SignalProxy& org) : signal(org.signal) { }
388 
389  SignalProxy& operator=(const SignalProxy& rhs) { signal = rhs.signal; }
390 
392 
393  connection_type connect(typename SignalType::slot_function_type f){
394  if(signal){
395  return signal->connect(f);
396  } else {
397  return connection_type();
398  }
399  };
400 
401 private:
402  SignalType* signal;
403 };
404 
405 } // namespace cnoid;
406 
407 #endif
SlotCallIterator(const SlotCallIterator &org)
Definition: Signal.h:81
SlotCallIterator(SlotType *firstSlot, ArgSetType &args)
Definition: Signal.h:76
Connection connection_type
Definition: Signal.h:332
ScopedConnection(const Connection &org)
Definition: Signal.h:160
ScopedConnection & changeOrder(Order order)
Definition: Signal.h:167
result_type operator*() const
Definition: Signal.h:101
connection_type connect(typename SignalType::slot_function_type f)
Definition: Signal.h:393
bool connected()
Definition: Signal.h:130
SignalProxy()
Definition: Signal.h:385
ScopedConnection()
Definition: Signal.h:159
void block()
Definition: Signal.h:134
bool connected()
Definition: Signal.h:164
Connection & changeOrder(Order order)
Definition: Signal.h:148
Definition: Signal.h:156
Definition: Referenced.h:67
void disconnect()
Definition: Signal.h:123
void reset(const Connection &c)
Definition: Signal.h:162
void unblock()
Definition: Signal.h:166
Connection & operator=(const Connection &rhs)
Definition: Signal.h:118
Definition: Referenced.h:128
Signal1< typename traits::result_type, typename traits::arg1_type, Combiner > type
Definition: Signal.h:280
void operator()(InputIterator first, InputIterator last) const
Definition: Signal.h:41
bool operator==(const SlotCallIterator &rhs) const
Definition: Signal.h:86
Signal0< typename traits::result_type, Combiner > type
Definition: Signal.h:270
Definition: Signal.h:327
bool result_type
Definition: Signal.h:349
Connection(signal_private::SlotHolderBase *slot)
Definition: Signal.h:114
virtual ~SlotHolderBase()
Definition: Signal.h:52
Signal2< typename traits::result_type, typename traits::arg1_type, typename traits::arg2_type, Combiner > type
Definition: Signal.h:291
SlotCallIterator operator++(int)
Definition: Signal.h:94
Defines the minimum processing for performing pasing file for STL.
Definition: AbstractSceneLoader.h:9
T result_type
Definition: Signal.h:25
signal_traits< SignalType >::connection_type connection_type
Definition: Signal.h:391
Definition: Signal.h:107
SignalProxy(const SignalProxy &org)
Definition: Signal.h:387
Connection(const Connection &org)
Definition: Signal.h:116
T operator()(InputIterator first, InputIterator last) const
Definition: Signal.h:28
TSignature Signature
Definition: Signal.h:323
Definition: Signal.h:346
bool operator!=(const SlotCallIterator &rhs) const
Definition: Signal.h:90
Signal3< typename traits::result_type, typename traits::arg1_type, typename traits::arg2_type, typename traits::arg3_type, Combiner > type
Definition: Signal.h:304
SignalProxy(SignalType &signal)
Definition: Signal.h:386
bool result_type
Definition: Signal.h:364
SlotHolderBase()
Definition: Signal.h:51
Connection()
Definition: Signal.h:112
void unblock()
Definition: Signal.h:140
bool operator()(InputIterator first, InputIterator last) const
Definition: Signal.h:366
Definition: Signal.h:320
void seekUnblockedSlot()
Definition: Signal.h:70
bool operator()(InputIterator first, InputIterator last) const
Definition: Signal.h:351
bool isBlocked
Definition: Signal.h:57
Definition: Signal.h:361
Definition: Signal.h:380
~ScopedConnection()
Definition: Signal.h:161
Definition: Signal.h:24
void block()
Definition: Signal.h:165
void disconnect()
Definition: Signal.h:163
Order
Definition: Signal.h:146
SignalProxy & operator=(const SignalProxy &rhs)
Definition: Signal.h:389
Signal< Signature, Combiner > SignalType
Definition: Signal.h:383