source: trunk/src/declarative/qml/qdeclarativenotifier_p.h@ 1056

Last change on this file since 1056 was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtDeclarative module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QDECLARATIVENOTIFIER_P_H
43#define QDECLARATIVENOTIFIER_P_H
44
45#include "private/qdeclarativeguard_p.h"
46
47QT_BEGIN_NAMESPACE
48
49class QDeclarativeNotifierEndpoint;
50class QDeclarativeNotifier
51{
52public:
53 inline QDeclarativeNotifier();
54 inline ~QDeclarativeNotifier();
55 inline void notify();
56
57private:
58 friend class QDeclarativeNotifierEndpoint;
59
60 static void emitNotify(QDeclarativeNotifierEndpoint *);
61 QDeclarativeNotifierEndpoint *endpoints;
62};
63
64class QDeclarativeNotifierEndpoint
65{
66public:
67 inline QDeclarativeNotifierEndpoint();
68 inline QDeclarativeNotifierEndpoint(QObject *t, int m);
69 inline ~QDeclarativeNotifierEndpoint();
70
71 QObject *target;
72 int targetMethod;
73
74 inline bool isConnected();
75 inline bool isConnected(QObject *source, int sourceSignal);
76 inline bool isConnected(QDeclarativeNotifier *);
77
78 void connect(QObject *source, int sourceSignal);
79 inline void connect(QDeclarativeNotifier *);
80 inline void disconnect();
81
82 void copyAndClear(QDeclarativeNotifierEndpoint &other);
83
84private:
85 friend class QDeclarativeNotifier;
86
87 struct Signal {
88 QDeclarativeGuard<QObject> source;
89 int sourceSignal;
90 };
91
92 struct Notifier {
93 QDeclarativeNotifier *notifier;
94 QDeclarativeNotifierEndpoint **disconnected;
95
96 QDeclarativeNotifierEndpoint *next;
97 QDeclarativeNotifierEndpoint **prev;
98 };
99
100 enum { InvalidType, SignalType, NotifierType } type;
101 union {
102 char signalData[sizeof(Signal)];
103 char notifierData[sizeof(Notifier)];
104 };
105
106 inline Notifier *toNotifier();
107 inline Notifier *asNotifier();
108 inline Signal *toSignal();
109 inline Signal *asSignal();
110};
111
112QDeclarativeNotifier::QDeclarativeNotifier()
113: endpoints(0)
114{
115}
116
117QDeclarativeNotifier::~QDeclarativeNotifier()
118{
119 QDeclarativeNotifierEndpoint *endpoint = endpoints;
120 while (endpoint) {
121 QDeclarativeNotifierEndpoint::Notifier *n = endpoint->asNotifier();
122 endpoint = n->next;
123
124 n->next = 0;
125 n->prev = 0;
126 n->notifier = 0;
127 if (n->disconnected) *n->disconnected = 0;
128 n->disconnected = 0;
129 }
130 endpoints = 0;
131}
132
133void QDeclarativeNotifier::notify()
134{
135 if (endpoints) emitNotify(endpoints);
136}
137
138QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint()
139: target(0), targetMethod(0), type(InvalidType)
140{
141}
142
143QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint(QObject *t, int m)
144: target(t), targetMethod(m), type(InvalidType)
145{
146}
147
148QDeclarativeNotifierEndpoint::~QDeclarativeNotifierEndpoint()
149{
150 disconnect();
151 if (SignalType == type) {
152 Signal *s = asSignal();
153 s->~Signal();
154 }
155}
156
157bool QDeclarativeNotifierEndpoint::isConnected()
158{
159 if (SignalType == type) {
160 return asSignal()->source;
161 } else if (NotifierType == type) {
162 return asNotifier()->notifier;
163 } else {
164 return false;
165 }
166}
167
168bool QDeclarativeNotifierEndpoint::isConnected(QObject *source, int sourceSignal)
169{
170 return SignalType == type && asSignal()->source == source && asSignal()->sourceSignal == sourceSignal;
171}
172
173bool QDeclarativeNotifierEndpoint::isConnected(QDeclarativeNotifier *notifier)
174{
175 return NotifierType == type && asNotifier()->notifier == notifier;
176}
177
178void QDeclarativeNotifierEndpoint::connect(QDeclarativeNotifier *notifier)
179{
180 Notifier *n = toNotifier();
181
182 if (n->notifier == notifier)
183 return;
184
185 disconnect();
186
187 n->next = notifier->endpoints;
188 if (n->next) { n->next->asNotifier()->prev = &n->next; }
189 notifier->endpoints = this;
190 n->prev = &notifier->endpoints;
191 n->notifier = notifier;
192}
193
194void QDeclarativeNotifierEndpoint::disconnect()
195{
196 if (type == SignalType) {
197 Signal *s = (Signal *)&signalData;
198 if (s->source) {
199 QMetaObject::disconnectOne(s->source, s->sourceSignal, target, targetMethod);
200 s->source = 0;
201 }
202 } else if (type == NotifierType) {
203 Notifier *n = asNotifier();
204
205 if (n->next) n->next->asNotifier()->prev = n->prev;
206 if (n->prev) *n->prev = n->next;
207 if (n->disconnected) *n->disconnected = 0;
208 n->next = 0;
209 n->prev = 0;
210 n->disconnected = 0;
211 n->notifier = 0;
212 }
213}
214
215QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::toNotifier()
216{
217 if (NotifierType == type)
218 return asNotifier();
219
220 if (SignalType == type) {
221 disconnect();
222 Signal *s = asSignal();
223 s->~Signal();
224 }
225
226 Notifier *n = asNotifier();
227 n->next = 0;
228 n->prev = 0;
229 n->disconnected = 0;
230 n->notifier = 0;
231 type = NotifierType;
232 return n;
233}
234
235QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::asNotifier()
236{
237 return (Notifier *)(&notifierData);
238}
239
240QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::toSignal()
241{
242 if (SignalType == type)
243 return asSignal();
244
245 disconnect();
246 Signal *s = asSignal();
247 new (s) Signal;
248 type = SignalType;
249
250 return s;
251}
252
253QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::asSignal()
254{
255 return (Signal *)(&signalData);
256}
257
258QT_END_NAMESPACE
259
260#endif // QDECLARATIVENOTIFIER_P_H
261
Note: See TracBrowser for help on using the repository browser.