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 #ifndef QAGGREGATION_H
35 #define QAGGREGATION_H
37 #include "aggregation_global.h"
39 #include <QtCore/QObject>
40 #include <QtCore/QList>
41 #include <QtCore/QHash>
42 #include <QtCore/QReadWriteLock>
43 #include <QtCore/QReadLocker>
45 namespace Aggregation {
47 class AGGREGATION_EXPORT Aggregate : public QObject
52 Aggregate(QObject *parent = 0);
55 void add(QObject *component);
56 void remove(QObject *component);
58 template <typename T> T *component() {
60 foreach (QObject *component, m_components) {
61 if (T *result = qobject_cast<T *>(component))
67 template <typename T> QList<T *> components() {
70 foreach (QObject *component, m_components) {
71 if (T *result = qobject_cast<T *>(component)) {
78 static Aggregate *parentAggregate(QObject *obj);
79 static QReadWriteLock &lock();
85 void deleteSelf(QObject *obj);
88 static QHash<QObject *, Aggregate *> &aggregateMap();
90 QList<QObject *> m_components;
93 // get a component via global template function
94 template <typename T> T *query(Aggregate *obj)
98 return obj->template component<T>();
101 template <typename T> T *query(QObject *obj)
105 T *result = qobject_cast<T *>(obj);
107 QReadLocker(&lock());
108 Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
109 result = (parentAggregation ? query<T>(parentAggregation) : 0);
114 // get all components of a specific type via template function
115 template <typename T> QList<T *> query_all(Aggregate *obj)
119 return obj->template components<T>();
122 template <typename T> QList<T *> query_all(QObject *obj)
126 QReadLocker(&lock());
127 Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
129 if (parentAggregation)
130 results = query_all<T>(parentAggregation);
131 else if (T *result = qobject_cast<T *>(obj))
132 results.append(result);
136 } // namespace Aggregation
138 #endif // QAGGREGATION_H