Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativenotifier_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
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 ** 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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
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
47 QT_BEGIN_NAMESPACE
48
49 class QDeclarativeNotifierEndpoint;
50 class QDeclarativeNotifier
51 {
52 public:
53     inline QDeclarativeNotifier();
54     inline ~QDeclarativeNotifier();
55     inline void notify();
56
57 private:
58     friend class QDeclarativeNotifierEndpoint;
59
60     static void emitNotify(QDeclarativeNotifierEndpoint *);
61     QDeclarativeNotifierEndpoint *endpoints;
62 };
63
64 class QDeclarativeNotifierEndpoint
65 {
66 public:
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
84 private:
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
112 QDeclarativeNotifier::QDeclarativeNotifier()
113 : endpoints(0)
114 {
115 }
116
117 QDeclarativeNotifier::~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
133 void QDeclarativeNotifier::notify()
134 {
135     if (endpoints) emitNotify(endpoints);
136 }
137
138 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint()
139 : target(0), targetMethod(0), type(InvalidType) 
140 {
141 }
142
143 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint(QObject *t, int m)
144 : target(t), targetMethod(m), type(InvalidType) 
145 {
146 }
147
148 QDeclarativeNotifierEndpoint::~QDeclarativeNotifierEndpoint()
149 {
150     disconnect();
151     if (SignalType == type) {
152         Signal *s = asSignal();
153         s->~Signal();
154     }
155 }
156
157 bool 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
168 bool QDeclarativeNotifierEndpoint::isConnected(QObject *source, int sourceSignal)
169 {
170     return SignalType == type && asSignal()->source == source && asSignal()->sourceSignal == sourceSignal;
171 }
172
173 bool QDeclarativeNotifierEndpoint::isConnected(QDeclarativeNotifier *notifier)
174 {
175     return NotifierType == type && asNotifier()->notifier == notifier;
176 }
177
178 void 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
194 void 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
215 QDeclarativeNotifierEndpoint::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
235 QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::asNotifier() 
236
237     return (Notifier *)(&notifierData); 
238 }
239
240 QDeclarativeNotifierEndpoint::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
253 QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::asSignal() 
254
255     return (Signal *)(&signalData); 
256 }
257
258 QT_END_NAMESPACE
259
260 #endif // QDECLARATIVENOTIFIER_P_H
261