1 | // Copyright (C) 2022 The Qt Company Ltd. |
2 | // Copyright (C) 2019 Alexey Edelev <semlanik@gmail.com> |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include <QtProtobuf/qabstractprotobufserializer.h> |
6 | |
7 | #include <QtProtobuf/qprotobufmessage.h> |
8 | |
9 | QT_BEGIN_NAMESPACE |
10 | |
11 | /*! |
12 | \class QAbstractProtobufSerializer |
13 | \inmodule QtProtobuf |
14 | \since 6.5 |
15 | \brief The QAbstractProtobufSerializer class is interface that represents |
16 | basic functions for serialization/deserialization. |
17 | \reentrant |
18 | |
19 | The QProtobufSerializer class registers serializers/deserializers for |
20 | classes implementing a protobuf message, inheriting QProtobufMessage. These |
21 | classes are generated automatically, based on a .proto file, using the cmake |
22 | build macro qt6_add_protobuf or by running qtprotobufgen directly. |
23 | |
24 | This class should be used as a base for specific serializers. The handlers |
25 | property contains all message-specific serializers and should be used while |
26 | serialization/deserialization. Inherited classes should reimplement scope of |
27 | virtual methods that used by registered message |
28 | serialization/deserialization functions. |
29 | */ |
30 | |
31 | /*! |
32 | \enum QAbstractProtobufSerializer::DeserializationError |
33 | |
34 | This enum contains possible errors that can occur during deserialization. |
35 | When an error occurs, call deserializationErrorString() to get a |
36 | human-readable error message. |
37 | |
38 | \value NoError No error occurred. |
39 | \value InvalidHeaderError Something went wrong while attempting to |
40 | decode a header in the message. |
41 | \value NoDeserializerError While deserializing a message, no |
42 | deserializer was found for a type in the |
43 | message. |
44 | \value UnexpectedEndOfStreamError While deserializing a message, the |
45 | stream ended unexpectedly. |
46 | */ |
47 | |
48 | /*! |
49 | Destroys this QAbstractProtobufSerializer. |
50 | */ |
51 | QAbstractProtobufSerializer::~QAbstractProtobufSerializer() = default; |
52 | |
53 | /*! |
54 | \fn template<typename T> QAbstractProtobufSerializer::serialize(const QProtobufMessage *message) const |
55 | |
56 | This function serializes a registered Protobuf message \a message into a |
57 | QByteArray. \a message must not be \nullptr. |
58 | |
59 | For a given type, \c{T}, you should call the \c{serialize()} function on an |
60 | instance of that type, which in turn will call this function for you. |
61 | |
62 | \sa deserialize() |
63 | */ |
64 | |
65 | QByteArray QAbstractProtobufSerializer::doSerialize(const QProtobufMessage *message, |
66 | const QtProtobufPrivate::QProtobufPropertyOrdering &ordering) const |
67 | { |
68 | Q_ASSERT(message); |
69 | return serializeMessage(message, ordering); |
70 | } |
71 | |
72 | /*! |
73 | \fn template<typename T> QAbstractProtobufSerializer::deserialize(T *object, QByteArrayView data) const |
74 | |
75 | This function deserializes a registered Protobuf message \a object from a |
76 | QByteArray \a data. \a object must not be \nullptr. |
77 | Returns \c true if deserialization was successful, otherwise \c false. |
78 | |
79 | For a given type, \c{T}, you should call the \c{deserialize()} function on |
80 | an instance of that type, which in turn will call this function for you. |
81 | |
82 | Unexpected/unknown properties in the \a data are skipped. |
83 | |
84 | \sa serialize() |
85 | */ |
86 | |
87 | bool QAbstractProtobufSerializer::doDeserialize(QProtobufMessage *message, |
88 | const QtProtobufPrivate::QProtobufPropertyOrdering &ordering, QByteArrayView data) const |
89 | { |
90 | Q_ASSERT(message); |
91 | return deserializeMessage(message, ordering, data); |
92 | } |
93 | |
94 | /*! |
95 | \fn QByteArray QAbstractProtobufSerializer::serializeMessage(const QProtobufMessage *message, const QtProtobufPrivate::QProtobufPropertyOrdering &ordering) const |
96 | |
97 | This is called by serialize() to serialize a registered Protobuf message |
98 | \a message with \a ordering. \a message must not be \nullptr. |
99 | Returns a QByteArray containing the serialized message. |
100 | */ |
101 | |
102 | /*! |
103 | \fn bool QAbstractProtobufSerializer::deserializeMessage(QProtobufMessage *message, const QtProtobufPrivate::QProtobufPropertyOrdering &ordering, QByteArrayView data) const |
104 | |
105 | This is called by deserialize() to deserialize a registered Protobuf message |
106 | \a message with \a ordering from a QByteArrayView \a data. \a message can be |
107 | assumed to not be \nullptr. |
108 | Returns \c true if deserialization was successful, otherwise \c false. |
109 | */ |
110 | |
111 | namespace QtProtobufPrivate { |
112 | extern QtProtobufPrivate::QProtobufPropertyOrdering getOrderingByMetaType(QMetaType type); |
113 | } |
114 | |
115 | QByteArray QAbstractProtobufSerializer::serializeRawMessage(const QProtobufMessage *message) const |
116 | { |
117 | Q_ASSERT(message != nullptr && message->metaObject() != nullptr); |
118 | auto ordering = QtProtobufPrivate::getOrderingByMetaType(type: message->metaObject()->metaType()); |
119 | Q_ASSERT(ordering.data != nullptr); |
120 | return serializeMessage(message, ordering); |
121 | } |
122 | |
123 | bool QAbstractProtobufSerializer::deserializeRawMessage(QProtobufMessage *message, QByteArrayView data) const |
124 | { |
125 | Q_ASSERT(message != nullptr && message->metaObject() != nullptr); |
126 | auto ordering = QtProtobufPrivate::getOrderingByMetaType(type: message->metaObject()->metaType()); |
127 | Q_ASSERT(ordering.data != nullptr); |
128 | return deserializeMessage(message, ordering, data); |
129 | } |
130 | |
131 | QT_END_NAMESPACE |
132 | |