2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
8 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
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.
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
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef QAGGREGATION_H
30 #define QAGGREGATION_H
32 #include "aggregation_global.h"
34 #include <QtCore/QObject>
35 #include <QtCore/QList>
36 #include <QtCore/QHash>
37 #include <QtCore/QReadWriteLock>
38 #include <QtCore/QReadLocker>
40 namespace Aggregation
{
41 class AGGREGATION_EXPORT Aggregate
: public QObject
{
45 Aggregate(QObject
*parent
= 0);
48 void add(QObject
*component
);
49 void remove(QObject
*component
);
51 template <typename T
> T
*component()
54 foreach(QObject
* component
, m_components
) {
55 if (T
* result
= qobject_cast
<T
*>(component
)) {
62 template <typename T
> QList
<T
*> components()
66 foreach(QObject
* component
, m_components
) {
67 if (T
* result
= qobject_cast
<T
*>(component
)) {
74 static Aggregate
*parentAggregate(QObject
*obj
);
75 static QReadWriteLock
&lock();
78 void deleteSelf(QObject
*obj
);
81 static QHash
<QObject
*, Aggregate
*> &aggregateMap();
83 QList
<QObject
*> m_components
;
86 // get a component via global template function
87 template <typename T
> T
*query(Aggregate
*obj
)
92 return obj
->template component
<T
>();
95 template <typename T
> T
*query(QObject
*obj
)
100 T
*result
= qobject_cast
<T
*>(obj
);
102 QReadLocker(&lock());
103 Aggregate
*parentAggregation
= Aggregate::parentAggregate(obj
);
104 result
= (parentAggregation
? query
<T
>(parentAggregation
) : 0);
109 // get all components of a specific type via template function
110 template <typename T
> QList
<T
*> query_all(Aggregate
*obj
)
115 return obj
->template components
<T
>();
118 template <typename T
> QList
<T
*> query_all(QObject
*obj
)
123 QReadLocker(&lock());
124 Aggregate
*parentAggregation
= Aggregate::parentAggregate(obj
);
126 if (parentAggregation
) {
127 results
= query_all
<T
>(parentAggregation
);
128 } else if (T
* result
= qobject_cast
<T
*>(obj
)) {
129 results
.append(result
);
133 } // namespace Aggregation
135 #endif // QAGGREGATION_H