| 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 | #ifndef QREMOTEOBJECTSOURCE_P_H |
| 41 | #define QREMOTEOBJECTSOURCE_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <QtCore/qobject.h> |
| 55 | #include <QtCore/qmetaobject.h> |
| 56 | #include <QtCore/qvector.h> |
| 57 | #include <QtCore/qpointer.h> |
| 58 | #include "qremoteobjectsource.h" |
| 59 | #include "qremoteobjectpacket_p.h" |
| 60 | |
| 61 | QT_BEGIN_NAMESPACE |
| 62 | |
| 63 | class QRemoteObjectSourceIo; |
| 64 | class IoDeviceBase; |
| 65 | |
| 66 | class QRemoteObjectSourceBase : public QObject |
| 67 | { |
| 68 | public: |
| 69 | ~QRemoteObjectSourceBase() override; |
| 70 | |
| 71 | void setConnections(); |
| 72 | void resetObject(QObject *newObject); |
| 73 | int qt_metacall(QMetaObject::Call call, int methodId, void **a) final; |
| 74 | QObject *m_object, *m_adapter; |
| 75 | const SourceApiMap *m_api; |
| 76 | QVariantList m_marshalledArgs; |
| 77 | bool hasAdapter() const { return m_adapter; } |
| 78 | virtual QString name() const = 0; |
| 79 | virtual bool isRoot() const = 0; |
| 80 | |
| 81 | QVariantList* marshalArgs(int index, void **a); |
| 82 | void handleMetaCall(int index, QMetaObject::Call call, void **a); |
| 83 | bool invoke(QMetaObject::Call c, int index, const QVariantList& args, QVariant* returnValue = nullptr); |
| 84 | QByteArray m_objectChecksum; |
| 85 | QMap<int, QPointer<QRemoteObjectSourceBase>> m_children; |
| 86 | struct Private { |
| 87 | Private(QRemoteObjectSourceIo *io, QRemoteObjectRootSource *root) : m_sourceIo(io), isDynamic(false), root(root) {} |
| 88 | QRemoteObjectSourceIo *m_sourceIo; |
| 89 | QVector<IoDeviceBase*> m_listeners; |
| 90 | QRemoteObjectPackets::DataStreamPacket m_packet; |
| 91 | |
| 92 | // Types needed during recursively sending a root to a new listener |
| 93 | QSet<QString> sentTypes; |
| 94 | bool isDynamic; |
| 95 | QRemoteObjectRootSource *root; |
| 96 | }; |
| 97 | Private *d; |
| 98 | static const int qobjectPropertyOffset; |
| 99 | static const int qobjectMethodOffset; |
| 100 | protected: |
| 101 | explicit QRemoteObjectSourceBase(QObject *object, Private *d, const SourceApiMap *, QObject *adapter); |
| 102 | }; |
| 103 | |
| 104 | class QRemoteObjectSource : public QRemoteObjectSourceBase |
| 105 | { |
| 106 | public: |
| 107 | explicit QRemoteObjectSource(QObject *object, Private *d, const SourceApiMap *, QObject *adapter); |
| 108 | ~QRemoteObjectSource() override; |
| 109 | |
| 110 | bool isRoot() const override { return false; } |
| 111 | QString name() const override { return m_name; } |
| 112 | |
| 113 | QString m_name; |
| 114 | }; |
| 115 | |
| 116 | class QRemoteObjectRootSource : public QRemoteObjectSourceBase |
| 117 | { |
| 118 | public: |
| 119 | explicit QRemoteObjectRootSource(QObject *object, const SourceApiMap *, |
| 120 | QObject *adapter, QRemoteObjectSourceIo *sourceIo); |
| 121 | ~QRemoteObjectRootSource() override; |
| 122 | |
| 123 | bool isRoot() const override { return true; } |
| 124 | QString name() const override { return m_name; } |
| 125 | void addListener(IoDeviceBase *io, bool dynamic = false); |
| 126 | int removeListener(IoDeviceBase *io, bool shouldSendRemove = false); |
| 127 | |
| 128 | QString m_name; |
| 129 | }; |
| 130 | |
| 131 | class DynamicApiMap final : public SourceApiMap |
| 132 | { |
| 133 | public: |
| 134 | DynamicApiMap(QObject *object, const QMetaObject *metaObject, const QString &name, const QString &typeName); |
| 135 | ~DynamicApiMap() override {} |
| 136 | QString name() const override { return m_name; } |
| 137 | QString typeName() const override { return m_typeName; } |
| 138 | QByteArray className() const override { return QByteArray(m_metaObject->className()); } |
| 139 | int enumCount() const override { return m_enumCount; } |
| 140 | int propertyCount() const override { return m_properties.size(); } |
| 141 | int signalCount() const override { return m_signals.size(); } |
| 142 | int methodCount() const override { return m_methods.size(); } |
| 143 | int sourceEnumIndex(int index) const override |
| 144 | { |
| 145 | if (index < 0 || index >= enumCount()) |
| 146 | return -1; |
| 147 | return m_enumOffset + index; |
| 148 | } |
| 149 | int sourcePropertyIndex(int index) const override |
| 150 | { |
| 151 | if (index < 0 || index >= propertyCount()) |
| 152 | return -1; |
| 153 | return m_properties.at(i: index); |
| 154 | } |
| 155 | int sourceSignalIndex(int index) const override |
| 156 | { |
| 157 | if (index < 0 || index >= signalCount()) |
| 158 | return -1; |
| 159 | return m_signals.at(i: index); |
| 160 | } |
| 161 | int sourceMethodIndex(int index) const override |
| 162 | { |
| 163 | if (index < 0 || index >= methodCount()) |
| 164 | return -1; |
| 165 | return m_methods.at(i: index); |
| 166 | } |
| 167 | int signalParameterCount(int index) const override { return parameterCount(objectIndex: m_signals.at(i: index)); } |
| 168 | int signalParameterType(int sigIndex, int paramIndex) const override { return parameterType(objectIndex: m_signals.at(i: sigIndex), paramIndex); } |
| 169 | const QByteArray signalSignature(int index) const override { return signature(objectIndex: m_signals.at(i: index)); } |
| 170 | QList<QByteArray> signalParameterNames(int index) const override; |
| 171 | |
| 172 | int methodParameterCount(int index) const override { return parameterCount(objectIndex: m_methods.at(i: index)); } |
| 173 | int methodParameterType(int methodIndex, int paramIndex) const override { return parameterType(objectIndex: m_methods.at(i: methodIndex), paramIndex); } |
| 174 | const QByteArray methodSignature(int index) const override { return signature(objectIndex: m_methods.at(i: index)); } |
| 175 | QMetaMethod::MethodType methodType(int index) const override; |
| 176 | const QByteArray typeName(int index) const override; |
| 177 | QList<QByteArray> methodParameterNames(int index) const override; |
| 178 | |
| 179 | int propertyIndexFromSignal(int index) const override |
| 180 | { |
| 181 | if (index >= 0 && index < m_propertyAssociatedWithSignal.size()) |
| 182 | return m_properties.at(i: m_propertyAssociatedWithSignal.at(i: index)); |
| 183 | return -1; |
| 184 | } |
| 185 | int propertyRawIndexFromSignal(int index) const override |
| 186 | { |
| 187 | if (index >= 0 && index < m_propertyAssociatedWithSignal.size()) |
| 188 | return m_propertyAssociatedWithSignal.at(i: index); |
| 189 | return -1; |
| 190 | } |
| 191 | QByteArray objectSignature() const override { return m_objectSignature; } |
| 192 | |
| 193 | bool isDynamic() const override { return true; } |
| 194 | |
| 195 | int parameterCount(int objectIndex) const; |
| 196 | int parameterType(int objectIndex, int paramIndex) const; |
| 197 | const QByteArray signature(int objectIndex) const; |
| 198 | inline void checkCache(int objectIndex) const |
| 199 | { |
| 200 | if (objectIndex != m_cachedMetamethodIndex) { |
| 201 | m_cachedMetamethodIndex = objectIndex; |
| 202 | m_cachedMetamethod = m_metaObject->method(index: objectIndex); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | QString m_name; |
| 207 | QString m_typeName; |
| 208 | int m_enumCount; |
| 209 | int m_enumOffset; |
| 210 | QVector<int> m_properties; |
| 211 | QVector<int> m_signals; |
| 212 | QVector<int> m_methods; |
| 213 | QVector<int> m_propertyAssociatedWithSignal; |
| 214 | const QMetaObject *m_metaObject; |
| 215 | mutable QMetaMethod m_cachedMetamethod; |
| 216 | mutable int m_cachedMetamethodIndex; |
| 217 | QByteArray m_objectSignature; |
| 218 | }; |
| 219 | |
| 220 | QT_END_NAMESPACE |
| 221 | |
| 222 | #endif |
| 223 | |