A protobuf encoder that encodes serialized proto data to a pw::stream::Writer.
While individual write calls on a proto encoder return pw::Status objects, the encoder tracks all status returns and latches onto the first error encountered. This status can be accessed via StreamEncoder::status().
BufferEncoder rather than StreamEncoder.
Public Types | |
| enum class | EmptyEncoderBehavior { kWriteFieldNumber , kWriteNothing } |
Public Member Functions | |
| constexpr | StreamEncoder (stream::Writer &writer, ByteSpan scratch_buffer) |
| StreamEncoder (stream::CountingNullStream &counting_stream) | |
| ~StreamEncoder () | |
| StreamEncoder & | operator= (const StreamEncoder &other)=delete |
| StreamEncoder (const StreamEncoder &other)=delete | |
| StreamEncoder & | operator= (StreamEncoder &&other)=delete |
| void | CloseEncoder () |
| size_t | ConservativeWriteLimit () const |
| StreamEncoder | GetNestedEncoder (uint32_t field_number, EmptyEncoderBehavior empty_encoder_behavior=EmptyEncoderBehavior::kWriteFieldNumber) |
| Status | WriteNestedMessage (uint32_t field_number, FunctionRef< Status(StreamEncoder &)> write_message, EmptyEncoderBehavior empty_encoder_behavior=EmptyEncoderBehavior::kWriteFieldNumber) |
| Status | status () const |
| Status | WriteUint32 (uint32_t field_number, uint32_t value) |
| Status | WritePackedUint32 (uint32_t field_number, span< const uint32_t > values) |
| template<typename T , typename = std::enable_if_t<std::is_enum_v<T>>> | |
| Status | WritePackedEnum (uint32_t field_number, span< const T > values) |
| Status | WriteRepeatedUint32 (uint32_t field_number, const pw::Vector< uint32_t > &values) |
| Status | WriteUint64 (uint32_t field_number, uint64_t value) |
| Status | WritePackedUint64 (uint32_t field_number, span< const uint64_t > values) |
| Status | WriteRepeatedUint64 (uint32_t field_number, const pw::Vector< uint64_t > &values) |
| Status | WriteInt32 (uint32_t field_number, int32_t value) |
| Status | WritePackedInt32 (uint32_t field_number, span< const int32_t > values) |
| Status | WriteRepeatedInt32 (uint32_t field_number, const pw::Vector< int32_t > &values) |
| Status | WriteInt64 (uint32_t field_number, int64_t value) |
| Status | WritePackedInt64 (uint32_t field_number, span< const int64_t > values) |
| Status | WriteRepeatedInt64 (uint32_t field_number, const pw::Vector< int64_t > &values) |
| Status | WriteSint32 (uint32_t field_number, int32_t value) |
| Status | WritePackedSint32 (uint32_t field_number, span< const int32_t > values) |
| Status | WriteRepeatedSint32 (uint32_t field_number, const pw::Vector< int32_t > &values) |
| Status | WriteSint64 (uint32_t field_number, int64_t value) |
| Status | WritePackedSint64 (uint32_t field_number, span< const int64_t > values) |
| Status | WriteRepeatedSint64 (uint32_t field_number, const pw::Vector< int64_t > &values) |
| Status | WriteBool (uint32_t field_number, bool value) |
| Status | WritePackedBool (uint32_t field_number, span< const bool > values) |
| Status | WriteRepeatedBool (uint32_t field_number, const pw::Vector< bool > &values) |
| Status | WriteFixed32 (uint32_t field_number, uint32_t value) |
| Status | WritePackedFixed32 (uint32_t field_number, span< const uint32_t > values) |
| Status | WriteRepeatedFixed32 (uint32_t field_number, const pw::Vector< uint32_t > &values) |
| Status | WriteFixed64 (uint32_t field_number, uint64_t value) |
| Status | WritePackedFixed64 (uint32_t field_number, span< const uint64_t > values) |
| Status | WriteRepeatedFixed64 (uint32_t field_number, const pw::Vector< uint64_t > &values) |
| Status | WriteSfixed32 (uint32_t field_number, int32_t value) |
| Status | WritePackedSfixed32 (uint32_t field_number, span< const int32_t > values) |
| Status | WriteRepeatedSfixed32 (uint32_t field_number, const pw::Vector< int32_t > &values) |
| Status | WriteSfixed64 (uint32_t field_number, int64_t value) |
| Status | WritePackedSfixed64 (uint32_t field_number, span< const int64_t > values) |
| Status | WriteRepeatedSfixed64 (uint32_t field_number, const pw::Vector< int64_t > &values) |
| Status | WriteFloat (uint32_t field_number, float value) |
| Status | WritePackedFloat (uint32_t field_number, span< const float > values) |
| Status | WriteRepeatedFloat (uint32_t field_number, const pw::Vector< float > &values) |
| Status | WriteDouble (uint32_t field_number, double value) |
| Status | WritePackedDouble (uint32_t field_number, span< const double > values) |
| Status | WriteRepeatedDouble (uint32_t field_number, const pw::Vector< double > &values) |
| Status | WriteBytes (uint32_t field_number, ConstByteSpan value) |
| Status | WriteBytes (uint32_t field_number, size_t num_bytes, FunctionRef< Status(stream::Writer &)> write_func) |
| Status | WriteBytesFromStream (uint32_t field_number, stream::Reader &bytes_reader, size_t num_bytes, ByteSpan stream_pipe_buffer) |
| Status | WriteString (uint32_t field_number, std::string_view value) |
| Status | WriteString (uint32_t field_number, const char *value, size_t len) |
| Status | WriteStringFromStream (uint32_t field_number, stream::Reader &bytes_reader, size_t num_bytes, ByteSpan stream_pipe_buffer) |
Protected Member Functions | |
| constexpr | StreamEncoder (StreamEncoder &&other) |
| Status | Write (span< const std::byte > message, span< const internal::MessageField > table) |
| StreamEncoder | GetNestedEncoder (uint32_t field_number, bool write_when_empty) |
Friends | |
| class | MemoryEncoder |
|
inlineconstexpr |
Constructs a StreamEncoder that serializes proto data to the provided writer.
The scratch buffer provided is for internal use ONLY and should not be considered valid proto data. All submessage data is buffered to this scratch buffer until the submessage is finalized.
If a StreamEncoder object will be writing nested proto messages, it must provide a scratch buffer large enough to hold the largest submessage plus some additional overhead incurred by the encoder's implementation. It is a good idea to be generous when sizing this buffer. MaxScratchBufferSize() can be helpful in providing an estimated size for this buffer. The scratch buffer must exist for the lifetime of the StreamEncoder object.
StreamEncoder objects that do not write nested proto messages can provide a zero-length scratch buffer.
\ref pw::Status::ResourceExhausted() "RESOURCE_EXHAUSTED". Always check the results of Write calls or the encoder status to ensure success, as otherwise the encoded data will be invalid.| [in] | writer | The destination stream writer for serialized data. |
| [in] | scratch_buffer | Buffer for staging nested submessages before their final sizes are known. |
|
inline |
Constructs a StreamEncoder that counts encoded bytes without writing data.
| [in] | counting_stream | The counting null stream used to measure encoded size. |
|
inline |
Destructs the StreamEncoder, closing it if active.
| void pw::protobuf::StreamEncoder::CloseEncoder | ( | ) |
Closes this encoder, finalizing its output.
This method is called automatically by StreamEncoder's destructor, but may be invoked manually in order to close an encoder before the end of its lexical scope.
|
inline |
Forwards the conservative write limit of the underlying pw::stream::Writer.
|
inline |
Creates a nested encoder with the provided field number.
Once this is called, the parent encoder is locked and not available for use until the nested encoder is finalized (either explicitly or through destruction).
The untyped encoder that this method returns can be cast to a typed encoder if needed.
| [in] | field_number | The field number for the nested message. |
| [in] | empty_encoder_behavior | Whether to write the tag number for the nested encoder if no data is written to that nested encoder. Defaults to EmptyEncoderBehavior::kWriteFieldNumber. |
|
inline |
Returns the current encoder's status.
While individual write calls return status objects, the encoder tracks all status returns and latches onto the first error encountered.
|
inline |
Writes a proto bool key-value pair.
|
inline |
Writes a proto bytes field as a key-value pair. This can also be used to write a pre-encoded nested submessage directly without using a nested encoder.
|
inline |
Provides access to a stream writer to a proto bytes field through a given callback function. The function must write exactly num_bytes bytes of data to the stream.
|
inline |
Writes a proto 'bytes' field from the stream bytes_reader.
The payload for the value is provided through the stream::Reader bytes_reader. The method reads a chunk of the data from the reader using the stream_pipe_buffer and writes it to the encoder.
stream_pipe_buffer.byte_size() >= 1 bytes_reader is exhausted before num_bytes of bytes is read.
|
inline |
Writes a proto double key-value pair.
|
inline |
Writes a proto fixed32 key-value pair.
|
inline |
Writes a proto fixed64 key-value pair.
|
inline |
Writes a proto float key-value pair.
|
inline |
Writes a proto int32 key-value pair.
|
inline |
Writes a proto int64 key-value pair.
| Status pw::protobuf::StreamEncoder::WriteNestedMessage | ( | uint32_t | field_number, |
| FunctionRef< Status(StreamEncoder &)> | write_message, | ||
| EmptyEncoderBehavior | empty_encoder_behavior = EmptyEncoderBehavior::kWriteFieldNumber |
||
| ) |
Invokes a given callback with an encoder to write a nested message field.
This performs a multi-pass encoding and invokes the callback twice: once to compute the total size of the nested message, and again to actually write the encoded data to the stream (after the nested message field prefix has been written).
| [in] | field_number | The field number of the submessage to be written. |
| [in] | write_message | A callable responsible for writing the submessage fields using the encoder passed to it. It must have the signature Status(StreamEncoder& encoder). It will be invoked twice and MUST perform the exact same set of writes on both invocations. |
| [in] | empty_encoder_behavior | Indicates the action to take when nothing is written to the nested message encoder. Defaults to EmptyEncoderBehavior::kWriteFieldNumber. |
|
inline |
Writes a repeated bool using packed encoding.
|
inline |
Writes a repeated double field using packed encoding.
|
inline |
Writes a repeated enum using packed encoding.
|
inline |
Writes a repeated fixed32 field using packed encoding.
|
inline |
Writes a repeated fixed64 field using packed encoding.
|
inline |
Writes a repeated float field using packed encoding.
|
inline |
Writes a repeated int32 using packed encoding.
|
inline |
Writes a repeated int64 using packed encoding.
|
inline |
Writes a repeated sfixed32 field using packed encoding.
|
inline |
Writes a repeated sfixed64 field using packed encoding.
|
inline |
Writes a repeated sint32 using packed encoding.
|
inline |
Writes a repeated sint64 using packed encoding.
|
inline |
Writes a repeated uint32 using packed encoding.
|
inline |
Writes a repeated uint64 using packed encoding.
|
inline |
Writes a repeated bool using packed encoding.
|
inline |
Writes a repeated double field using packed encoding.
|
inline |
Writes a repeated fixed32 field using packed encoding.
|
inline |
Writes a repeated fixed64 field using packed encoding.
|
inline |
Writes a repeated float field using packed encoding.
|
inline |
Writes a repeated int32 using packed encoding.
|
inline |
Writes a repeated int64 using packed encoding.
|
inline |
Writes a repeated sfixed32 field using packed encoding.
|
inline |
Writes a repeated sfixed64 field using packed encoding.
|
inline |
Writes a repeated sint32 using packed encoding.
|
inline |
Writes a repeated sint64 using packed encoding.
|
inline |
Writes a repeated uint32 using packed encoding.
|
inline |
Writes a repeated uint64 using packed encoding.
|
inline |
Writes a proto sfixed32 key-value pair.
|
inline |
Writes a proto sfixed64 key-value pair.
|
inline |
Writes a proto sint32 key-value pair.
|
inline |
Writes a proto sint64 key-value pair.
|
inline |
Writes a proto string key-value pair.
|
inline |
Writes a proto string key-value pair.
|
inline |
Writes a proto 'string' field from the stream bytes_reader.
The payload for the value is provided through the stream::Reader bytes_reader. The method reads a chunk of the data from the reader using the stream_pipe_buffer and writes it to the encoder.
stream_pipe_buffer.byte_size() >= 1 bytes_reader is exhausted before num_bytes of bytes is read.
|
inline |
Writes a proto uint32 key-value pair.
|
inline |
Writes a proto uint64 key-value pair.