LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / aggregation / aggregate.h
blobd150f2089de4eec7f6abfe7acf4c9d1853857a25
1 /**
2 ******************************************************************************
4 * @file aggregate.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
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
22 * for more details.
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 {
42 Q_OBJECT
44 public:
45 Aggregate(QObject *parent = 0);
46 virtual ~Aggregate();
48 void add(QObject *component);
49 void remove(QObject *component);
51 template <typename T> T *component()
53 QReadLocker(&lock());
54 foreach(QObject * component, m_components) {
55 if (T * result = qobject_cast<T *>(component)) {
56 return result;
59 return (T *)0;
62 template <typename T> QList<T *> components()
64 QReadLocker(&lock());
65 QList<T *> results;
66 foreach(QObject * component, m_components) {
67 if (T * result = qobject_cast<T *>(component)) {
68 results << result;
71 return results;
74 static Aggregate *parentAggregate(QObject *obj);
75 static QReadWriteLock &lock();
77 private slots:
78 void deleteSelf(QObject *obj);
80 private:
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)
89 if (!obj) {
90 return (T *)0;
92 return obj->template component<T>();
95 template <typename T> T *query(QObject *obj)
97 if (!obj) {
98 return (T *)0;
100 T *result = qobject_cast<T *>(obj);
101 if (!result) {
102 QReadLocker(&lock());
103 Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
104 result = (parentAggregation ? query<T>(parentAggregation) : 0);
106 return result;
109 // get all components of a specific type via template function
110 template <typename T> QList<T *> query_all(Aggregate *obj)
112 if (!obj) {
113 return QList<T *>();
115 return obj->template components<T>();
118 template <typename T> QList<T *> query_all(QObject *obj)
120 if (!obj) {
121 return QList<T *>();
123 QReadLocker(&lock());
124 Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
125 QList<T *> results;
126 if (parentAggregation) {
127 results = query_all<T>(parentAggregation);
128 } else if (T * result = qobject_cast<T *>(obj)) {
129 results.append(result);
131 return results;
133 } // namespace Aggregation
135 #endif // QAGGREGATION_H