Expressive interface for encoding and decoding protocol buffers.
|
| Result< uint32_t > | pw::protobuf::DecodeBytesToUint32 (Decoder &decoder) |
| |
| constexpr size_t | pw::protobuf::MaxScratchBufferSize (size_t max_message_size, size_t max_nested_depth) |
| |
| Status | pw::protobuf::WriteVarint (uint64_t value, stream::Writer &writer) |
| |
| Status | pw::protobuf::WriteLengthDelimitedKeyAndLengthPrefix (uint32_t field_number, size_t payload_size, stream::Writer &writer) |
| |
| template<typename ToStreamEncoder , typename FromStreamEncoder > |
| ToStreamEncoder & | pw::protobuf::StreamEncoderCast (FromStreamEncoder &encoder) |
| |
| Status | pw::protobuf::WriteProtoStringToBytesMapEntry (uint32_t field_number, stream::Reader &key, size_t key_size, stream::Reader &value, size_t value_size, ByteSpan stream_pipe_buffer, stream::Writer &writer) |
| |
| template<typename T > |
| constexpr size_t | pw::protobuf::TagSizeBytes (T field_number) |
| |
| template<typename T , typename U > |
| constexpr size_t | pw::protobuf::SizeOfVarintField (T field_number, U value) |
| |
| template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfDelimitedFieldWithoutValue (T field_number, uint32_t length_bytes=std::numeric_limits< uint32_t >::max()) |
| |
| template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfDelimitedField (T field_number, uint32_t length_bytes) |
| |
| template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfField (T field_number, WireType type, size_t data_size_bytes) |
| |
| template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldFloat (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldDouble (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldInt32 (T field_number, int32_t value=-1) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldInt64 (T field_number, int64_t value=-1) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldSint32 (T field_number, int32_t value=std::numeric_limits< int32_t >::min()) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldSint64 (T field_number, int64_t value=std::numeric_limits< int64_t >::min()) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldUint32 (T field_number, uint32_t value=std::numeric_limits< uint32_t >::max()) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldUint64 (T field_number, uint64_t value=std::numeric_limits< uint64_t >::max()) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldFixed32 (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldFixed64 (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldSfixed32 (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldSfixed64 (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldBool (T field_number) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldString (T field_number, uint32_t length_bytes) |
| |
|
template<typename T > |
| constexpr size_t | pw::protobuf::SizeOfFieldBytes (T field_number, uint32_t length_bytes) |
| |
|
template<typename T , typename U = int32_t> |
| constexpr size_t | pw::protobuf::SizeOfFieldEnum (T field_number, U value=static_cast< U >(-1)) |
| |
|
constexpr bool | pw::protobuf::ValidFieldNumber (uint32_t field_number) |
| |
|
constexpr bool | pw::protobuf::ValidFieldNumber (uint64_t field_number) |
| |
|
constexpr uint32_t | pw::protobuf::MakeKey (uint32_t field_number, WireType wire_type) |
| |
|
template<> |
| Uint32 | pw::protobuf::Message::Field::As () |
| |
| #define PW_PROTOBUF_CFG_MAX_VARINT_SIZE 4 |
When encoding nested messages, this macro defines the number of bytes to reserve for the varint submessage length prefix. Nested messages are limited in size to the maximum value that can be varint-encoded into this reserved space.
The values that can be set, and their corresponding maximum submessage lengths, are outlined below:
1 byte => 127 bytes
2 bytes => 16,383 bytes (< 16 KiB)
3 bytes => 2,097,151 bytes (< 2048 KiB)
4 bytes => 268,435,455 bytes (< 256 MiB)
5 bytes => 4,294,967,295 bytes (< 4 GiB, max uint32_t)
| constexpr size_t pw::protobuf::MaxScratchBufferSize |
( |
size_t |
max_message_size, |
|
|
size_t |
max_nested_depth |
|
) |
| |
|
constexpr |
Provides a size estimate to help with sizing buffers passed to StreamEncoder and MemoryEncoder objects.
Writing proto messages with nested submessages requires buffering due to limitations of the proto format. Every proto submessage must know the size of the submessage before its final serialization can begin. A streaming encoder can be passed a scratch buffer to use when constructing nested messages. All submessage data is buffered to this scratch buffer until the submessage is finalized. Note that the contents of this scratch buffer is not necessarily valid proto data, so don't try to use it directly.
- See also
- If you need to avoid scratch buffers or reduce copying of nested submessages, consider using
BufferEncoder instead of StreamEncoder or MemoryEncoder.
The code generation includes a kScratchBufferSizeBytes constant that represents the size of the largest submessage and all necessary overhead, excluding the contents of any field values which require a callback. If a submessage field requires a callback due to a dependency cycle or a repeated field of unknown length, add the appropriate structure's kMaxEncodedSizeBytes constant to the scratch buffer size to guarantee enough space.
When calculating buffer sizes yourself, MaxScratchBufferSize() can be useful in estimating how much space to allocate to account for nested submessage encoding overhead.
- Warning
- If the scratch buffer size is not sufficient, the encoding will fail with RESOURCE_EXHAUSTED. Always check the results of
Write calls or the encoder status to ensure success, as otherwise the encoded data will be invalid.
- Parameters
-
| [in] | max_message_size | For MemoryEncoder objects, this is the max expected size of the final proto. For StreamEncoder objects, this should be the max size of any nested proto submessage that will be built with this encoder (recursively accumulating the size from the root submessage). If your proto will encode many large submessages, this value should just be the size of the largest one. |
| [in] | max_nested_depth | The max number of nested submessage encoders that are expected to be open simultaneously to encode this proto message. |
template<typename ToStreamEncoder , typename FromStreamEncoder >
| ToStreamEncoder & pw::protobuf::StreamEncoderCast |
( |
FromStreamEncoder & |
encoder | ) |
|
|
inline |
pw_protobuf guarantees that all generated StreamEncoder classes can be converted among each other. It is also safe to convert any MemoryEncoder to any other StreamEncoder.
This guarantee exists to facilitate usage of protobuf overlays. Protobuf overlays are protobuf message definitions that deliberately ensure that fields defined in one message will not conflict with fields defined in other messages.
message BaseMessage {
uint32 length = 1;
reserved 2;
}
message Overlay {
reserved 1;
uint32 height = 2;
}
message Both {
uint32 length = 1;
uint32 height = 2;
}
message InvalidOverlay {
fixed32 length = 1;
}
While this use case is somewhat uncommon, it is a core supported use case of pw_protobuf.
- Warning
- Using this to convert one stream encoder to another when the messages themselves do not safely overlay will result in corrupt protos. Be careful when doing this as there is no compile-time way to detect whether or not two messages are meant to overlay.
- Parameters
-
| [in] | encoder | The encoder to cast. |
- Returns
- A reference to the casted
StreamEncoder type.