Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMessage.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef mitkMessageHIncluded
18 #define mitkMessageHIncluded
19 
20 #include <functional>
21 #include <itkSimpleFastMutexLock.h>
22 #include <vector>
23 
28 #define mitkNewMessageMacro(msgHandleObject) \
29 private: \
30  ::mitk::Message<> m_##msgHandleObject##Message; \
31  \
32 public: \
33  inline void Add##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate<> &delegate) \
34  { \
35  m_##msgHandleObject##Message += delegate; \
36  } \
37  inline void Remove##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate<> &delegate) \
38  { \
39  m_##msgHandleObject##Message -= delegate; \
40  }
41 
42 #define mitkNewMessageWithReturnMacro(msgHandleObject, returnType) \
43 private: \
44  ::mitk::Message<returnType> m_##msgHandleObject##Message; \
45  \
46 public: \
47  inline void Add##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate<returnType> &delegate) \
48  { \
49  m_##msgHandleObject##Message += delegate; \
50  } \
51  inline void Remove##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate<returnType> &delegate) \
52  { \
53  m_##msgHandleObject##Message -= delegate; \
54  }
55 
56 #define mitkNewMessage1Macro(msgHandleObject, type1) \
57 private: \
58  ::mitk::Message1<type1> m_##msgHandleObject##Message; \
59  \
60 public: \
61  void Add##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate1<type1> &delegate) \
62  { \
63  m_##msgHandleObject##Message += delegate; \
64  } \
65  void Remove##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate1<type1> &delegate) \
66  { \
67  m_##msgHandleObject##Message -= delegate; \
68  }
69 
70 #define mitkNewMessage2Macro(msgHandleObject, type1, type2) \
71 private: \
72  ::mitk::Message2<type1, type2> m_##msgHandleObject##Message; \
73  \
74 public: \
75  void Add##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate2<type1, type2> &delegate) \
76  { \
77  m_##msgHandleObject##Message += delegate; \
78  } \
79  void Remove##msgHandleObject##Listener(const ::mitk::MessageAbstractDelegate2<type1, type2> &delegate) \
80  { \
81  m_##msgHandleObject##Message -= delegate; \
82  }
83 
84 namespace mitk
85 {
86  template <typename A = void>
88  {
89  public:
91  virtual A Execute() const = 0;
92  virtual bool operator==(const MessageAbstractDelegate *cmd) const = 0;
93  virtual MessageAbstractDelegate *Clone() const = 0;
94  };
95 
96  template <typename T, typename A = void>
98  {
99  public:
101  virtual A Execute(T t) const = 0;
102  virtual bool operator==(const MessageAbstractDelegate1 *cmd) const = 0;
103  virtual MessageAbstractDelegate1 *Clone() const = 0;
104  };
105 
106  template <typename T, typename U, typename A = void>
108  {
109  public:
111  virtual A Execute(T t, U u) const = 0;
112  virtual bool operator==(const MessageAbstractDelegate2 *cmd) const = 0;
113  virtual MessageAbstractDelegate2 *Clone() const = 0;
114  };
115 
116  template <typename T, typename U, typename V, typename A = void>
118  {
119  public:
121  virtual A Execute(T t, U u, V v) const = 0;
122  virtual bool operator==(const MessageAbstractDelegate3 *cmd) const = 0;
123  virtual MessageAbstractDelegate3 *Clone() const = 0;
124  };
125 
126  template <typename T, typename U, typename V, typename W, typename A = void>
128  {
129  public:
131  virtual A Execute(T t, U u, V v, W w) const = 0;
132  virtual bool operator==(const MessageAbstractDelegate4 *cmd) const = 0;
133  virtual MessageAbstractDelegate4 *Clone() const = 0;
134  };
135 
144  template <class R, typename A = void>
146  {
147  public:
148  // constructor - takes pointer to an object and pointer to a member and stores
149  // them in two private variables
150  MessageDelegate(R *object, A (R::*memberFunctionPointer)())
151  : m_Object(object), m_MemberFunctionPointer(memberFunctionPointer)
152  {
153  }
154 
155  virtual ~MessageDelegate() {}
156  // override function "Call"
157  virtual A Execute() const
158  {
159  return (m_Object->*m_MemberFunctionPointer)(); // execute member function
160  }
161 
163  {
164  const MessageDelegate<R, A> *cmd = dynamic_cast<const MessageDelegate<R, A> *>(c);
165  if (!cmd)
166  return false;
167 
168  if ((void *)this->m_Object != (void *)cmd->m_Object)
169  return false;
170  if (this->m_MemberFunctionPointer != cmd->m_MemberFunctionPointer)
171  return false;
172  return true;
173  }
174 
175  MessageAbstractDelegate<A> *Clone() const { return new MessageDelegate(m_Object, m_MemberFunctionPointer); }
176  private:
177  R *m_Object; // pointer to object
178  A (R::*m_MemberFunctionPointer)(); // pointer to member function
179  };
180 
192  template <class R, typename T, typename A = void>
194  {
195  public:
196  // constructor - takes pointer to an object and pointer to a member and stores
197  // them in two private variables
198  MessageDelegate1(R *object, A (R::*memberFunctionPointer)(T))
199  : m_Object(object), m_MemberFunctionPointer(memberFunctionPointer)
200  {
201  }
202 
203  virtual ~MessageDelegate1() {}
204  // override function "Call"
205  virtual A Execute(T t) const
206  {
207  return (m_Object->*m_MemberFunctionPointer)(t); // execute member function
208  }
209 
211  {
212  const MessageDelegate1<R, T, A> *cmd = dynamic_cast<const MessageDelegate1<R, T, A> *>(c);
213  if (!cmd)
214  return false;
215 
216  if ((void *)this->m_Object != (void *)cmd->m_Object)
217  return false;
218  if (this->m_MemberFunctionPointer != cmd->m_MemberFunctionPointer)
219  return false;
220  return true;
221  }
222 
223  MessageAbstractDelegate1<T, A> *Clone() const { return new MessageDelegate1(m_Object, m_MemberFunctionPointer); }
224  private:
225  R *m_Object; // pointer to object
226  A (R::*m_MemberFunctionPointer)(T); // pointer to member function
227  };
228 
229  template <class R, typename T, typename U, typename A = void>
231  {
232  public:
233  // constructor - takes pointer to an object and pointer to a member and stores
234  // them in two private variables
235  MessageDelegate2(R *object, A (R::*memberFunctionPointer)(T, U))
236  : m_Object(object), m_MemberFunctionPointer(memberFunctionPointer)
237  {
238  }
239 
240  virtual ~MessageDelegate2() {}
241  // override function "Call"
242  virtual A Execute(T t, U u) const
243  {
244  return (m_Object->*m_MemberFunctionPointer)(t, u); // execute member function
245  }
246 
248  {
249  const MessageDelegate2<R, T, U, A> *cmd = dynamic_cast<const MessageDelegate2<R, T, U, A> *>(c);
250  if (!cmd)
251  return false;
252 
253  if ((void *)this->m_Object != (void *)cmd->m_Object)
254  return false;
255  if (this->m_MemberFunctionPointer != cmd->m_MemberFunctionPointer)
256  return false;
257  return true;
258  }
259 
260  MessageAbstractDelegate2<T, U, A> *Clone() const { return new MessageDelegate2(m_Object, m_MemberFunctionPointer); }
261  private:
262  R *m_Object; // pointer to object
263  A (R::*m_MemberFunctionPointer)(T, U); // pointer to member function
264  };
265 
266  template <class R, typename T, typename U, typename V, typename A = void>
267  class MessageDelegate3 : public MessageAbstractDelegate3<T, U, V, A>
268  {
269  public:
270  // constructor - takes pointer to an object and pointer to a member and stores
271  // them in two private variables
272  MessageDelegate3(R *object, A (R::*memberFunctionPointer)(T, U, V))
273  : m_Object(object), m_MemberFunctionPointer(memberFunctionPointer)
274  {
275  }
276 
277  virtual ~MessageDelegate3() {}
278  // override function "Call"
279  virtual A Execute(T t, U u, V v) const
280  {
281  return (m_Object->*m_MemberFunctionPointer)(t, u, v); // execute member function
282  }
283 
285  {
286  const MessageDelegate3<R, T, U, V, A> *cmd = dynamic_cast<const MessageDelegate3<R, T, U, V, A> *>(c);
287  if (!cmd)
288  return false;
289 
290  if ((void *)this->m_Object != (void *)cmd->m_Object)
291  return false;
292  if (this->m_MemberFunctionPointer != cmd->m_MemberFunctionPointer)
293  return false;
294  return true;
295  }
296 
298  {
299  return new MessageDelegate3(m_Object, m_MemberFunctionPointer);
300  }
301 
302  private:
303  R *m_Object; // pointer to object
304  A (R::*m_MemberFunctionPointer)(T, U, V); // pointer to member function
305  };
306 
307  template <class R, typename T, typename U, typename V, typename W, typename A = void>
308  class MessageDelegate4 : public MessageAbstractDelegate4<T, U, V, W, A>
309  {
310  public:
311  // constructor - takes pointer to an object and pointer to a member and stores
312  // them in two private variables
313  MessageDelegate4(R *object, A (R::*memberFunctionPointer)(T, U, V, W))
314  : m_Object(object), m_MemberFunctionPointer(memberFunctionPointer)
315  {
316  }
317 
318  virtual ~MessageDelegate4() {}
319  // override function "Call"
320  virtual A Execute(T t, U u, V v, W w) const
321  {
322  return (m_Object->*m_MemberFunctionPointer)(t, u, v, w); // execute member function
323  }
324 
326  {
327  const MessageDelegate4<R, T, U, V, W, A> *cmd = dynamic_cast<const MessageDelegate4<R, T, U, V, W, A> *>(c);
328  if (!cmd)
329  return false;
330 
331  if ((void *)this->m_Object != (void *)cmd->m_Object)
332  return false;
333  if (this->m_MemberFunctionPointer != cmd->m_MemberFunctionPointer)
334  return false;
335  return true;
336  }
337 
339  {
340  return new MessageDelegate4(m_Object, m_MemberFunctionPointer);
341  }
342 
343  private:
344  R *m_Object; // pointer to object
345  A (R::*m_MemberFunctionPointer)(T, U, V, W); // pointer to member function
346  };
347 
348  template <typename AbstractDelegate>
350  {
351  public:
352  typedef std::vector<AbstractDelegate *> ListenerList;
353 
354  virtual ~MessageBase()
355  {
356  for (auto iter = m_Listeners.begin(); iter != m_Listeners.end(); ++iter)
357  {
358  delete *iter;
359  }
360  }
361 
364  {
365  for (typename ListenerList::iterator iter = o.m_Listeners.begin(); iter != o.m_Listeners.end(); ++iter)
366  {
367  m_Listeners.push_back((*iter)->Clone());
368  }
369  }
370 
372  {
373  MessageBase tmp(o);
374  std::swap(tmp.m_Listeners, this->m_Listeners);
375  return *this;
376  }
377 
378  void AddListener(const AbstractDelegate &delegate) const
379  {
380  AbstractDelegate *msgCmd = delegate.Clone();
381 
382  m_Mutex.Lock();
383  for (auto iter = m_Listeners.begin(); iter != m_Listeners.end(); ++iter)
384  {
385  if ((*iter)->operator==(msgCmd))
386  {
387  delete msgCmd;
388  m_Mutex.Unlock();
389  return;
390  }
391  }
392  m_Listeners.push_back(msgCmd);
393  m_Mutex.Unlock();
394  }
395 
396  void operator+=(const AbstractDelegate &delegate) const { this->AddListener(delegate); }
397  void RemoveListener(const AbstractDelegate &delegate) const
398  {
399  m_Mutex.Lock();
400  for (auto iter = m_Listeners.begin(); iter != m_Listeners.end(); ++iter)
401  {
402  if ((*iter)->operator==(&delegate))
403  {
404  delete *iter;
405  m_Listeners.erase(iter);
406  m_Mutex.Unlock();
407  return;
408  }
409  }
410  m_Mutex.Unlock();
411  }
412 
413  void operator-=(const AbstractDelegate &delegate) const { this->RemoveListener(delegate); }
414  const ListenerList &GetListeners() const { return m_Listeners; }
415  bool HasListeners() const { return !m_Listeners.empty(); }
416  bool IsEmpty() const { return m_Listeners.empty(); }
417  protected:
435  mutable ListenerList m_Listeners;
436  mutable itk::SimpleFastMutexLock m_Mutex;
437  };
438 
571  template <typename A = void>
572  class Message : public MessageBase<MessageAbstractDelegate<A>>
573  {
574  public:
577 
578  void Send()
579  {
580  ListenerList listeners;
581 
582  {
583  this->m_Mutex.Lock();
584  listeners.assign(this->m_Listeners.begin(), this->m_Listeners.end());
585  this->m_Mutex.Unlock();
586  }
587 
588  for (auto iter = listeners.begin(); iter != listeners.end(); ++iter)
589  {
590  // notify each listener
591  (*iter)->Execute();
592  }
593  }
594 
595  void operator()() { this->Send(); }
596  };
597 
598  // message with 1 parameter and return type
599  template <typename T, typename A = void>
600  class Message1 : public MessageBase<MessageAbstractDelegate1<T, A>>
601  {
602  public:
605 
606  void Send(T t)
607  {
608  ListenerList listeners;
609 
610  {
611  this->m_Mutex.Lock();
612  listeners.assign(this->m_Listeners.begin(), this->m_Listeners.end());
613  this->m_Mutex.Unlock();
614  }
615 
616  for (auto iter = listeners.begin(); iter != listeners.end(); ++iter)
617  {
618  // notify each listener
619  (*iter)->Execute(t);
620  }
621  }
622 
623  void operator()(T t) { this->Send(t); }
624  };
625 
626  // message with 2 parameters and return type
627  template <typename T, typename U, typename A = void>
628  class Message2 : public MessageBase<MessageAbstractDelegate2<T, U, A>>
629  {
630  public:
633 
634  void Send(T t, U u)
635  {
636  ListenerList listeners;
637 
638  {
639  this->m_Mutex.Lock();
640  listeners.assign(this->m_Listeners.begin(), this->m_Listeners.end());
641  this->m_Mutex.Unlock();
642  }
643 
644  for (auto iter = listeners.begin(); iter != listeners.end(); ++iter)
645  {
646  // notify each listener
647  (*iter)->Execute(t, u);
648  }
649  }
650 
651  void operator()(T t, U u) { this->Send(t, u); }
652  };
653 
654  // message with 3 parameters and return type
655  template <typename T, typename U, typename V, typename A = void>
656  class Message3 : public MessageBase<MessageAbstractDelegate3<T, U, V, A>>
657  {
658  public:
661 
662  void Send(T t, U u, V v)
663  {
664  ListenerList listeners;
665 
666  {
667  this->m_Mutex.Lock();
668  listeners.assign(this->m_Listeners.begin(), this->m_Listeners.end());
669  this->m_Mutex.Unlock();
670  }
671 
672  for (typename ListenerList::iterator iter = listeners.begin(); iter != listeners.end(); ++iter)
673  {
674  // notify each listener
675  (*iter)->Execute(t, u, v);
676  }
677  }
678 
679  void operator()(T t, U u, V v) { this->Send(t, u, v); }
680  };
681 
682  // message with 4 parameters and return type
683  template <typename T, typename U, typename V, typename W, typename A = void>
684  class Message4 : public MessageBase<MessageAbstractDelegate4<T, U, V, W>>
685  {
686  public:
689 
690  void Send(T t, U u, V v, W w)
691  {
692  ListenerList listeners;
693 
694  {
695  this->m_Mutex.Lock();
696  listeners.assign(this->m_Listeners.begin(), this->m_Listeners.end());
697  this->m_Mutex.Unlock();
698  }
699 
700  for (typename ListenerList::iterator iter = listeners.begin(); iter != listeners.end(); ++iter)
701  {
702  // notify each listener
703  (*iter)->Execute(t, u, v, w);
704  }
705  }
706 
707  void operator()(T t, U u, V v, W w) { this->Send(t, u, v, w); }
708  };
709 
710 } // namespace
711 
712 #endif
Super::ListenerList ListenerList
Definition: mitkMessage.h:688
void RemoveListener(const AbstractDelegate &delegate) const
Definition: mitkMessage.h:397
MessageDelegate(R *object, A(R::*memberFunctionPointer)())
Definition: mitkMessage.h:150
void AddListener(const AbstractDelegate &delegate) const
Definition: mitkMessage.h:378
virtual A Execute() const
Definition: mitkMessage.h:157
MessageAbstractDelegate3< T, U, V, A > * Clone() const
Definition: mitkMessage.h:297
void operator()(T t, U u, V v, W w)
Definition: mitkMessage.h:707
virtual MessageAbstractDelegate1 * Clone() const =0
MessageDelegate4(R *object, A(R::*memberFunctionPointer)(T, U, V, W))
Definition: mitkMessage.h:313
bool operator==(const MessageAbstractDelegate2< T, U, A > *c) const
Definition: mitkMessage.h:247
Super::ListenerList ListenerList
Definition: mitkMessage.h:604
MessageDelegate2(R *object, A(R::*memberFunctionPointer)(T, U))
Definition: mitkMessage.h:235
bool operator==(const MessageAbstractDelegate3< T, U, V, A > *c) const
Definition: mitkMessage.h:284
MessageBase< MessageAbstractDelegate< A > > Super
Definition: mitkMessage.h:575
virtual MessageAbstractDelegate3 * Clone() const =0
void Send(T t)
Definition: mitkMessage.h:606
void operator+=(const AbstractDelegate &delegate) const
Definition: mitkMessage.h:396
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool operator==(const MessageAbstractDelegate3 *cmd) const =0
MessageBase< MessageAbstractDelegate1< T, A > > Super
Definition: mitkMessage.h:603
virtual A Execute(T t, U u, V v) const
Definition: mitkMessage.h:279
bool IsEmpty() const
Definition: mitkMessage.h:416
Super::ListenerList ListenerList
Definition: mitkMessage.h:660
void operator()(T t)
Definition: mitkMessage.h:623
itk::SimpleFastMutexLock m_Mutex
Definition: mitkMessage.h:436
void operator()()
Definition: mitkMessage.h:595
MessageAbstractDelegate4< T, U, V, W, A > * Clone() const
Definition: mitkMessage.h:338
void Send(T t, U u)
Definition: mitkMessage.h:634
virtual A Execute(T t) const
Definition: mitkMessage.h:205
void Send(T t, U u, V v, W w)
Definition: mitkMessage.h:690
bool HasListeners() const
Definition: mitkMessage.h:415
virtual A Execute(T t) const =0
void operator-=(const AbstractDelegate &delegate) const
Definition: mitkMessage.h:413
virtual A Execute() const =0
const ListenerList & GetListeners() const
Definition: mitkMessage.h:414
MessageBase< MessageAbstractDelegate2< T, U, A > > Super
Definition: mitkMessage.h:631
virtual ~MessageDelegate()
Definition: mitkMessage.h:155
void operator()(T t, U u)
Definition: mitkMessage.h:651
MessageBase & operator=(const MessageBase &o)
Definition: mitkMessage.h:371
MessageDelegate1(R *object, A(R::*memberFunctionPointer)(T))
Definition: mitkMessage.h:198
virtual ~MessageDelegate4()
Definition: mitkMessage.h:318
MessageAbstractDelegate2< T, U, A > * Clone() const
Definition: mitkMessage.h:260
virtual bool operator==(const MessageAbstractDelegate1 *cmd) const =0
virtual bool operator==(const MessageAbstractDelegate2 *cmd) const =0
virtual ~MessageDelegate1()
Definition: mitkMessage.h:203
virtual A Execute(T t, U u) const
Definition: mitkMessage.h:242
Event/message/notification class.
Definition: mitkMessage.h:572
MessageAbstractDelegate< A > * Clone() const
Definition: mitkMessage.h:175
MessageDelegate3(R *object, A(R::*memberFunctionPointer)(T, U, V))
Definition: mitkMessage.h:272
void operator()(T t, U u, V v)
Definition: mitkMessage.h:679
Super::ListenerList ListenerList
Definition: mitkMessage.h:576
virtual ~MessageDelegate2()
Definition: mitkMessage.h:240
bool operator==(const MessageAbstractDelegate< A > *c) const
Definition: mitkMessage.h:162
std::vector< AbstractDelegate * > ListenerList
Definition: mitkMessage.h:352
virtual MessageAbstractDelegate4 * Clone() const =0
virtual MessageAbstractDelegate2 * Clone() const =0
virtual A Execute(T t, U u, V v, W w) const =0
MessageBase< MessageAbstractDelegate3< T, U, V, A > > Super
Definition: mitkMessage.h:659
bool operator==(const MessageAbstractDelegate4< T, U, V, W, A > *c) const
Definition: mitkMessage.h:325
static void swap(T &x, T &y)
Definition: svm.cpp:72
virtual ~MessageBase()
Definition: mitkMessage.h:354
bool operator==(const MessageAbstractDelegate1< T, A > *c) const
Definition: mitkMessage.h:210
MessageBase< MessageAbstractDelegate4< T, U, V, W, A > > Super
Definition: mitkMessage.h:687
void Send(T t, U u, V v)
Definition: mitkMessage.h:662
virtual A Execute(T t, U u) const =0
Super::ListenerList ListenerList
Definition: mitkMessage.h:632
virtual MessageAbstractDelegate * Clone() const =0
ListenerList m_Listeners
List of listeners.
Definition: mitkMessage.h:435
virtual bool operator==(const MessageAbstractDelegate *cmd) const =0
MessageBase(const MessageBase &o)
Definition: mitkMessage.h:363
virtual A Execute(T t, U u, V v) const =0
virtual ~MessageDelegate3()
Definition: mitkMessage.h:277
virtual bool operator==(const MessageAbstractDelegate4 *cmd) const =0
virtual A Execute(T t, U u, V v, W w) const
Definition: mitkMessage.h:320
MessageAbstractDelegate1< T, A > * Clone() const
Definition: mitkMessage.h:223