Nymph  v1.5.2
Flow-Based Data Processing Framework
KTSlot.hh
Go to the documentation of this file.
1 /*
2  * KTSlot.hh
3  *
4  * Created on: Jan 13, 2013
5  * Author: nsoblath
6  */
7 
8 #ifndef KTSLOT_HH_
9 #define KTSLOT_HH_
10 
11 #include "KTData.hh"
12 #include "KTLogger.hh"
13 #include "KTSignal.hh"
14 
15 #include <boost/bind.hpp>
16 #include <boost/function.hpp>
17 
18 #include <string>
19 
20 namespace Nymph
21 {
22  KTLOGGER(slotlog, "KTSlot");
23 
24  template< typename Signature>
26  {
27  public:
28  typedef boost::function< Signature > function_signature;
29  typedef typename function_signature::result_type return_type;
30 
31  public:
33  template< class XFuncOwnerType >
34  KTSlotNoArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)());
36  template< class XFuncOwnerType >
37  KTSlotNoArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)());
38  virtual ~KTSlotNoArg();
39 
40  return_type operator()();
41 
42  protected:
43  boost::function< Signature > fFunc;
44 
45  };
46 
64  template< typename Signature>
66  {
67  public:
68  typedef boost::function< Signature > function_signature;
69  typedef typename function_signature::result_type return_type;
70  typedef typename function_signature::argument_type argument_type;
71 
72  public:
74  template< class XFuncOwnerType >
75  KTSlotOneArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(argument_type));
77  template< class XFuncOwnerType >
78  KTSlotOneArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(argument_type));
79  virtual ~KTSlotOneArg();
80 
81  return_type operator()(argument_type arg);
82 
83  protected:
84  boost::function< Signature > fFunc;
85 
86  };
87 
88  template< typename Signature>
90  {
91  public:
92  typedef boost::function< Signature > function_signature;
93  typedef typename function_signature::result_type return_type;
94  typedef typename function_signature::first_argument_type first_argument_type;
95  typedef typename function_signature::second_argument_type second_argument_type;
96 
97  public:
99  template< class XFuncOwnerType >
100  KTSlotTwoArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(first_argument_type, second_argument_type));
102  template< class XFuncOwnerType >
103  KTSlotTwoArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(first_argument_type, second_argument_type));
104  virtual ~KTSlotTwoArg();
105 
106  return_type operator()(first_argument_type arg1, second_argument_type arg2);
107 
108  protected:
109  boost::function< Signature > fFunc;
110 
111  };
112 
113 
114 
115  template< typename Signature>
116  template< class XFuncOwnerType >
117  KTSlotNoArg< Signature >::KTSlotNoArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)()) :
118  fFunc(boost::bind(func, owner))
119  {
120  owner->RegisterSlot(name, this, &KTSlotNoArg::operator());
121  }
122 
123  template< typename Signature>
124  template< class XFuncOwnerType >
125  KTSlotNoArg< Signature >::KTSlotNoArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)()) :
126  fFunc(boost::bind(func, owner))
127  {
128  proc->RegisterSlot(name, this, &KTSlotNoArg::operator());
129  }
130 
131  template< typename Signature>
133  {
134  }
135 
136  template< typename Signature>
138  {
139  return fFunc();
140  }
141 
142 
143 
144 
145  template< typename Signature>
146  template< class XFuncOwnerType >
147  KTSlotOneArg< Signature >::KTSlotOneArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(argument_type)) :
148  fFunc(boost::bind(func, owner, _1))
149  {
150  owner->RegisterSlot(name, this, &KTSlotOneArg::operator());
151  }
152 
153  template< typename Signature>
154  template< class XFuncOwnerType >
155  KTSlotOneArg< Signature >::KTSlotOneArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(argument_type)) :
156  fFunc(boost::bind(func, owner, _1))
157  {
158  proc->RegisterSlot(name, this, &KTSlotOneArg::operator());
159  }
160 
161  template< typename Signature>
163  {
164  }
165 
166  template< typename Signature>
168  {
169  return fFunc(arg);
170  }
171 
172 
173 
174 
175 
176  template< typename Signature>
177  template< class XFuncOwnerType >
178  KTSlotTwoArg< Signature >::KTSlotTwoArg(const std::string& name, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(first_argument_type, second_argument_type)) :
179  fFunc(boost::bind(func, owner, _1, _2))
180  {
181  owner->RegisterSlot(name, this, &KTSlotTwoArg::operator());
182  }
183 
184  template< typename Signature>
185  template< class XFuncOwnerType >
186  KTSlotTwoArg< Signature >::KTSlotTwoArg(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, return_type (XFuncOwnerType::*func)(first_argument_type, second_argument_type)) :
187  fFunc(boost::bind(func, owner, _1, _2))
188  {
189  proc->RegisterSlot(name, this, &KTSlotTwoArg::operator());
190  }
191 
192  template< typename Signature>
194  {
195  }
196 
197  template< typename Signature>
198  typename KTSlotTwoArg< Signature >::return_type KTSlotTwoArg< Signature >::operator()(first_argument_type arg1, second_argument_type arg2)
199  {
200  return fFunc(arg1, arg2);
201  }
202 
203 
204 
205 
206 
207 
228  template< class XDataType >
230  {
231  public:
232  typedef XDataType data_type;
233  typedef boost::function< void (KTDataPtr) > function_signature;
234  typedef typename function_signature::result_type return_type;
235  typedef typename function_signature::argument_type argument_type;
236 
237  public:
239  template< class XFuncOwnerType >
240  KTSlotDataOneType(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(data_type&), KTSignalData* signalPtr=NULL);
242  template< class XFuncOwnerType >
243  KTSlotDataOneType(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(data_type&), KTSignalData* signalPtr=NULL);
244  virtual ~KTSlotDataOneType();
245 
246  void operator()(KTDataPtr data);
247 
248  protected:
249  boost::function< bool (data_type&) > fFunc;
250 
252  };
253 
254 
255  template< class XDataType1, class XDataType2 >
257  {
258  public:
259  typedef XDataType1 first_data_type;
260  typedef XDataType2 second_data_type;
261  typedef boost::function< void (KTDataPtr) > function_signature;
262  typedef typename function_signature::result_type return_type;
263  typedef typename function_signature::argument_type argument_type;
264 
265  public:
267  template< class XFuncOwnerType >
268  KTSlotDataTwoTypes(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&), KTSignalData* signalPtr=NULL);
270  template< class XFuncOwnerType >
271  KTSlotDataTwoTypes(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&), KTSignalData* signalPtr=NULL);
272  virtual ~KTSlotDataTwoTypes();
273 
274  void operator()(KTDataPtr data);
275 
276  protected:
277  boost::function< bool (first_data_type&, second_data_type&) > fFunc;
278 
280  };
281 
282 
283  template< class XDataType1, class XDataType2, class XDataType3 >
285  {
286  public:
287  typedef XDataType1 first_data_type;
288  typedef XDataType2 second_data_type;
289  typedef XDataType3 third_data_type;
290  typedef boost::function< void (KTDataPtr) > function_signature;
291  typedef typename function_signature::result_type return_type;
292  typedef typename function_signature::argument_type argument_type;
293 
294  public:
296  template< class XFuncOwnerType >
297  KTSlotDataThreeTypes(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&, third_data_type&), KTSignalData* signalPtr=NULL);
299  template< class XFuncOwnerType >
300  KTSlotDataThreeTypes(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&, third_data_type&), KTSignalData* signalPtr=NULL);
301  virtual ~KTSlotDataThreeTypes();
302 
303  void operator()(KTDataPtr data);
304 
305  protected:
306  boost::function< bool (first_data_type&, second_data_type&, third_data_type&) > fFunc;
307 
309  };
310 
311 
312 
313  template< class XDataType >
314  template< class XFuncOwnerType >
315  KTSlotDataOneType< XDataType >::KTSlotDataOneType(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(data_type&), KTSignalData* signalPtr) :
316  fFunc(boost::bind(func, owner, _1)),
317  fSignalPtr(signalPtr)
318  {
319  owner->RegisterSlot(name, this, &KTSlotDataOneType::operator());
320  }
321 
322  template< class XDataType >
323  template< class XFuncOwnerType >
324  KTSlotDataOneType< XDataType >::KTSlotDataOneType(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(data_type&), KTSignalData* signalPtr) :
325  fFunc(boost::bind(func, owner, _1)),
326  fSignalPtr(signalPtr)
327  {
328  proc->RegisterSlot(name, this, &KTSlotDataOneType::operator());
329  }
330 
331  template< class XDataType >
333  {
334  }
335 
336  template< class XDataType >
338  {
339  // Standard data slot pattern:
340  // Check to ensure that the required data type is present
341  if (! data->Has< data_type >())
342  {
343  KTERROR(slotlog, "Data not found with type <" << typeid(data_type).name() << ">");
344  return;
345  }
346  // Call the function
347  if (! fFunc(data->Of< data_type >()))
348  {
349  KTERROR(slotlog, "Something went wrong while analyzing data with type <" << typeid(data_type).name() << ">");
350  return;
351  }
352  // If there's a signal pointer, emit the signal
353  if (fSignalPtr != NULL)
354  {
355  (*fSignalPtr)(data);
356  }
357  return;
358  }
359 
360 
361 
362  template< class XDataType1, class XDataType2 >
363  template< class XFuncOwnerType >
364  KTSlotDataTwoTypes< XDataType1, XDataType2 >::KTSlotDataTwoTypes(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&), KTSignalData* signalPtr) :
365  fFunc(boost::bind(func, owner, _1, _2)),
366  fSignalPtr(signalPtr)
367  {
368  owner->RegisterSlot(name, this, &KTSlotDataTwoTypes::operator());
369  }
370 
371  template< class XDataType1, class XDataType2 >
372  template< class XFuncOwnerType >
373  KTSlotDataTwoTypes< XDataType1, XDataType2 >::KTSlotDataTwoTypes(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&), KTSignalData* signalPtr) :
374  fFunc(boost::bind(func, owner, _1, _2)),
375  fSignalPtr(signalPtr)
376  {
377  proc->RegisterSlot(name, this, &KTSlotDataTwoTypes::operator());
378  }
379 
380  template< class XDataType1, class XDataType2 >
382  {
383  }
384 
385  template< class XDataType1, class XDataType2 >
387  {
388  // Standard data slot pattern:
389  // Check to ensure that the required data type is present
390  if (! data->Has< first_data_type >())
391  {
392  KTERROR(slotlog, "Data not found with type <" << typeid(first_data_type).name() << ">");
393  return;
394  }
395  if (! data->Has< second_data_type >())
396  {
397  KTERROR(slotlog, "Data not found with type <" << typeid(second_data_type).name() << ">");
398  return;
399  }
400  // Call the function
401  if (! fFunc(data->Of< first_data_type >(), data->Of< second_data_type >()))
402  {
403  KTERROR(slotlog, "Something went wrong while analyzing data with types <" << typeid(first_data_type).name() << "> and <" << typeid(second_data_type).name() << ">");
404  return;
405  }
406  // If there's a signal pointer, emit the signal
407  if (fSignalPtr != NULL)
408  {
409  (*fSignalPtr)(data);
410  }
411  return;
412  }
413 
414 
415  template< class XDataType1, class XDataType2, class XDataType3 >
416  template< class XFuncOwnerType >
417  KTSlotDataThreeTypes< XDataType1, XDataType2, XDataType3 >::KTSlotDataThreeTypes(const std::string& name, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&, third_data_type&), KTSignalData* signalPtr) :
418  fFunc(boost::bind(func, owner, _1, _2, _3)),
419  fSignalPtr(signalPtr)
420  {
421  owner->RegisterSlot(name, this, &KTSlotDataThreeTypes::operator());
422  }
423 
424  template< class XDataType1, class XDataType2, class XDataType3 >
425  template< class XFuncOwnerType >
426  KTSlotDataThreeTypes< XDataType1, XDataType2, XDataType3 >::KTSlotDataThreeTypes(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, bool (XFuncOwnerType::*func)(first_data_type&, second_data_type&, third_data_type&), KTSignalData* signalPtr) :
427  fFunc(boost::bind(func, owner, _1, _2, _3)),
428  fSignalPtr(signalPtr)
429  {
430  proc->RegisterSlot(name, this, &KTSlotDataThreeTypes::operator());
431  }
432 
433  template< class XDataType1, class XDataType2, class XDataType3 >
435  {
436  }
437 
438  template< class XDataType1, class XDataType2, class XDataType3 >
440  {
441  // Standard data slot pattern:
442  // Check to ensure that the required data type is present
443  if (! data->Has< first_data_type >())
444  {
445  KTERROR(slotlog, "Data not found with type <" << typeid(first_data_type).name() << ">");
446  return;
447  }
448  if (! data->Has< second_data_type >())
449  {
450  KTERROR(slotlog, "Data not found with type <" << typeid(second_data_type).name() << ">");
451  return;
452  }
453  if (! data->Has< third_data_type >())
454  {
455  KTERROR(slotlog, "Data not found with type <" << typeid(third_data_type).name() << ">");
456  return;
457  }
458  // Call the function
459  if (! fFunc(data->Of< first_data_type >(), data->Of< second_data_type >(), data->Of< third_data_type >()))
460  {
461  KTERROR(slotlog, "Something went wrong while analyzing data with types <" << typeid(first_data_type).name() << ">, <" << typeid(second_data_type).name() << ">, and <" << typeid(third_data_type).name() << ">");
462  return;
463  }
464  // If there's a signal pointer, emit the signal
465  if (fSignalPtr != NULL)
466  {
467  (*fSignalPtr)(data);
468  }
469  return;
470  }
471 
472 
494  {
495  public:
496  typedef boost::function< void () > function_signature;
497  typedef bool return_type;
498 
499  public:
501  template< class XFuncOwnerType >
502  KTSlotDone(const std::string& name, XFuncOwnerType* owner, void (XFuncOwnerType::*func)(), KTSignalDone* signalPtr=NULL);
504  template< class XFuncOwnerType >
505  KTSlotDone(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, void (XFuncOwnerType::*func)(), KTSignalDone* signalPtr=NULL);
506  virtual ~KTSlotDone();
507 
508  void operator()();
509 
510  protected:
511  boost::function< void () > fFunc;
512 
514  };
515 
516  template< class XFuncOwnerType >
517  KTSlotDone::KTSlotDone(const std::string& name, XFuncOwnerType* owner, void (XFuncOwnerType::*func)(), KTSignalDone* signalPtr) :
518  fFunc(boost::bind(func, owner)),
519  fSignalPtr(signalPtr)
520  {
521  owner->RegisterSlot(name, this, &KTSlotDone::operator());
522  }
523 
524  template< class XFuncOwnerType >
525  KTSlotDone::KTSlotDone(const std::string& name, KTProcessor* proc, XFuncOwnerType* owner, void (XFuncOwnerType::*func)(), KTSignalDone* signalPtr) :
526  fFunc(boost::bind(func, owner)),
527  fSignalPtr(signalPtr)
528  {
529  proc->RegisterSlot(name, this, &KTSlotDone::operator());
530  }
531 
533  {
534  }
535 
537  {
538  // Call the function
539  fFunc();
540 
541  // If there's a signal pointer, emit the signal
542  if (fSignalPtr != NULL)
543  {
544  (*fSignalPtr)();
545  }
546  return;
547  }
548 
549 
550 
551 } /* namespace Nymph */
552 #endif /* KTSLOT_HH_ */
function_signature::argument_type argument_type
Definition: KTSlot.hh:292
function_signature::result_type return_type
Definition: KTSlot.hh:93
return_type operator()(first_argument_type arg1, second_argument_type arg2)
Definition: KTSlot.hh:198
KTSlotOneArg(const std::string &name, XFuncOwnerType *owner, return_type(XFuncOwnerType::*func)(argument_type))
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:147
function_signature::argument_type argument_type
Definition: KTSlot.hh:263
function_signature::argument_type argument_type
Definition: KTSlot.hh:70
boost::function< void(KTDataPtr) > function_signature
Definition: KTSlot.hh:233
boost::function< Signature > function_signature
Definition: KTSlot.hh:92
KTSlotDataOneType(const std::string &name, XFuncOwnerType *owner, bool(XFuncOwnerType::*func)(data_type &), KTSignalData *signalPtr=NULL)
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:315
boost::function< Signature > fFunc
Definition: KTSlot.hh:43
KTSlotTwoArg(const std::string &name, XFuncOwnerType *owner, return_type(XFuncOwnerType::*func)(first_argument_type, second_argument_type))
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:178
function_signature::argument_type argument_type
Definition: KTSlot.hh:235
void operator()(KTDataPtr data)
Definition: KTSlot.hh:439
void RegisterSlot(std::string name, XTarget *target, XReturn(XTarget::*funcPtr)())
Definition: KTProcessor.hh:96
Creates a slot that calls a member function of the func_owner_type object, taking one argument...
Definition: KTSlot.hh:65
virtual ~KTSlotTwoArg()
Definition: KTSlot.hh:193
boost::function< Signature > fFunc
Definition: KTSlot.hh:109
boost::function< bool(first_data_type &, second_data_type &) > fFunc
Definition: KTSlot.hh:277
KTSlotNoArg(const std::string &name, XFuncOwnerType *owner, return_type(XFuncOwnerType::*func)())
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:117
boost::function< void() > fFunc
Definition: KTSlot.hh:511
boost::function< bool(first_data_type &, second_data_type &, third_data_type &) > fFunc
Definition: KTSlot.hh:306
boost::function< Signature > function_signature
Definition: KTSlot.hh:68
KTSlotDataTwoTypes(const std::string &name, XFuncOwnerType *owner, bool(XFuncOwnerType::*func)(first_data_type &, second_data_type &), KTSignalData *signalPtr=NULL)
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:364
KTSignalData * fSignalPtr
Definition: KTSlot.hh:251
function_signature::result_type return_type
Definition: KTSlot.hh:29
virtual ~KTSlotNoArg()
Definition: KTSlot.hh:132
KTSlotDone(const std::string &name, XFuncOwnerType *owner, void(XFuncOwnerType::*func)(), KTSignalDone *signalPtr=NULL)
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:517
Creates a signal that takes a KTDataPtr object as its argument.
Definition: KTSignal.hh:119
boost::function< void(KTDataPtr) > function_signature
Definition: KTSlot.hh:261
return_type operator()()
Definition: KTSlot.hh:137
XDataType1 first_data_type
Definition: KTSlot.hh:287
boost::function< Signature > fFunc
Definition: KTSlot.hh:84
boost::function< void() > function_signature
Definition: KTSlot.hh:496
KTLOGGER(applog, "KTApplication")
virtual ~KTSlotDone()
Definition: KTSlot.hh:532
XDataType3 third_data_type
Definition: KTSlot.hh:289
boost::function< bool(data_type &) > fFunc
Definition: KTSlot.hh:249
function_signature::result_type return_type
Definition: KTSlot.hh:262
KTSignalData * fSignalPtr
Definition: KTSlot.hh:308
KTSignalDone * fSignalPtr
Definition: KTSlot.hh:513
boost::function< Signature > function_signature
Definition: KTSlot.hh:28
XDataType2 second_data_type
Definition: KTSlot.hh:288
function_signature::first_argument_type first_argument_type
Definition: KTSlot.hh:94
KTSignalData * fSignalPtr
Definition: KTSlot.hh:279
virtual ~KTSlotDataTwoTypes()
Definition: KTSlot.hh:381
function_signature::second_argument_type second_argument_type
Definition: KTSlot.hh:95
boost::shared_ptr< KTData > KTDataPtr
Definition: KTData.hh:67
KTSlotDataThreeTypes(const std::string &name, XFuncOwnerType *owner, bool(XFuncOwnerType::*func)(first_data_type &, second_data_type &, third_data_type &), KTSignalData *signalPtr=NULL)
Constructor for the case where the processor has the function that will be called by the slot...
Definition: KTSlot.hh:417
boost::function< void(KTDataPtr) > function_signature
Definition: KTSlot.hh:290
virtual ~KTSlotDataOneType()
Definition: KTSlot.hh:332
void operator()(KTDataPtr data)
Definition: KTSlot.hh:386
function_signature::result_type return_type
Definition: KTSlot.hh:234
function_signature::result_type return_type
Definition: KTSlot.hh:291
XDataType2 second_data_type
Definition: KTSlot.hh:260
#define KTERROR(...)
Definition: KTLogger.hh:347
void operator()()
Definition: KTSlot.hh:536
function_signature::result_type return_type
Definition: KTSlot.hh:69
virtual ~KTSlotDataThreeTypes()
Definition: KTSlot.hh:434
XDataType1 first_data_type
Definition: KTSlot.hh:259
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
void operator()(KTDataPtr data)
Definition: KTSlot.hh:337