OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / libs / aggregation / aggregate.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef QAGGREGATION_H
35 #define QAGGREGATION_H
36
37 #include "aggregation_global.h"
38
39 #include <QtCore/QObject>
40 #include <QtCore/QList>
41 #include <QtCore/QHash>
42 #include <QtCore/QReadWriteLock>
43 #include <QtCore/QReadLocker>
44
45 namespace Aggregation {
46
47 class AGGREGATION_EXPORT Aggregate : public QObject
48 {
49     Q_OBJECT
50
51 public:
52     Aggregate(QObject *parent = 0);
53     virtual ~Aggregate();
54
55     void add(QObject *component);
56     void remove(QObject *component);
57
58     template <typename T> T *component() {
59         QReadLocker(&lock());
60         foreach (QObject *component, m_components) {
61             if (T *result = qobject_cast<T *>(component))
62                 return result;
63         }
64         return (T *)0;
65     }
66
67     template <typename T> QList<T *> components() {
68         QReadLocker(&lock());
69         QList<T *> results;
70         foreach (QObject *component, m_components) {
71             if (T *result = qobject_cast<T *>(component)) {
72                 results << result;
73             }
74         }
75         return results;
76     }
77
78     static Aggregate *parentAggregate(QObject *obj);
79     static QReadWriteLock &lock();
80
81 signals:
82     void changed();
83
84 private slots:
85     void deleteSelf(QObject *obj);
86
87 private:
88     static QHash<QObject *, Aggregate *> &aggregateMap();
89
90     QList<QObject *> m_components;
91 };
92
93 // get a component via global template function
94 template <typename T> T *query(Aggregate *obj)
95 {
96     if (!obj)
97         return (T *)0;
98     return obj->template component<T>();
99 }
100
101 template <typename T> T *query(QObject *obj)
102 {
103     if (!obj)
104         return (T *)0;
105     T *result = qobject_cast<T *>(obj);
106     if (!result) {
107         QReadLocker(&lock());
108         Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
109         result = (parentAggregation ? query<T>(parentAggregation) : 0);
110     }
111     return result;
112 }
113
114 // get all components of a specific type via template function
115 template <typename T> QList<T *> query_all(Aggregate *obj)
116 {
117     if (!obj)
118         return QList<T *>();
119     return obj->template components<T>();
120 }
121
122 template <typename T> QList<T *> query_all(QObject *obj)
123 {
124     if (!obj)
125         return QList<T *>();
126     QReadLocker(&lock());
127     Aggregate *parentAggregation = Aggregate::parentAggregate(obj);
128     QList<T *> results;
129     if (parentAggregation)
130         results = query_all<T>(parentAggregation);
131     else if (T *result = qobject_cast<T *>(obj))
132         results.append(result);
133     return results;
134 }
135
136 } // namespace Aggregation
137
138 #endif // QAGGREGATION_H