1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #ifndef QDECLARATIVENOTIFIER_P_H
43 #define QDECLARATIVENOTIFIER_P_H
45 #include "private/qdeclarativeguard_p.h"
49 class QDeclarativeNotifierEndpoint;
50 class QDeclarativeNotifier
53 inline QDeclarativeNotifier();
54 inline ~QDeclarativeNotifier();
58 friend class QDeclarativeNotifierEndpoint;
60 static void emitNotify(QDeclarativeNotifierEndpoint *);
61 QDeclarativeNotifierEndpoint *endpoints;
64 class QDeclarativeNotifierEndpoint
67 inline QDeclarativeNotifierEndpoint();
68 inline QDeclarativeNotifierEndpoint(QObject *t, int m);
69 inline ~QDeclarativeNotifierEndpoint();
74 inline bool isConnected();
75 inline bool isConnected(QObject *source, int sourceSignal);
76 inline bool isConnected(QDeclarativeNotifier *);
78 void connect(QObject *source, int sourceSignal);
79 inline void connect(QDeclarativeNotifier *);
80 inline void disconnect();
82 void copyAndClear(QDeclarativeNotifierEndpoint &other);
85 friend class QDeclarativeNotifier;
88 QDeclarativeGuard<QObject> source;
93 QDeclarativeNotifier *notifier;
94 QDeclarativeNotifierEndpoint **disconnected;
96 QDeclarativeNotifierEndpoint *next;
97 QDeclarativeNotifierEndpoint **prev;
100 enum { InvalidType, SignalType, NotifierType } type;
102 char signalData[sizeof(Signal)];
103 char notifierData[sizeof(Notifier)];
106 inline Notifier *toNotifier();
107 inline Notifier *asNotifier();
108 inline Signal *toSignal();
109 inline Signal *asSignal();
112 QDeclarativeNotifier::QDeclarativeNotifier()
117 QDeclarativeNotifier::~QDeclarativeNotifier()
119 QDeclarativeNotifierEndpoint *endpoint = endpoints;
121 QDeclarativeNotifierEndpoint::Notifier *n = endpoint->asNotifier();
127 if (n->disconnected) *n->disconnected = 0;
133 void QDeclarativeNotifier::notify()
135 if (endpoints) emitNotify(endpoints);
138 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint()
139 : target(0), targetMethod(0), type(InvalidType)
143 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint(QObject *t, int m)
144 : target(t), targetMethod(m), type(InvalidType)
148 QDeclarativeNotifierEndpoint::~QDeclarativeNotifierEndpoint()
151 if (SignalType == type) {
152 Signal *s = asSignal();
157 bool QDeclarativeNotifierEndpoint::isConnected()
159 if (SignalType == type) {
160 return asSignal()->source;
161 } else if (NotifierType == type) {
162 return asNotifier()->notifier;
168 bool QDeclarativeNotifierEndpoint::isConnected(QObject *source, int sourceSignal)
170 return SignalType == type && asSignal()->source == source && asSignal()->sourceSignal == sourceSignal;
173 bool QDeclarativeNotifierEndpoint::isConnected(QDeclarativeNotifier *notifier)
175 return NotifierType == type && asNotifier()->notifier == notifier;
178 void QDeclarativeNotifierEndpoint::connect(QDeclarativeNotifier *notifier)
180 Notifier *n = toNotifier();
182 if (n->notifier == notifier)
187 n->next = notifier->endpoints;
188 if (n->next) { n->next->asNotifier()->prev = &n->next; }
189 notifier->endpoints = this;
190 n->prev = ¬ifier->endpoints;
191 n->notifier = notifier;
194 void QDeclarativeNotifierEndpoint::disconnect()
196 if (type == SignalType) {
197 Signal *s = (Signal *)&signalData;
199 QMetaObject::disconnectOne(s->source, s->sourceSignal, target, targetMethod);
202 } else if (type == NotifierType) {
203 Notifier *n = asNotifier();
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;
215 QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::toNotifier()
217 if (NotifierType == type)
220 if (SignalType == type) {
222 Signal *s = asSignal();
226 Notifier *n = asNotifier();
235 QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::asNotifier()
237 return (Notifier *)(¬ifierData);
240 QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::toSignal()
242 if (SignalType == type)
246 Signal *s = asSignal();
253 QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::asSignal()
255 return (Signal *)(&signalData);
260 #endif // QDECLARATIVENOTIFIER_P_H