KItinerary

protobufstreamreader.h
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#ifndef KITINERARY_PROTOBUFSTREAMREADER_H
7#define KITINERARY_PROTOBUFSTREAMREADER_H
8
9#include <QByteArray>
10#include <QMetaType>
11
12#include <string_view>
13
14class QString;
15
16namespace KItinerary {
17
18/** Protocol Buffers stream reader.
19 * For use on protobuf data for which the full format definition is unknown.
20 * @see https://protobuf.dev/
21 */
23{
24 Q_GADGET
25public:
26 explicit ProtobufStreamReader();
27 explicit ProtobufStreamReader(std::string_view data);
28 explicit ProtobufStreamReader(const QByteArray &data);
30
31 /** Returns the number of the current field.
32 * Assumes the cursor is on the beginning of a field. The cursor does not advance.
33 */
34 Q_INVOKABLE quint64 fieldNumber();
35
36 enum WireType {
37 VARINT,
38 I64,
39 LEN,
40 SGROUP,
41 EGROUP,
42 I32,
43 };
44 Q_ENUM(WireType)
45
46 /** Returns the wire type of the current field.
47 * Assumes the cursor is on the beginning of a field. The cursor does not advance.
48 */
49 Q_INVOKABLE WireType wireType();
50
51 /** Read a field of type VARINT.
52 * This assumes the cursor is placed at the beginning of a field with wire type VARINT.
53 * The cursor is advanced to after the field.
54 */
55 Q_INVOKABLE quint64 readVarintField();
56
57 /** Reads a field of type LEN.
58 * This assumes the cursor is placed at the beginning of a field with wire type LEN.
59 * The cursor is advanced to after the field.
60 */
61 std::string_view readLengthDelimitedRecord();
62
63 /** Reads a string.
64 * This assumes the cursor is placed at the beginning of a field with wire type LEN
65 * containing a string. The cursor is advanced to after the field.
66 */
67 Q_INVOKABLE QString readString();
68
69 /** Reads a nested message.
70 * This assumes the cursor is placed at the beginning of a field with wire type LEN
71 * containing a sub-message. The cursor is advanced to after the field.
72 */
74
75 /** Returns @c true when having reached the end of the stream. */
76 Q_INVOKABLE bool atEnd() const;
77
78 /** Skips over the next field in the stream. */
79 Q_INVOKABLE void skip();
80
81 ///@cond internal
82 /** Read Base 128 varint value from the current stream position and advances the cursor. */
83 uint64_t readVarint();
84 /** Read Base 128 varint value from the current stream position without advancing the cursor. */
85 uint64_t peekVarint();
86 ///@endcond
87
88private:
89 QByteArray m_ownedData;
90 std::string_view m_data;
91 std::string_view::size_type m_cursor = 0;
92};
93
94}
95
96#endif // KITINERARY_PROTOBUFSTREAMREADER_H
Protocol Buffers stream reader.
Q_INVOKABLE void skip()
Skips over the next field in the stream.
Q_INVOKABLE QString readString()
Reads a string.
Q_INVOKABLE WireType wireType()
Returns the wire type of the current field.
Q_INVOKABLE bool atEnd() const
Returns true when having reached the end of the stream.
Q_INVOKABLE KItinerary::ProtobufStreamReader readSubMessage()
Reads a nested message.
Q_INVOKABLE quint64 fieldNumber()
Returns the number of the current field.
std::string_view readLengthDelimitedRecord()
Reads a field of type LEN.
Q_INVOKABLE quint64 readVarintField()
Read a field of type VARINT.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 4 2024 12:00:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.