1/****************************************************************************
2**
3** Copyright (C) 2017 Ford Motor Company
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtRemoteObjects 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
40#include "qremoteobjectdynamicreplica.h"
41#include "qremoteobjectreplica_p.h"
42
43#include <QtCore/qmetaobject.h>
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \class QRemoteObjectDynamicReplica
49 \inmodule QtRemoteObjects
50 \brief A dynamically instantiated \l {Replica}.
51
52 There are generated replicas (replicas having the header files produced by the \l {repc} {Replica Compiler}), and dynamic replicas, which are generated on-the-fly. This is the class for the dynamic type of replica.
53
54 When the connection to the \l {Source} object is made, the initialization step passes the current property values (see \l {Replica Initialization}). In a DynamicReplica, the property/signal/slot details are also sent, allowing the replica object to be created on-the-fly. This can be conventient in QML or scripting, but has two primary disadvantages. First, the object is in effect "empty" until it is successfully initialized by the \l {Source}. Second, in C++, calls must be made using QMetaObject::invokeMethod(), as the moc generated lookup will not be available.
55
56 This class does not have a public constructor. It can only be instantiated by using the dynamic QRemoteObjectNode::acquire method.
57*/
58
59QRemoteObjectDynamicReplica::QRemoteObjectDynamicReplica()
60 : QRemoteObjectReplica()
61{
62}
63
64QRemoteObjectDynamicReplica::QRemoteObjectDynamicReplica(QRemoteObjectNode *node, const QString &name)
65 : QRemoteObjectReplica(ConstructWithNode)
66{
67 initializeNode(node, name);
68}
69
70/*!
71 Destroys the dynamic replica.
72
73 \sa {Replica Ownership}
74*/
75QRemoteObjectDynamicReplica::~QRemoteObjectDynamicReplica()
76{
77}
78
79/*!
80 \internal
81 Returns a pointer to the dynamically generated meta-object of this object, or
82 QRemoteObjectDynamicReplica's metaObject if the object is not initialized. This
83 function overrides the QObject::metaObject() virtual function to provide the same
84 functionality for dynamic replicas.
85
86 \sa QObject::metaObject(), {Replica Initialization}
87*/
88const QMetaObject* QRemoteObjectDynamicReplica::metaObject() const
89{
90 auto impl = qSharedPointerCast<QRemoteObjectReplicaImplementation>(src: d_impl);
91 // Returning nullptr will likely result in a crash if this type is used before the
92 // definition is received. Note: QRemoteObjectDynamicReplica doesn't include the
93 // QObject macro, so it's metaobject would resolve to QRemoteObjectReplica::metaObject()
94 // if we weren't overriding it.
95 if (!impl->m_metaObject) {
96 qWarning() << "Dynamic metaobject is not assigned, returning generic Replica metaObject.";
97 qWarning() << "This may cause issues if used for more than checking the Replica state.";
98 return QRemoteObjectReplica::metaObject();
99 }
100
101 return impl->m_metaObject;
102}
103
104/*!
105 \internal
106 This function overrides the QObject::qt_metacast() virtual function to provide the same functionality for dynamic replicas.
107
108 \sa QObject::qt_metacast()
109*/
110void *QRemoteObjectDynamicReplica::qt_metacast(const char *name)
111{
112 if (!name)
113 return 0;
114
115 if (!strcmp(s1: name, s2: "QRemoteObjectDynamicReplica"))
116 return static_cast<void*>(const_cast<QRemoteObjectDynamicReplica*>(this));
117
118 // not entirely sure that one is needed... TODO: check
119 auto impl = qSharedPointerCast<QRemoteObjectReplicaImplementation>(src: d_impl);
120 if (QString::fromLatin1(str: name) == impl->m_objectName)
121 return static_cast<void*>(const_cast<QRemoteObjectDynamicReplica*>(this));
122
123 return QObject::qt_metacast(name);
124}
125
126/*!
127 \internal
128 This function overrides the QObject::qt_metacall() virtual function to provide the same functionality for dynamic replicas.
129
130 \sa QObject::qt_metacall()
131*/
132int QRemoteObjectDynamicReplica::qt_metacall(QMetaObject::Call call, int id, void **argv)
133{
134 static const bool debugArgs = qEnvironmentVariableIsSet(varName: "QT_REMOTEOBJECT_DEBUG_ARGUMENTS");
135
136 auto impl = qSharedPointerCast<QConnectedReplicaImplementation>(src: d_impl);
137
138 int saved_id = id;
139 id = QRemoteObjectReplica::qt_metacall(call, id, argv);
140 if (id < 0 || impl->m_metaObject == nullptr)
141 return id;
142
143 if (call == QMetaObject::ReadProperty || call == QMetaObject::WriteProperty) {
144 QMetaProperty mp = metaObject()->property(index: saved_id);
145
146 if (call == QMetaObject::WriteProperty) {
147 QVariantList args;
148 if (mp.userType() == QMetaType::QVariant)
149 args << *reinterpret_cast<QVariant*>(argv[0]);
150 else
151 args << QVariant(mp.userType(), argv[0]);
152 QRemoteObjectReplica::send(call: QMetaObject::WriteProperty, index: saved_id, args);
153 } else {
154 if (mp.userType() == QMetaType::QVariant)
155 *reinterpret_cast<QVariant*>(argv[0]) = impl->m_propertyStorage[id];
156 else {
157 const QVariant value = propAsVariant(i: id);
158 QMetaType::destruct(type: mp.userType(), where: argv[0]);
159 QMetaType::construct(type: mp.userType(), where: argv[0], copy: value.data());
160 }
161 }
162
163 id = -1;
164 } else if (call == QMetaObject::InvokeMetaMethod) {
165 if (id < impl->m_numSignals) {
166 qCDebug(QT_REMOTEOBJECT) << "DynamicReplica Activate" << impl->m_metaObject->method(index: saved_id).methodSignature();
167 // signal relay from Source world to Replica
168 QMetaObject::activate(sender: this, impl->m_metaObject, local_signal_index: id, argv);
169
170 } else {
171 // method relay from Replica to Source
172 const QMetaMethod mm = impl->m_metaObject->method(index: saved_id);
173 const QList<QByteArray> types = mm.parameterTypes();
174
175 const int typeSize = types.size();
176 QVariantList args;
177 args.reserve(alloc: typeSize);
178 for (int i = 0; i < typeSize; ++i) {
179 const int type = QMetaType::type(typeName: types[i].constData());
180 if (impl->m_metaObject->indexOfEnumerator(name: types[i].constData()) != -1) {
181 const auto size = QMetaType(type).sizeOf();
182 switch (size) {
183 case 1: args.push_back(t: QVariant(QMetaType::Char, argv[i + 1])); break;
184 case 2: args.push_back(t: QVariant(QMetaType::Short, argv[i + 1])); break;
185 case 4: args.push_back(t: QVariant(QMetaType::Int, argv[i + 1])); break;
186 // Qt currently only supports enum values of 4 or less bytes (QMetaEnum value(index) returns int)
187// case 8: args.push_back(QVariant(QMetaType::Int, argv[i + 1])); break;
188 default:
189 qWarning() << "Invalid enum detected (Dynamic Replica)" << QMetaType::typeName(type) << "with size" << size;
190 args.push_back(t: QVariant(QMetaType::Int, argv[i + 1])); break;
191 }
192 } else
193 args.push_back(t: QVariant(type, argv[i + 1]));
194 }
195
196 if (debugArgs) {
197 qCDebug(QT_REMOTEOBJECT) << "method" << mm.methodSignature() << "invoked - args:" << args;
198 } else {
199 qCDebug(QT_REMOTEOBJECT) << "method" << mm.methodSignature() << "invoked";
200 }
201
202 if (mm.returnType() == QMetaType::Void)
203 QRemoteObjectReplica::send(call: QMetaObject::InvokeMetaMethod, index: saved_id, args);
204 else {
205 QRemoteObjectPendingCall call = QRemoteObjectReplica::sendWithReply(call: QMetaObject::InvokeMetaMethod, index: saved_id, args);
206 if (argv[0])
207 *(static_cast<QRemoteObjectPendingCall*>(argv[0])) = call;
208 }
209 }
210
211 id = -1;
212 }
213
214 return id;
215}
216
217QT_END_NAMESPACE
218

source code of qtremoteobjects/src/remoteobjects/qremoteobjectdynamicreplica.cpp