1// Copyright (C) 2017 Ford Motor Company
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QREMOTEOBJECTSOURCE_P_H
5#define QREMOTEOBJECTSOURCE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qlist.h>
19#include <QtCore/qmetaobject.h>
20#include <QtCore/qobject.h>
21#include <QtCore/qpointer.h>
22#include "qremoteobjectsource.h"
23#include "qremoteobjectpacket_p.h"
24
25QT_BEGIN_NAMESPACE
26
27class QRemoteObjectSourceIo;
28class QtROIoDeviceBase;
29
30class QRemoteObjectSourceBase : public QObject
31{
32public:
33 ~QRemoteObjectSourceBase() override;
34
35 void setConnections();
36 void resetObject(QObject *newObject);
37 int qt_metacall(QMetaObject::Call call, int methodId, void **a) final;
38 QObject *m_object, *m_adapter;
39 const SourceApiMap *m_api;
40 QVariantList m_marshalledArgs;
41 bool hasAdapter() const { return m_adapter; }
42 virtual QString name() const = 0;
43 virtual bool isRoot() const = 0;
44
45 QVariantList* marshalArgs(int index, void **a);
46 void handleMetaCall(int index, QMetaObject::Call call, void **a);
47 bool invoke(QMetaObject::Call c, int index, const QVariantList& args, QVariant* returnValue = nullptr);
48 QByteArray m_objectChecksum;
49 QMap<int, QPointer<QRemoteObjectSourceBase>> m_children;
50 struct Private {
51 Private(QRemoteObjectSourceIo *io, QRemoteObjectRootSource *root);
52 QRemoteObjectSourceIo *m_sourceIo;
53 QList<QtROIoDeviceBase*> m_listeners;
54 // Pointer to codec, not owned by Private. We can assume it is valid.
55 QRemoteObjectPackets::CodecBase *codec;
56
57 // Types needed during recursively sending a root to a new listener
58 QSet<QString> sentTypes;
59 bool isDynamic;
60 QRemoteObjectRootSource *root;
61 };
62 Private *d;
63 static const int qobjectPropertyOffset;
64 static const int qobjectMethodOffset;
65protected:
66 explicit QRemoteObjectSourceBase(QObject *object, Private *d, const SourceApiMap *, QObject *adapter);
67};
68
69class QRemoteObjectSource : public QRemoteObjectSourceBase
70{
71public:
72 explicit QRemoteObjectSource(QObject *object, Private *d, const SourceApiMap *, QObject *adapter, const QString &parentName);
73 ~QRemoteObjectSource() override;
74
75 bool isRoot() const override { return false; }
76 QString name() const override { return m_name; }
77
78 QString m_name;
79};
80
81class QRemoteObjectRootSource : public QRemoteObjectSourceBase
82{
83public:
84 explicit QRemoteObjectRootSource(QObject *object, const SourceApiMap *,
85 QObject *adapter, QRemoteObjectSourceIo *sourceIo);
86 ~QRemoteObjectRootSource() override;
87
88 bool isRoot() const override { return true; }
89 QString name() const override { return m_name; }
90 void addListener(QtROIoDeviceBase *io, bool dynamic = false);
91 int removeListener(QtROIoDeviceBase *io, bool shouldSendRemove = false);
92
93 QString m_name;
94};
95
96class DynamicApiMap final : public SourceApiMap
97{
98public:
99 DynamicApiMap(QObject *object, const QMetaObject *metaObject, const QString &name, const QString &typeName);
100 ~DynamicApiMap() override {}
101 QString name() const override { return m_name; }
102 QString typeName() const override { return m_typeName; }
103 QByteArray className() const override { return QByteArray(m_metaObject->className()); }
104 int enumCount() const override { return m_enumCount; }
105 int propertyCount() const override { return m_properties.size(); }
106 int signalCount() const override { return m_signals.size(); }
107 int methodCount() const override { return m_methods.size(); }
108 int sourceEnumIndex(int index) const override
109 {
110 if (index < 0 || index >= enumCount())
111 return -1;
112 return m_enumOffset + index;
113 }
114 int sourcePropertyIndex(int index) const override
115 {
116 if (index < 0 || index >= propertyCount())
117 return -1;
118 return m_properties.at(i: index);
119 }
120 int sourceSignalIndex(int index) const override
121 {
122 if (index < 0 || index >= signalCount())
123 return -1;
124 return m_signals.at(i: index);
125 }
126 int sourceMethodIndex(int index) const override
127 {
128 if (index < 0 || index >= methodCount())
129 return -1;
130 return m_methods.at(i: index);
131 }
132 int signalParameterCount(int index) const override { return parameterCount(objectIndex: m_signals.at(i: index)); }
133 int signalParameterType(int sigIndex, int paramIndex) const override { return parameterType(objectIndex: m_signals.at(i: sigIndex), paramIndex); }
134 const QByteArray signalSignature(int index) const override { return signature(objectIndex: m_signals.at(i: index)); }
135 QByteArrayList signalParameterNames(int index) const override;
136
137 int methodParameterCount(int index) const override { return parameterCount(objectIndex: m_methods.at(i: index)); }
138 int methodParameterType(int methodIndex, int paramIndex) const override { return parameterType(objectIndex: m_methods.at(i: methodIndex), paramIndex); }
139 const QByteArray methodSignature(int index) const override { return signature(objectIndex: m_methods.at(i: index)); }
140 QMetaMethod::MethodType methodType(int index) const override;
141 const QByteArray typeName(int index) const override;
142 QByteArrayList methodParameterNames(int index) const override;
143
144 int propertyIndexFromSignal(int index) const override
145 {
146 if (index >= 0 && index < m_propertyAssociatedWithSignal.size())
147 return m_properties.at(i: m_propertyAssociatedWithSignal.at(i: index));
148 return -1;
149 }
150 int propertyRawIndexFromSignal(int index) const override
151 {
152 if (index >= 0 && index < m_propertyAssociatedWithSignal.size())
153 return m_propertyAssociatedWithSignal.at(i: index);
154 return -1;
155 }
156 QByteArray objectSignature() const override { return m_objectSignature; }
157
158 bool isDynamic() const override { return true; }
159
160 int parameterCount(int objectIndex) const;
161 int parameterType(int objectIndex, int paramIndex) const;
162 const QByteArray signature(int objectIndex) const;
163 inline void checkCache(int objectIndex) const
164 {
165 if (objectIndex != m_cachedMetamethodIndex) {
166 m_cachedMetamethodIndex = objectIndex;
167 m_cachedMetamethod = m_metaObject->method(index: objectIndex);
168 }
169 }
170
171 QString m_name;
172 QString m_typeName;
173 int m_enumCount;
174 int m_enumOffset;
175 QList<int> m_properties;
176 QList<int> m_signals;
177 QList<int> m_methods;
178 QList<int> m_propertyAssociatedWithSignal;
179 const QMetaObject *m_metaObject;
180 mutable QMetaMethod m_cachedMetamethod;
181 mutable int m_cachedMetamethodIndex;
182 QByteArray m_objectSignature;
183};
184
185QT_END_NAMESPACE
186
187#endif
188

source code of qtremoteobjects/src/remoteobjects/qremoteobjectsource_p.h