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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVENOTIFIER_P_H
43 #define QDECLARATIVENOTIFIER_P_H
45 #include "private/qdeclarativeguard_p.h"
49 class QDeclarativeNotifierEndpoint;
50 class Q_DECLARATIVE_EXPORT 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;
105 char signalData[sizeof(Signal)];
106 qint64 q_for_alignment_1;
107 double q_for_alignment_2;
113 inline Notifier *toNotifier();
114 inline Notifier *asNotifier();
115 inline Signal *toSignal();
116 inline Signal *asSignal();
119 QDeclarativeNotifier::QDeclarativeNotifier()
124 QDeclarativeNotifier::~QDeclarativeNotifier()
126 QDeclarativeNotifierEndpoint *endpoint = endpoints;
128 QDeclarativeNotifierEndpoint::Notifier *n = endpoint->asNotifier();
134 if (n->disconnected) *n->disconnected = 0;
140 void QDeclarativeNotifier::notify()
142 if (endpoints) emitNotify(endpoints);
145 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint()
146 : target(0), targetMethod(0), type(InvalidType)
150 QDeclarativeNotifierEndpoint::QDeclarativeNotifierEndpoint(QObject *t, int m)
151 : target(t), targetMethod(m), type(InvalidType)
155 QDeclarativeNotifierEndpoint::~QDeclarativeNotifierEndpoint()
158 if (SignalType == type) {
159 Signal *s = asSignal();
164 bool QDeclarativeNotifierEndpoint::isConnected()
166 if (SignalType == type) {
167 return asSignal()->source;
168 } else if (NotifierType == type) {
169 return asNotifier()->notifier;
175 bool QDeclarativeNotifierEndpoint::isConnected(QObject *source, int sourceSignal)
177 return SignalType == type && asSignal()->source == source && asSignal()->sourceSignal == sourceSignal;
180 bool QDeclarativeNotifierEndpoint::isConnected(QDeclarativeNotifier *notifier)
182 return NotifierType == type && asNotifier()->notifier == notifier;
185 void QDeclarativeNotifierEndpoint::connect(QDeclarativeNotifier *notifier)
187 Notifier *n = toNotifier();
189 if (n->notifier == notifier)
194 n->next = notifier->endpoints;
195 if (n->next) { n->next->asNotifier()->prev = &n->next; }
196 notifier->endpoints = this;
197 n->prev = ¬ifier->endpoints;
198 n->notifier = notifier;
201 void QDeclarativeNotifierEndpoint::disconnect()
203 if (type == SignalType) {
204 Signal *s = asSignal();
206 QMetaObject::disconnectOne(s->source, s->sourceSignal, target, targetMethod);
209 } else if (type == NotifierType) {
210 Notifier *n = asNotifier();
212 if (n->next) n->next->asNotifier()->prev = n->prev;
213 if (n->prev) *n->prev = n->next;
214 if (n->disconnected) *n->disconnected = 0;
222 QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::toNotifier()
224 if (NotifierType == type)
227 if (SignalType == type) {
229 Signal *s = asSignal();
233 Notifier *n = asNotifier();
242 QDeclarativeNotifierEndpoint::Notifier *QDeclarativeNotifierEndpoint::asNotifier()
244 Q_ASSERT(type == NotifierType);
248 QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::toSignal()
250 if (SignalType == type)
254 signal = new (&signalData) Signal;
259 QDeclarativeNotifierEndpoint::Signal *QDeclarativeNotifierEndpoint::asSignal()
261 Q_ASSERT(type == SignalType);
267 #endif // QDECLARATIVENOTIFIER_P_H