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