1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "aggregate.h"
36 #include <QtCore/QWriteLocker>
39 \namespace Aggregation
40 \brief The Aggregation namespace contains support for bundling related components,
41 such that each component exposes the properties and behavior of the
42 other components to the outside.
44 Components that are bundled to an Aggregate can be "cast" to each other
45 and have a coupled life cycle. See the documentation of Aggregation::Aggregate for
50 \class Aggregation::Aggregate
54 \brief Defines a collection of related components that can be viewed as a unit.
56 An Aggregate is a collection of components that are handled as a unit,
57 such that each component exposes the properties and behavior of the
58 other components in the Aggregate to the outside.
59 Specifically that means:
61 \o They can be "cast" to each other (using query and query_all methods).
62 \o Their life cycle is coupled, i.e. whenever one is deleted all of them are.
64 Components can be of any QObject derived type.
66 You can use an Aggregate to simulate multiple inheritance by aggregation. Assume we have
68 using namespace Aggregation;
69 class MyInterface : public QObject { ........ };
70 class MyInterfaceEx : public QObject { ........ };
72 MyInterface *object = new MyInterface; // this is single inheritance
74 The query method works like a qobject_cast with normal objects:
76 Q_ASSERT(query<MyInterface>(object) == object);
77 Q_ASSERT(query<MyInterfaceEx>(object) == 0);
79 If we want 'object' to also implement the class MyInterfaceEx,
80 but don't want to or cannot use multiple inheritance, we can do it
81 at any point using an Aggregate:
83 MyInterfaceEx *objectEx = new MyInterfaceEx;
84 Aggregate *aggregate = new Aggregate;
85 aggregate->add(object);
86 aggregate->add(objectEx);
88 The Aggregate bundles the two objects together.
89 If we have any part of the collection we get all parts:
91 Q_ASSERT(query<MyInterface>(object) == object);
92 Q_ASSERT(query<MyInterfaceEx>(object) == objectEx);
93 Q_ASSERT(query<MyInterface>(objectEx) == object);
94 Q_ASSERT(query<MyInterfaceEx>(objectEx) == objectEx);
96 The following deletes all three: object, objectEx and aggregate:
100 // or delete aggregate;
103 Aggregation aware code never uses qobject_cast, but always uses
104 Aggregation::query which behaves like a qobject_cast as a fallback.
108 \fn T *Aggregate::component()
110 Template method that returns the component with the given type, if there is one.
111 If there are multiple components with that type a random one is returned.
113 \sa Aggregate::components()
118 \fn QList<T *> Aggregate::components()
120 Template method that returns all components with the given type, if there are any.
122 \sa Aggregate::component()
127 \fn T *Aggregation::query<T *>(Aggregate *obj)
132 \fn QList<T *> Aggregation::query_all<T *>(Aggregate *obj)
137 \relates Aggregation::Aggregate
138 \fn T *Aggregation::query<T *>(QObject *obj)
140 Performs a dynamic cast that is aware of a possible Aggregate that \a obj
141 might belong to. If \a obj itself is of the requested type then it is simply cast
142 and returned. Otherwise, if \a obj belongs to an Aggregate all its components are
143 checked, or if it doesn't belong to an Aggregate null is returned.
145 \sa Aggregate::component()
149 \relates Aggregation::Aggregate
150 \fn QList<T *> Aggregation::query_all<T *>(QObject *obj)
152 If \a obj belongs to an Aggregate, all components that can be cast to the given
153 type are returned. Otherwise, \a obj is returned if it is of the requested type.
155 \sa Aggregate::components()
158 using namespace Aggregation;
161 \fn Aggregate *Aggregate::parentAggregate(QObject *obj)
163 Returns the Aggregate object of \a obj if there is one. Otherwise returns 0.
165 Aggregate *Aggregate::parentAggregate(QObject *obj)
167 QReadLocker locker(&lock());
168 return aggregateMap().value(obj);
171 QHash<QObject *, Aggregate *> &Aggregate::aggregateMap()
173 static QHash<QObject *, Aggregate *> map;
178 \fn QReadWriteLock &Aggregate::lock()
181 QReadWriteLock &Aggregate::lock()
183 static QReadWriteLock lock;
188 \fn Aggregate::Aggregate(QObject *parent)
190 Creates a new Aggregate with the given \a parent.
191 The \a parent is passed directly passed to the QObject part
192 of the class and is not used beside that.
194 Aggregate::Aggregate(QObject *parent)
197 QWriteLocker locker(&lock());
198 aggregateMap().insert(this, this);
202 \fn Aggregate::~Aggregate()
204 Deleting the aggregate automatically deletes all its components.
206 Aggregate::~Aggregate()
208 QWriteLocker locker(&lock());
209 foreach (QObject *component, m_components) {
210 disconnect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));
211 aggregateMap().remove(component);
213 qDeleteAll(m_components);
214 m_components.clear();
215 aggregateMap().remove(this);
218 void Aggregate::deleteSelf(QObject *obj)
221 QWriteLocker locker(&lock());
222 aggregateMap().remove(obj);
223 m_components.removeAll(obj);
229 \fn void Aggregate::add(QObject *component)
231 Adds the \a component to the aggregate.
233 \sa Aggregate::remove()
235 void Aggregate::add(QObject *component)
240 QWriteLocker locker(&lock());
241 Aggregate *parentAggregation = aggregateMap().value(component);
242 if (parentAggregation == this)
244 if (parentAggregation)
245 parentAggregation->remove(component);
246 m_components.append(component);
247 connect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));
248 aggregateMap().insert(component, this);
254 \fn void Aggregate::remove(QObject *component)
256 Removes the \a component from the aggregate.
260 void Aggregate::remove(QObject *component)
265 QWriteLocker locker(&lock());
266 aggregateMap().remove(component);
267 m_components.removeAll(component);
268 disconnect(component, SIGNAL(destroyed(QObject*)), this, SLOT(deleteSelf(QObject*)));