1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt WebGL module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
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 General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include <QtCore/qdatastream.h> |
31 | #include <QtCore/qvariant.h> |
32 | |
33 | #ifndef PARAMETERS_H |
34 | #define PARAMETERS_H |
35 | |
36 | QT_BEGIN_NAMESPACE |
37 | |
38 | namespace Parameters |
39 | { |
40 | |
41 | QVariantList read(const QByteArray &data, QDataStream &stream, quint32 &offset, int count); |
42 | |
43 | template<typename T> |
44 | T readNext(QDataStream &stream, quint32 &offset) |
45 | { |
46 | T value; |
47 | stream >> value; |
48 | offset += sizeof(T); |
49 | return value; |
50 | } |
51 | |
52 | template<> |
53 | QString readNext(QDataStream &stream, quint32 &offset) |
54 | { |
55 | std::vector<char> data(readNext<quint32>(stream, offset)); |
56 | stream.readRawData(&data[0], len: data.size()); |
57 | offset += data.size(); |
58 | return QString::fromUtf8(str: data.data(), size: data.size()); |
59 | } |
60 | |
61 | template<> |
62 | QByteArray readNext(QDataStream &stream, quint32 &offset) |
63 | { |
64 | QByteArray data; |
65 | stream >> data; |
66 | offset += quint32(int(sizeof(qint32)) + data.size()); |
67 | return data; |
68 | } |
69 | |
70 | QVariantList readNextArray(const QByteArray &data, QDataStream &stream, quint32 &offset) |
71 | { |
72 | quint8 count; |
73 | stream >> count; |
74 | offset += sizeof(count); |
75 | return read(data, stream, offset, count); |
76 | } |
77 | |
78 | QVariant readNext(const QByteArray &data, QDataStream &stream, quint32 &offset) |
79 | { |
80 | char type; |
81 | offset += quint32(stream.readRawData(&type, len: 1)); |
82 | switch (type) { |
83 | case 'i': return readNext<qint32>(stream, offset); |
84 | case 'u': return readNext<quint32>(stream, offset); |
85 | case 'd': return readNext<double>(stream, offset); |
86 | case 'b': return readNext<quint8>(stream, offset); |
87 | case 's': return readNext<QString>(stream, offset); |
88 | case 'x': return readNext<QByteArray>(stream, offset); |
89 | case 'a': return readNextArray(data, stream, offset); |
90 | } |
91 | return QVariant(); |
92 | } |
93 | |
94 | QVariantList read(const QByteArray &data, QDataStream &stream, quint32 &offset) |
95 | { |
96 | QVariantList parameters; |
97 | for (const auto size = data.size(); int(offset + 4) < size;) |
98 | parameters.append(t: readNext(data, stream, offset)); |
99 | return parameters; |
100 | } |
101 | |
102 | QVariantList read(const QByteArray &data, QDataStream &stream, quint32 &offset, int count) |
103 | { |
104 | QVariantList parameters; |
105 | for (int i = 0; i < count; ++i) |
106 | parameters.append(t: readNext(data, stream, offset)); |
107 | return parameters; |
108 | } |
109 | |
110 | } |
111 | |
112 | QT_END_NAMESPACE |
113 | |
114 | #endif // PARAMETERS_H |
115 | |