Debian Codegraph @

src/serviceframework/ipc/qslotinvoker.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 "qslotinvoker_p.h"
40 #include "qsignalintercepter_p.h"
41 #include "qservicedebuglog_p.h"
42 #include <qmetaobject.h>
43 #include <qmetatype.h>
44 #include <qvarlengtharray.h>
45
46 #include <stdlib.h>
47
48 /*!
49 \class QSlotInvoker
50 \inpublicgroup QtBaseModule
51 \internal
52
53 \brief The QSlotInvoker class provides an interface for invoking slots with explicit arguments
54
55 IPC mechanisms need to intercept protocol messages and convert them into
56 slot invocations, but it is generally impractical to create explicit code
57 for every slot that needs to be dispatched. The QSlotInvoker class allows
58 an IPC dispatching mechanism to invoke slots in a generic fashion using
59 the invoke() method.
60
61 Methods that are marked with Q_INVOKABLE or Q_SCRIPTABLE can also
62 be invoked with this class.
63
64 \sa QSignalIntercepter
65
66 \ingroup objectmodel
67 */
68
69 QT_BEGIN_NAMESPACE
70
71 class QSlotInvokerPrivate
72 {
73 public:
74 QObject *receiver;
75 QByteArray member;
76 int memberIndex;
77 bool destroyed;
78 int returnType;
79 int *types;
80 int numArgs;
81
82 ~QSlotInvokerPrivate()
83 {
84 if ( types )
85 free(types);
86 }
87 };
88
89 /*!
90 Create a slot invoker that can invoke \a member on \a receiver.
91 The object will be attached to \a parent, if present.
92 */
93 QSlotInvoker::QSlotInvoker( QObject *receiver, const QByteArray &member,
94 QObject *parent )
95 : QObject( parent )
96 {
97 d = new QSlotInvokerPrivate();
98 d->receiver = receiver;
99 QByteArray name;
100 if ( member.size() > 0 && member[0] >= '0' && member[0] <= '9' ) {
101 // Strip off the member type code.
102 name = member.mid(1);
103 } else {
104 name = member;
105 }
106 name = QMetaObject::normalizedSignature( name.constData() );
107 d->member = name;
108 d->destroyed = false;
109 d->returnType = 0;
110 d->types = 0;
111 d->numArgs = 0;
112 if ( receiver && name.size() > 0 ) {
113 d->memberIndex
114 = receiver->metaObject()->indexOfMethod( name.constData() );
115 } else {
116 d->memberIndex = -1;
117 }
118 if ( d->memberIndex != -1 ) {
119 QMetaMethod method = receiver->metaObject()->method
120 ( d->memberIndex );
121 {
122 connect( receiver, SIGNAL(destroyed()),
123 this, SLOT(receiverDestroyed()) );
124 d->returnType =
125 QSignalIntercepter::typeFromName( method.typeName() );
126 d->types = QSignalIntercepter::connectionTypes
127 ( name, d->numArgs );
128 if ( !( d->types ) )
129 d->destroyed = true;
130 }
131 } else {
132 d->destroyed = true;
133 }
134 }
135
136 /*!
137 Destroy a slot invoker.
138 */
139 QSlotInvoker::~QSlotInvoker()
140 {
141 delete d;
142 }
143
144 /*!
145 Returns true if the member is present on the object.
146 */
147 bool QSlotInvoker::memberPresent() const
148 {
149 return ! d->destroyed;
150 }
151
152 /*!
153 Returns true if the member can be invoked with \a numArgs arguments.
154 That is, the receiver has not been destroyed, the member is present,
155 and it requires \a numArgs or less arguments.
156 */
157 bool QSlotInvoker::canInvoke( int numArgs ) const
158 {
159 if ( d->destroyed )
160 return false;
161 return ( numArgs >= d->numArgs );
162 }
163
164 /*!
165 Returns the object that will receive slot invocations.
166 */
167 QObject *QSlotInvoker::receiver() const
168 {
169 return d->receiver;
170 }
171
172 /*!
173 Returns the member that will receiver slot invocations.
174 */
175 QByteArray QSlotInvoker::member() const
176 {
177 return d->member;
178 }
179
180 /*!
181 Returns the parameter types associated with this member.
182 */
183 int *QSlotInvoker::parameterTypes() const
184 {
185 return d->types;
186 }
187
188 /*!
189 Returns the number of parameter types associated with this member.
190 */
191 int QSlotInvoker::parameterTypesCount() const
192 {
193 return d->numArgs;
194 }
195
196 /*!
197 Invokes the slot represented by this object with the argument
198 list \a args. The slot's return value is returned from
199 this method. If the slot's return type is "void", then a
200 QVariant instance of type QVariant::Invalid will be returned.
201
202 If it is possible that the slot may throw an exception,
203 it is the responsibility of the caller to catch and
204 handle the exception.
205 */
206 QVariant QSlotInvoker::invoke( const QList<QVariant>& args )
207 {
208 int arg;
209 QVariant returnValue;
210
211 // Create a default instance of the return type for the result buffer.
212 if ( d->returnType != (int)QVariant::Invalid ) {
213 returnValue = QVariant( d->returnType, (const void *)0 );
214 }
215
216 // Bail out if the receiver object has already disappeared.
217 if ( d->destroyed )
218 return returnValue;
219
220 // Check that the number of arguments is compatible with the slot.
221 int numArgs = args.size();
222 if ( numArgs < d->numArgs ) {
223 qWarning( "QSlotInvoker::invoke: insufficient arguments for slot" );
224 return returnValue;
225 } else if ( numArgs > d->numArgs ) {
226 // Drop extraneous arguments.
227 numArgs = d->numArgs;
228 }
229
230 // Construct the raw argument list.
231 QVarLengthArray<void *, 32> a( numArgs + 1 );
232 if ( d->returnType == (int)QVariant::Invalid )
233 a[0] = 0;
234 else
235 a[0] = returnValue.data();
236 for ( arg = 0; arg < numArgs; ++arg ) {
237 if ( d->types[arg] == QSignalIntercepter::QVariantId ) {
238 a[arg + 1] = (void *)&( args[arg] );
239 } else if ( args[arg].userType() != d->types[arg] ) {
240 qWarning( "QSlotInvoker::invoke: argument %d has incorrect type",
241 arg );
242 return QVariant();
243 } else {
244 a[arg + 1] = (void *)( args[arg].data() );
245 }
246 }
247
248 qServiceLog() << "event" << "slot invoked"
249 << "class" << "QSlotInvoker"
250 << "receiver" << d->receiver->objectName()
251 << "member" << QString::fromLatin1(d->member)
252 << "args" << numArgs;
253
254 // Invoke the specified slot.
255 d->receiver->qt_metacall( QMetaObject::InvokeMetaMethod,
256 d->memberIndex, a.data() );
257 return returnValue;
258 }
259
260 void QSlotInvoker::receiverDestroyed()
261 {
262 d->destroyed = true;
263 }
264
265 #include "moc_qslotinvoker_p.cpp"
266
267 QT_END_NAMESPACE