summaryrefslogtreecommitdiffstats
path: root/src/remoteobjects/qremoteobjectcontainers.cpp
blob: 9e4cc40a993ca013693c0029c02dcbfb1a1bcd7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (C) 2021 Ford Motor Company
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include <QtCore/qiodevice.h>

#include "qremoteobjectcontainers_p.h"
#include "qremoteobjectpacket_p.h"

QT_BEGIN_NAMESPACE

QDataStream &operator>>(QDataStream &ds, QtROSequentialContainer &p)
{
    QByteArray typeName;
    quint32 count;
    ds >> typeName;
    p.setValueType(typeName);
    ds >> count;
    p.reserve(count);
    QVariant value{p.m_valueType, nullptr};
    for (quint32 i = 0; i < count; i++) {
        if (!p.m_valueType.load(ds, value.data())) {
            qWarning("QSQ_: unable to load type '%s', returning an empty list.", p.m_valueTypeName.constData());
            p.clear();
            break;
        }
        p.append(value);
    }
    return ds;
}

QDataStream &operator<<(QDataStream &ds, const QtROSequentialContainer &p)
{
    ds << p.m_valueTypeName;
    auto pos = ds.device()->pos();
    quint32 count = p.size();
    ds << count;
    for (quint32 i = 0; i < count; i++) {
        if (!p.m_valueType.save(ds, p.at(i).data())) {
            ds.device()->seek(pos);
            ds.resetStatus();
            ds << quint32(0);
            qWarning("QSQ_: unable to save type '%s'.", p.m_valueTypeName.constData());
            break;
        }
    }
    return ds;
}

const char *descopedName(QMetaType type) {
    auto name = QByteArray::fromRawData(type.name(), qstrlen(type.name()));
    int index = name.lastIndexOf(':'); // Returns -1 if not found
    return type.name() + index + 1;
}

QDataStream &operator>>(QDataStream &ds, QtROAssociativeContainer &p)
{
    QByteArray keyTypeName, valueTypeName;
    quint32 count;
    ds >> keyTypeName;
    ds >> valueTypeName;
    p.setTypes(keyTypeName, valueTypeName);
    ds >> count;
    p.m_keys.reserve(count);
    auto transferType = p.m_keyType;
    if (p.m_keyType.flags().testFlag(QMetaType::IsEnumeration))
        transferType = QRemoteObjectPackets::transferTypeForEnum(p.m_keyType);
    QVariant key{transferType, nullptr};
    QVariant value{p.m_valueType, nullptr};
    for (quint32 i = 0; i < count; i++) {
        if (!transferType.load(ds, key.data())) {
            qWarning("QAS_: unable to load key '%s', returning an empty map.", p.m_keyTypeName.constData());
            p.clear();
            break;
        }
        if (!p.m_valueType.load(ds, value.data())) {
            qWarning("QAS_: unable to load value '%s', returning an empty map.", p.m_valueTypeName.constData());
            p.clear();
            break;
        }
        if (transferType != p.m_keyType) {
            bool isFlag = false;
            QVariant enumKey(key);
            enumKey.convert(p.m_keyType);
            p.m_keys.append(enumKey);
            if (auto meta = p.m_keyType.metaObject()) {
                int index = meta->indexOfEnumerator(descopedName(p.m_keyType));
                isFlag = meta->enumerator(index).isFlag();
            }
            // If multiple flag values are set, toString() returns an empty string
            // Thus, for flags, we convert the integer value to a string
            if (isFlag)
                p.insert(key.toString(), value);
            else
                p.insert(enumKey.toString(), value);
        } else {
            p.insert(key.toString(), value);
            p.m_keys.append(key);
        }
    }
    return ds;
}

QDataStream &operator<<(QDataStream &ds, const QtROAssociativeContainer &p)
{
    ds << p.m_keyTypeName;
    ds << p.m_valueTypeName;
    auto pos = ds.device()->pos();
    quint32 count = p.size();
    ds << count;
    QAssociativeIterable map(&p);
    QAssociativeIterable::const_iterator iter = map.begin();
    auto transferType = p.m_keyType;
    if (p.m_keyType.flags().testFlag(QMetaType::IsEnumeration))
        transferType = QRemoteObjectPackets::transferTypeForEnum(p.m_keyType);
    bool keySaved;
    for (quint32 i = 0; i < count; i++) {
        if (transferType != p.m_keyType) {
            QVariant intKey(iter.key());
            intKey.convert(transferType);
            keySaved = transferType.save(ds, intKey.data());
        } else {
            keySaved = transferType.save(ds, iter.key().data());
        }
        if (!keySaved) {
            ds.device()->seek(pos);
            ds.resetStatus();
            ds << quint32(0);
            qWarning("QAS_: unable to save type '%s'.", p.m_valueTypeName.constData());
            break;
        }
        if (!p.m_valueType.save(ds, iter.value().data())) {
            ds.device()->seek(pos);
            ds.resetStatus();
            ds << quint32(0);
            qWarning("QAS_: unable to save type '%s'.", p.m_valueTypeName.constData());
            break;
        }
        iter++;
    }
    return ds;
}

QT_END_NAMESPACE