dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
aggregate.h
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 #ifndef QAGGREGATION_H
29 #define QAGGREGATION_H
30 
31 #include "aggregation_global.h"
32 
33 #include <QtCore/QObject>
34 #include <QtCore/QList>
35 #include <QtCore/QHash>
36 #include <QtCore/QReadWriteLock>
37 #include <QtCore/QReadLocker>
38 
39 namespace Aggregation {
40 
41 class AGGREGATION_EXPORT Aggregate : public QObject
42 {
43  Q_OBJECT
44 
45 public:
46  Aggregate(QObject *parent = 0);
47  virtual ~Aggregate();
48 
49  void add(QObject *component);
50  void remove(QObject *component);
51 
52  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;
57  }
58  return (T *)0;
59  }
60 
61  template <typename T> QList<T *> components() {
62  QReadLocker(&lock());
64  foreach (QObject *component, m_components) {
65  if (T *result = qobject_cast<T *>(component)) {
66  results << result;
67  }
68  }
69  return results;
70  }
71 
72  static Aggregate *parentAggregate(QObject *obj);
73  static QReadWriteLock &lock();
74 
75 private slots:
76  void deleteSelf(QObject *obj);
77 
78 private:
79  static QHash<QObject *, Aggregate *> &aggregateMap();
80 
81  QList<QObject *> m_components;
82 };
83 
84 // get a component via global template function
85 template <typename T> T *query(Aggregate *obj)
86 {
87  if (!obj)
88  return (T *)0;
89  return obj->template component<T>();
90 }
91 
92 template <typename T> T *query(QObject *obj)
93 {
94  if (!obj)
95  return (T *)0;
96  T *result = qobject_cast<T *>(obj);
97  if (!result) {
98  QReadLocker(&lock());
99  Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
100  result = (parentAggregation ? query<T>(parentAggregation) : 0);
101  }
102  return result;
103 }
104 
105 // get all components of a specific type via template function
106 template <typename T> QList<T *> query_all(Aggregate *obj)
107 {
108  if (!obj)
109  return QList<T *>();
110  return obj->template components<T>();
111 }
112 
113 template <typename T> QList<T *> query_all(QObject *obj)
114 {
115  if (!obj)
116  return QList<T *>();
117  QReadLocker(&lock());
118  Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
120  if (parentAggregation)
121  results = query_all<T>(parentAggregation);
122  else if (T *result = qobject_cast<T *>(obj))
123  results.append(result);
124  return results;
125 }
126 
127 } // namespace Aggregation
128 
129 #endif // QAGGREGATION_H
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
T * query(Aggregate *obj)
Definition: aggregate.h:85
end function found on Matlab s file exchange The two functions return identical results
Definition: icore.h:39
QList< T * > query_all(Aggregate *obj)
Definition: aggregate.h:106
QList< T * > components()
Definition: aggregate.h:61