Medical Imaging Interaction Toolkit  2023.12.00
Medical Imaging Interaction Toolkit
berryListenerList.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 BERRYLISTENERLIST_H
14 #define BERRYLISTENERLIST_H
15 
16 #include <QMutex>
17 #include <QList>
18 #include <QReadWriteLock>
19 
20 namespace berry {
21 
23 {
24  template<class T>
25  bool operator()(const T& l1, const T& l2) const
26  {
27  return l1 == l2;
28  }
29 };
30 
58 template<class T, typename C = ListenerListEquals>
60 {
61 
62 public:
63 
70  void Add(const T& listener)
71  {
72  // This method is synchronized to protect against multiple threads adding
73  // or removing listeners concurrently.
74 
75  // check for duplicates
76  QWriteLocker l(&mutex);
77  const int oldSize = listeners.size();
78  for (int i = 0; i < oldSize; ++i)
79  {
80  T& listener2 = listeners[i];
81  if (comparator(listener, listener2))
82  return;
83  }
84 
85  listeners.push_back(listener);
86  }
87 
100  QList<T> GetListeners() const
101  {
102  QReadLocker l(&mutex);
103  return listeners;
104  }
105 
112  bool IsEmpty() const
113  {
114  QReadLocker l(&mutex);
115  return listeners.empty();
116  }
117 
124  void Remove(const T &listener)
125  {
126  QWriteLocker l(&mutex);
127  // This method is synchronized to protect against multiple threads adding
128  // or removing listeners concurrently.
129  const int oldSize = listeners.size();
130  for (int i = 0; i < oldSize; ++i)
131  {
132  T& listener2 = listeners[i];
133  if (comparator(listener, listener2))
134  {
135  listeners.removeAt(i);
136  return;
137  }
138  }
139  }
140 
146  int Size() const
147  {
148  QReadLocker l(&mutex);
149  return listeners.size();
150  }
151 
155  void Clear()
156  {
157  QWriteLocker l(&mutex);
158  listeners.clear();
159  }
160 
161 private:
162 
166  QList<T> listeners;
167 
168  mutable QReadWriteLock mutex;
169 
170  C comparator;
171 };
172 
173 }
174 
175 #endif // BERRYLISTENERLIST_H
berry::ListenerListEquals
Definition: berryListenerList.h:22
berry::ListenerList::Remove
void Remove(const T &listener)
Definition: berryListenerList.h:124
berry::ListenerList::IsEmpty
bool IsEmpty() const
Definition: berryListenerList.h:112
berry::ListenerList::Add
void Add(const T &listener)
Definition: berryListenerList.h:70
berry::ListenerList::GetListeners
QList< T > GetListeners() const
Definition: berryListenerList.h:100
berry::ListenerList::Clear
void Clear()
Definition: berryListenerList.h:155
berry::ListenerList::Size
int Size() const
Definition: berryListenerList.h:146
berry::ListenerList
Definition: berryListenerList.h:59
berry::ListenerListEquals::operator()
bool operator()(const T &l1, const T &l2) const
Definition: berryListenerList.h:25
berry
Definition: QmitkPropertyItemModel.h:24