Debian Codegraph @

src/serviceframework/ipc/qsignalintercepter.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems 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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 #include "qsignalintercepter_p.h"
40 #include <qmetaobject.h>
41 #include <qmetatype.h>
42 #include <qobjectdefs.h>
43 #include <qdebug.h>
44
45 #include <stdlib.h>
46
47 /*!
48 \class QSignalIntercepter
49 \inpublicgroup QtBaseModule
50 \internal
51
52 \brief The QSignalIntercepter class provides an interface for intercepting signals as meta-calls
53
54 IPC mechanisms need to intercept signals and convert them into protocol
55 messages, but it is generally impractical to create a slot for every
56 signal that needs to be dispatched. The QSignalIntercepter class allows
57 signals to be intercepted as meta-calls so that IPC dispatching can
58 be implemented in a generic fashion.
59
60 The activated() method is called whenever the signal is emitted,
61 with the arguments in a typed list.
62
63 \sa QSlotInvoker
64
65 \ingroup objectmodel
66 */
67
68 QT_BEGIN_NAMESPACE
69
70 class QSignalIntercepterPrivate
71 {
72 public:
73 QObject *sender;
74 QByteArray signal;
75 int signalIndex;
76 int destroyIndex;
77 int slotIndex;
78 int *types;
79 int numArgs;
80
81 ~QSignalIntercepterPrivate()
82 {
83 if ( types )
84 free(types);
85 }
86 };
87
88 /*!
89 Create a new signal intercepter which traps \a signal on \a sender.
90 The object will be attached to \a parent, if present.
91 */
92 QSignalIntercepter::QSignalIntercepter
93 ( QObject *sender, const QByteArray& signal, QObject *parent )
94 : QObject( parent )
95 {
96 // Initialize the private members.
97 d = new QSignalIntercepterPrivate();
98 d->sender = sender;
99 d->signal = signal;
100 d->signalIndex = -1;
101 d->destroyIndex = -1;
102 d->slotIndex = -1;
103 d->types = 0;
104
105 // Resolve the indices of the signals we are interested in.
106 if ( sender && signal.size() > 0 ) {
107 // '2' is QSIGNAL_CODE in Qt 4.4 and below,
108 // '6' is QSIGNAL_CODE in Qt 4.5 and higher.
109 if ( signal[0] != '2' && signal[0] != '6' ) {
110 qWarning( "QSignalIntercepter: `%s' is not a valid signal "
111 "specification", signal.constData() );
112 return;
113 }
114 QByteArray name = QMetaObject::normalizedSignature
115 ( signal.constData() + 1 );
116 d->signalIndex
117 = sender->metaObject()->indexOfSignal( name.constData() );
118 if ( d->signalIndex < 0 ) {
119 qWarning( "QSignalIntercepter: no such signal: %s::%s",
120 sender->metaObject()->className(), signal.constData() );
121 return;
122 }
123 d->destroyIndex
124 = sender->metaObject()->indexOfSignal( "destroyed()" );
125 d->types = connectionTypes( name, d->numArgs );
126 }
127
128 // Derive a fake slot index to use in our manual qt_metacall implementation.
129 d->slotIndex = staticMetaObject.methodCount();
130
131 // Connect up the signals.
132 if ( d->signalIndex >= 0 ) {
133 QMetaObject::connect( sender, d->signalIndex,
134 this, d->slotIndex,
135 Qt::DirectConnection, 0 );
136 }
137 if ( d->destroyIndex >= 0 ) {
138 QMetaObject::connect( sender, d->destroyIndex,
139 this, d->slotIndex + 1,
140 Qt::DirectConnection, 0 );
141 }
142 }
143
144 /*!
145 Destroy a signal intercepter.
146 */
147 QSignalIntercepter::~QSignalIntercepter()
148 {
149 if ( d->signalIndex >= 0 ) {
150 QMetaObject::disconnect( d->sender, d->signalIndex,
151 this, d->slotIndex );
152 }
153 if ( d->destroyIndex >= 0 ) {
154 QMetaObject::disconnect( d->sender, d->destroyIndex,
155 this, d->slotIndex + 1 );
156 }
157 delete d;
158 }
159
160 /*!
161 Returns the sender that this signal interceptor is attached to.
162 */
163 QObject *QSignalIntercepter::sender() const
164 {
165 return d->sender;
166 }
167
168 /*!
169 Returns the name of the signal that this signal interceptor is attached to.
170 */
171 QByteArray QSignalIntercepter::signal() const
172 {
173 return d->signal;
174 }
175
176 /*!
177 Returns true if this signal intercepter is valid; that is, there was
178 a signal present with the specified parameters when this object
179 was constructed.
180 */
181 bool QSignalIntercepter::isValid() const
182 {
183 return ( d->signalIndex != -1 );
184 }
185
186 /*!
187 \internal
188 */
189 int QSignalIntercepter::qt_metacall(QMetaObject::Call c, int id, void **a)
190 {
191 id = QObject::qt_metacall(c, id, a);
192 if (id < 0)
193 return id;
194 if (c == QMetaObject::InvokeMetaMethod) {
195 switch (id) {
196 case 0: {
197 // The signal we are interested in has been activated.
198 if ( d->types ) {
199 QList<QVariant> args;
200 for ( int i = 0; i < d->numArgs; ++i ) {
201 if ( d->types[i] != QVariantId ) {
202 QVariant arg( d->types[i], a[i + 1] );
203 args.append( arg );
204 } else {
205 args.append( *((const QVariant *)( a[i + 1] )) );
206 }
207 }
208 activated( args );
209 }
210 }
211 break;
212
213 case 1: {
214 // The sender has been destroyed. Clear the signal indices
215 // so that we don't try to do a manual disconnect when our
216 // own destructor is called.
217 d->signalIndex = -1;
218 d->destroyIndex = -1;
219 }
220 break;
221 }
222 id -= 2;
223 }
224 return id;
225 }
226
227 /*!
228 \fn void QSignalIntercepter::activated( const QList<QVariant>& args )
229
230 Called when the signal that is being intercepted is activated.
231 The arguments to the signal are passed in the list \a args.
232 */
233
234 // Get the QVariant type number for a type name.
235 int QSignalIntercepter::typeFromName( const QByteArray& type )
236 {
237 int id;
238 if (type.endsWith('*'))
239 return QMetaType::VoidStar;
240 else if ( type.size() == 0 || type == "void" )
241 return QMetaType::Void;
242 id = QMetaType::type( type.constData() );
243 if (id == QMetaType::QVariant)
244 return QSignalIntercepter::QVariantId;
245 return id;
246 }
247
248 /*!
249 Returns the connection types associated with a signal or slot \a member
250 specification. The array of types is returned from this function,
251 and the number of arguments is returned in \a nargs. Returns null
252 if \a member is invalid. The return value must be freed with qFree().
253 */
254 int *QSignalIntercepter::connectionTypes( const QByteArray& member, int& nargs )
255 {
256 // Based on Qt's internal queuedConnectionTypes function.
257 nargs = 0;
258 int *types = 0;
259 const char *s = member.constData();
260 while (*s != '\0' && *s != '(') { ++s; }
261 if ( *s == '\0' )
262 return 0;
263 ++s;
264 const char *e = s;
265 while (*e != ')') {
266 ++e;
267 if (*e == ')' || *e == ',')
268 ++nargs;
269 }
270
271 types = (int *) malloc((nargs + 1) * sizeof(int));
272 types[nargs] = 0;
273 for (int n = 0; n < nargs; ++n) {
274 e = s;
275 while (*s != ',' && *s != ')')
276 ++s;
277 QByteArray type(e, s-e);
278 ++s;
279
280 types[n] = typeFromName(type);
281 if (!types[n]) {
282 qWarning("QSignalIntercepter::connectionTypes: Cannot marshal arguments of type '%s'", type.data());
283 free(types);
284 return 0;
285 }
286 }
287 return types;
288 }
289
290 QT_END_NAMESPACE