dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
aggregate.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 #include "aggregate.h"
29 
30 #include <QtCore/QWriteLocker>
31 
152 using namespace Aggregation;
153 
160 {
161  QReadLocker locker(&lock());
162  return aggregateMap().value(obj);
163 }
164 
165 QHash<QObject *, Aggregate *> &Aggregate::aggregateMap()
166 {
167  static QHash<QObject *, Aggregate *> map;
168  return map;
169 }
170 
175 QReadWriteLock &Aggregate::lock()
176 {
177  static QReadWriteLock lock;
178  return lock;
179 }
180 
188 Aggregate::Aggregate(QObject *parent)
189  : QObject(parent)
190 {
191  QWriteLocker locker(&lock());
192  aggregateMap().insert(this, this);
193 }
194 
201 {
202  QWriteLocker locker(&lock());
203  foreach (QObject *component, m_components) {
204  disconnect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));
205  aggregateMap().remove(component);
206  }
207  qDeleteAll(m_components);
208  m_components.clear();
209  aggregateMap().remove(this);
210 }
211 
212 void Aggregate::deleteSelf(QObject *obj)
213 {
214  {
215  QWriteLocker locker(&lock());
216  aggregateMap().remove(obj);
217  m_components.removeAll(obj);
218  }
219  delete this;
220 }
221 
229 void Aggregate::add(QObject *component)
230 {
231  if (!component)
232  return;
233  QWriteLocker locker(&lock());
234  Aggregate *parentAggregation = aggregateMap().value(component);
235  if (parentAggregation == this)
236  return;
237  if (parentAggregation)
238  parentAggregation->remove(component);
239  m_components.append(component);
240  connect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));
241  aggregateMap().insert(component, this);
242 }
243 
251 void Aggregate::remove(QObject *component)
252 {
253  if (!component)
254  return;
255  QWriteLocker locker(&lock());
256  aggregateMap().remove(component);
257  m_components.removeAll(component);
258  disconnect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));
259 }
Aggregate(QObject *parent=0)
Definition: aggregate.cpp:188
static Aggregate * parentAggregate(QObject *obj)
Definition: aggregate.cpp:159
Defines a collection of related components that can be viewed as a unit.
Definition: aggregate.h:41
void remove(QObject *component)
Definition: aggregate.cpp:251
void add(QObject *component)
Definition: aggregate.cpp:229
static QReadWriteLock & lock()
Definition: aggregate.cpp:175