Qt 4.8
qdeclarativenotifier_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
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, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 #include <QtCore/qmetaobject.h>
47 
49 
52 {
53 public:
54  inline QDeclarativeNotifier();
55  inline ~QDeclarativeNotifier();
56  inline void notify();
57 
58 private:
60 
63 };
64 
66 {
67 public:
69  inline QDeclarativeNotifierEndpoint(QObject *t, int m);
71 
74 
75  inline bool isConnected();
76  inline bool isConnected(QObject *source, int sourceSignal);
77  inline bool isConnected(QDeclarativeNotifier *);
78 
79  void connect(QObject *source, int sourceSignal);
80  inline void connect(QDeclarativeNotifier *);
81 
82  // Disconnects unconditionally, regardless of the refcount
83  inline void disconnect();
84 
85  // Decreases the refcount and disconnects when refcount reaches 0
86  inline void deref();
87 
88  void copyAndClear(QDeclarativeNotifierEndpoint &other);
89 
90 private:
91  friend class QDeclarativeNotifier;
92 
93  struct Signal {
96  };
97 
98  struct Notifier {
101 
104  };
105 
106  enum { InvalidType, SignalType, NotifierType } type;
107  union {
108  struct {
110  union {
111  char signalData[sizeof(Signal)];
114  };
115  } signal;
117  };
118 
120 
121  inline Notifier *toNotifier();
122  inline Notifier *asNotifier();
123  inline Signal *toSignal();
124  inline Signal *asSignal();
125 };
126 
128 : endpoints(0)
129 {
130 }
131 
133 {
135  while (endpoint) {
137  endpoint = n->next;
138 
139  n->next = 0;
140  n->prev = 0;
141  n->notifier = 0;
142  if (n->disconnected) *n->disconnected = 0;
143  n->disconnected = 0;
144  }
145  endpoints = 0;
146 }
147 
149 {
151 }
152 
154  : target(0), targetMethod(0), type(InvalidType), refCount(0)
155 {
156 }
157 
160 {
161 }
162 
164 {
165  disconnect();
166  if (SignalType == type) {
167  Signal *s = asSignal();
168  s->~Signal();
169  }
170 }
171 
173 {
174  if (SignalType == type) {
175  return asSignal()->source;
176  } else if (NotifierType == type) {
177  return asNotifier()->notifier;
178  } else {
179  return false;
180  }
181 }
182 
183 bool QDeclarativeNotifierEndpoint::isConnected(QObject *source, int sourceSignal)
184 {
185  return SignalType == type && asSignal()->source == source && asSignal()->sourceSignal == sourceSignal;
186 }
187 
189 {
190  return NotifierType == type && asNotifier()->notifier == notifier;
191 }
192 
194 {
195  Notifier *n = toNotifier();
196 
197  if (n->notifier == notifier) {
198  refCount++;
199  return;
200  }
201 
202  disconnect();
203 
204  n->next = notifier->endpoints;
205  if (n->next) { n->next->asNotifier()->prev = &n->next; }
206  notifier->endpoints = this;
207  n->prev = &notifier->endpoints;
208  n->notifier = notifier;
209  refCount++;
210 }
211 
213 {
214  if (type == SignalType) {
215  Signal *s = asSignal();
216  if (s->source) {
220  QVarLengthArray<char> signalSignature;
221  QObjectPrivate::signalSignature(signal, &signalSignature);
222  priv->disconnectNotify(signalSignature.constData());
223  s->source = 0;
224  }
225  } else if (type == NotifierType) {
226  Notifier *n = asNotifier();
227 
228  if (n->next) n->next->asNotifier()->prev = n->prev;
229  if (n->prev) *n->prev = n->next;
230  if (n->disconnected) *n->disconnected = 0;
231  n->next = 0;
232  n->prev = 0;
233  n->disconnected = 0;
234  n->notifier = 0;
235  }
236  refCount = 0;
237 }
238 
240 {
241  refCount--;
242  if (refCount <= 0)
243  disconnect();
244 }
245 
247 {
248  if (NotifierType == type)
249  return asNotifier();
250 
251  if (SignalType == type) {
252  disconnect();
253  Signal *s = asSignal();
254  s->~Signal();
255  }
256 
257  type = NotifierType;
258  Notifier *n = asNotifier();
259  n->next = 0;
260  n->prev = 0;
261  n->disconnected = 0;
262  n->notifier = 0;
263  return n;
264 }
265 
267 {
269  return &notifier;
270 }
271 
273 {
274  if (SignalType == type)
275  return asSignal();
276 
277  disconnect();
278  signal.signal = new (&signal.signalData) Signal;
279  type = SignalType;
280  return signal.signal;
281 }
282 
284 {
286  return signal.signal;
287 }
288 
290 
291 #endif // QDECLARATIVENOTIFIER_P_H
292 
const T * constData() const
int type
Definition: qmetatype.cpp:239
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static void signalSignature(const QMetaMethod &signal, QVarLengthArray< char > *result)
Definition: qobject_p.h:255
friend class QDeclarativeNotifierEndpoint
QDeclarativeNotifierEndpoint ** disconnected
void connect(QObject *source, int sourceSignal)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static QObjectPrivate * get(QObject *o)
Definition: qobject_p.h:177
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
unsigned short quint16
Definition: qglobal.h:936
__int64 qint64
Definition: qglobal.h:942
static void emitNotify(QDeclarativeNotifierEndpoint *)
enum QDeclarativeNotifierEndpoint::@137 type
QDeclarativeNotifierEndpoint ** prev
static const QMetaObjectPrivate * priv(const uint *data)
static bool disconnectOne(const QObject *sender, int signal_index, const QObject *receiver, int method_index)
Definition: qobject.cpp:3290
QMetaMethod method(int index) const
Returns the meta-data for the method with the given index.
void disconnectNotify(const char *signal)
Definition: qobject_p.h:250
QDeclarativeNotifierEndpoint * endpoints
The QMetaMethod class provides meta-data about a member function.
Definition: qmetaobject.h:56
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.