A low-level, event-based protobuf wire format decoder that operates on a stream.
The decoder processes an encoded message by iterating over its fields using Next(). The caller can then check FieldNumber() and extract the values of fields using the Read*() methods.
While individual read calls return pw::Result, pw::StatusWithSize, or pw::Status objects, the decoder tracks all status returns and latches onto the first error encountered. This status can be accessed via StreamDecoder::status().
In the case of errors during reading, decoding stops and returns with the cursor on the field that caused the error. Unknown fields in the wire encoding are skipped automatically during iteration.
MemoryDecoder (Decoder), which avoids stream overhead.Classes | |
| struct | Bounds |
| Bounds of a payload interval within a reader. More... | |
| class | BytesReader |
Public Member Functions | |
| constexpr | StreamDecoder (stream::Reader &reader) |
| constexpr | StreamDecoder (stream::Reader &reader, size_t length) |
| StreamDecoder (const StreamDecoder &other)=delete | |
| StreamDecoder & | operator= (const StreamDecoder &other)=delete |
| Status | Next () |
| constexpr Result< uint32_t > | FieldNumber () const |
| Result< int32_t > | ReadInt32 () |
Reads a proto int32 value from the current position. | |
| StatusWithSize | ReadPackedInt32 (span< int32_t > out) |
| Status | ReadRepeatedInt32 (pw::Vector< int32_t > &out) |
| Result< uint32_t > | ReadUint32 () |
Reads a proto uint32 value from the current position. | |
| StatusWithSize | ReadPackedUint32 (span< uint32_t > out) |
| Status | ReadRepeatedUint32 (pw::Vector< uint32_t > &out) |
| template<typename T , typename = std::enable_if_t<std::is_enum_v<T>>> | |
| StatusWithSize | ReadPackedEnum (span< T > out) |
| template<typename T , typename = std::enable_if_t<std::is_enum_v<T>>> | |
| Status | ReadRepeatedEnum (pw::Vector< T > &out) |
| Result< int64_t > | ReadInt64 () |
Reads a proto int64 value from the current position. | |
| StatusWithSize | ReadPackedInt64 (span< int64_t > out) |
| Status | ReadRepeatedInt64 (pw::Vector< int64_t > &out) |
| Result< uint64_t > | ReadUint64 () |
Reads a proto uint64 value from the current position. | |
| StatusWithSize | ReadPackedUint64 (span< uint64_t > out) |
| Status | ReadRepeatedUint64 (pw::Vector< uint64_t > &out) |
| Result< int32_t > | ReadSint32 () |
Reads a proto sint32 value from the current position. | |
| StatusWithSize | ReadPackedSint32 (span< int32_t > out) |
| Status | ReadRepeatedSint32 (pw::Vector< int32_t > &out) |
| Result< int64_t > | ReadSint64 () |
Reads a proto sint64 value from the current position. | |
| StatusWithSize | ReadPackedSint64 (span< int64_t > out) |
| Status | ReadRepeatedSint64 (pw::Vector< int64_t > &out) |
| Result< bool > | ReadBool () |
Reads a proto bool value from the current position. | |
| StatusWithSize | ReadPackedBool (span< bool > out) |
| Status | ReadRepeatedBool (pw::Vector< bool > &out) |
| Result< uint32_t > | ReadFixed32 () |
Reads a proto fixed32 value from the current position. | |
| StatusWithSize | ReadPackedFixed32 (span< uint32_t > out) |
| Status | ReadRepeatedFixed32 (pw::Vector< uint32_t > &out) |
| Result< uint64_t > | ReadFixed64 () |
Reads a proto fixed64 value from the current position. | |
| StatusWithSize | ReadPackedFixed64 (span< uint64_t > out) |
| Status | ReadRepeatedFixed64 (pw::Vector< uint64_t > &out) |
| Result< int32_t > | ReadSfixed32 () |
Reads a proto sfixed32 value from the current position. | |
| StatusWithSize | ReadPackedSfixed32 (span< int32_t > out) |
| Status | ReadRepeatedSfixed32 (pw::Vector< int32_t > &out) |
| Result< int64_t > | ReadSfixed64 () |
Reads a proto sfixed64 value from the current position. | |
| StatusWithSize | ReadPackedSfixed64 (span< int64_t > out) |
| Status | ReadRepeatedSfixed64 (pw::Vector< int64_t > &out) |
| Result< float > | ReadFloat () |
Reads a proto float value from the current position. | |
| StatusWithSize | ReadPackedFloat (span< float > out) |
| Status | ReadRepeatedFloat (pw::Vector< float > &out) |
| Result< double > | ReadDouble () |
Reads a proto double value from the current position. | |
| StatusWithSize | ReadPackedDouble (span< double > out) |
| Status | ReadRepeatedDouble (pw::Vector< double > &out) |
| StatusWithSize | ReadString (span< char > out) |
| StatusWithSize | ReadBytes (span< std::byte > out) |
| BytesReader | GetBytesReader () |
| StreamDecoder | GetNestedDecoder () |
| Status | SkipField () |
| Result< Bounds > | GetLengthDelimitedPayloadBounds () |
Protected Member Functions | |
| constexpr | StreamDecoder (StreamDecoder &&other) |
| Status | Read (span< std::byte > message, span< const internal::MessageField > table) |
Friends | |
| class | BytesReader |
| class | Message |
|
inlineconstexpr |
Constructs a StreamDecoder operating on reader with unbounded length.
| [in] | reader | Source stream reader containing serialized protobuf data. |
|
inlineconstexpr |
Constructs a StreamDecoder with a specified maximum length.
Where the length of the protobuf message is known in advance, the decoder can be prevented from reading from the stream beyond the known bounds by specifying the length. When a decoder constructed in this way goes out of scope, it automatically consumes any remaining bytes up to length, allowing the next Read() on the stream to start after the protobuf even if it was not fully parsed.
| [in] | reader | Source stream reader containing serialized protobuf data. |
| [in] | length | Maximum number of bytes belonging to this protobuf message. |
|
inlineconstexpr |
Returns the field number of the current field.
Next() has not been called successfully.Next() and before any Read*() operation. | BytesReader pw::protobuf::StreamDecoder::GetBytesReader | ( | ) |
Returns a stream::Reader (BytesReader) for accessing a bytes (or string) field as a stream.
The BytesReader shares the same stream as the decoder, using RAII to manage ownership of the stream.
BytesReader is active, any use of the parent StreamDecoder that created it will trigger a crash. To resume using the parent decoder, destroy the BytesReader first.BytesReader stream reader targeting the current field. The reader supports seeking if the underlying StreamDecoder stream supports seeking. Gets the interval of the payload part of a length-delimited field.
That is, the interval excluding the field key and the length prefix. The bounds are relative to the given reader.
| StreamDecoder pw::protobuf::StreamDecoder::GetNestedDecoder | ( | ) |
Returns a decoder for a nested protobuf message located at the current position.
The nested decoder shares the same stream as its parent, using RAII to manage ownership of the stream.
StreamDecoder for reading the nested submessage. | Status pw::protobuf::StreamDecoder::Next | ( | ) |
Advances to the next field in the proto.
If Next() returns \ref pw::OkStatus() "OK", there is guaranteed to be a valid protobuf field at the current position, which can then be consumed through one of the Read*() methods.
|
inline |
Reads a proto bytes value from the current position into the provided span.
The value is copied into the provided buffer and the read size is returned. For larger bytes values that won't fit into memory, use GetBytesReader() to acquire a stream::Reader to the bytes instead.
| [out] | out | Destination span for the bytes data. |
|
inline |
Reads repeated bool values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated double values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated enum values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated fixed32 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated fixed64 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated float values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated int32 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated int64 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated sfixed32 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated sfixed64 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated sint32 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated sint64 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated uint32 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated uint64 values from the current position using packed encoding into the provided span.
| [out] | out | Destination span for read values. |
|
inline |
Reads repeated bool values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated double values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated enum values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated fixed32 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated fixed64 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated float values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated int32 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated int64 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated sfixed32 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated sfixed64 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated sint32 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated sint64 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated uint32 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads repeated uint64 values from the current position into the vector, supporting either repeated single field elements or packed encoding.
| [out] | out | Vector where values will be appended. |
|
inline |
Reads a proto string value from the current position into the provided span.
The string is copied into the provided buffer and the read size is returned. Since the span is updated with the size of the string, the string is NOT automatically null-terminated; this should be done manually if desired. pw_string provides utility methods to copy string data from spans into other targets.
| [out] | out | Destination span for the string data. |
| Status pw::protobuf::StreamDecoder::SkipField | ( | ) |
Consumes the current protobuf field, advancing the stream to the key of the next field (if one exists).