C/C++ API Reference
Loading...
Searching...
No Matches
pw::protobuf::CallbackDecoder Class Reference

Overview

A protobuf decoder that iterates over an encoded protobuf, calling a handler for each field it encounters.

class FooProtoHandler : public DecodeHandler {
public:
Status ProcessField(CallbackDecoder& decoder,
uint32_t field_number) override {
switch (field_number) {
case FooFields::kBar:
if (!decoder.ReadSint32(&bar).ok()) {
bar = 0;
}
break;
case FooFields::kBaz:
if (!decoder.ReadUint32(&baz).ok()) {
baz = 0;
}
break;
}
return OkStatus();
}
int bar;
unsigned int baz;
};
void DecodeFooProto(span<std::byte> raw_proto) {
Decoder decoder;
FooProtoHandler handler;
decoder.set_handler(&handler);
if (!decoder.Decode(raw_proto).ok()) {
LOG_FATAL("Invalid foo message!");
}
LOG_INFO("Read Foo proto message; bar: %d baz: %u",
handler.bar, handler.baz);
}
Definition: status.h:120
constexpr bool ok() const
Definition: status.h:346
Definition: decoder.h:308
void set_handler(DecodeHandler *handler)
Definition: decoder.h:319
Status ReadUint32(uint32_t *out)
Definition: decoder.h:338
Status ReadSint32(int32_t *out)
Definition: decoder.h:353
Definition: decoder.h:432

Public Member Functions

 CallbackDecoder (const CallbackDecoder &other)=delete
 
CallbackDecoderoperator= (const CallbackDecoder &other)=delete
 
void set_handler (DecodeHandler *handler)
 
Status Decode (span< const std::byte > proto)
 
Status ReadInt32 (int32_t *out)
 
Status ReadUint32 (uint32_t *out)
 
Status ReadInt64 (int64_t *out)
 
Status ReadUint64 (uint64_t *out)
 
Status ReadSint32 (int32_t *out)
 
Status ReadSint64 (int64_t *out)
 
Status ReadBool (bool *out)
 
Status ReadFixed32 (uint32_t *out)
 
Status ReadFixed64 (uint64_t *out)
 
Status ReadSfixed32 (int32_t *out)
 
Status ReadSfixed64 (int64_t *out)
 
Status ReadFloat (float *out)
 
Status ReadDouble (double *out)
 
Status ReadString (std::string_view *out)
 
Status ReadBytes (span< const std::byte > *out)
 
bool cancelled () const
 

Member Function Documentation

◆ Decode()

Status pw::protobuf::CallbackDecoder::Decode ( span< const std::byte >  proto)

Decodes the specified protobuf data.

The registered handler's ProcessField function is called on each field found in the data.

Parameters
[in]protoSpan containing the serialized protobuf data.
Returns
Status resulting from the decoding operation or handler return.

◆ ReadBool()

Status pw::protobuf::CallbackDecoder::ReadBool ( bool *  out)
inline

Reads a proto bool value from the current cursor.

Parameters
[out]outPointer to store the read bool value.

◆ ReadBytes()

Status pw::protobuf::CallbackDecoder::ReadBytes ( span< const std::byte > *  out)
inline

Reads a proto bytes value from the current cursor as a view.

Note
The raw protobuf data must outlive out. If the bytes field is invalid, out is not modified.
Parameters
[out]outPointer to store the byte span view (span<const std::byte>).

◆ ReadDouble()

Status pw::protobuf::CallbackDecoder::ReadDouble ( double *  out)
inline

Reads a proto double value from the current cursor.

Parameters
[out]outPointer to store the read double value.

◆ ReadFixed32()

Status pw::protobuf::CallbackDecoder::ReadFixed32 ( uint32_t *  out)
inline

Reads a proto fixed32 value from the current cursor.

Parameters
[out]outPointer to store the read fixed32 value.

◆ ReadFixed64()

Status pw::protobuf::CallbackDecoder::ReadFixed64 ( uint64_t *  out)
inline

Reads a proto fixed64 value from the current cursor.

Parameters
[out]outPointer to store the read fixed64 value.

◆ ReadFloat()

Status pw::protobuf::CallbackDecoder::ReadFloat ( float *  out)
inline

Reads a proto float value from the current cursor.

Parameters
[out]outPointer to store the read float value.

◆ ReadInt32()

Status pw::protobuf::CallbackDecoder::ReadInt32 ( int32_t *  out)
inline

Reads a proto int32 value from the current cursor.

Parameters
[out]outPointer to store the read int32 value.

◆ ReadInt64()

Status pw::protobuf::CallbackDecoder::ReadInt64 ( int64_t *  out)
inline

Reads a proto int64 value from the current cursor.

Parameters
[out]outPointer to store the read int64 value.

◆ ReadSfixed32()

Status pw::protobuf::CallbackDecoder::ReadSfixed32 ( int32_t *  out)
inline

Reads a proto sfixed32 value from the current cursor.

Parameters
[out]outPointer to store the read sfixed32 value.

◆ ReadSfixed64()

Status pw::protobuf::CallbackDecoder::ReadSfixed64 ( int64_t *  out)
inline

Reads a proto sfixed64 value from the current cursor.

Parameters
[out]outPointer to store the read sfixed64 value.

◆ ReadSint32()

Status pw::protobuf::CallbackDecoder::ReadSint32 ( int32_t *  out)
inline

Reads a proto sint32 value from the current cursor.

Parameters
[out]outPointer to store the read sint32 value.

◆ ReadSint64()

Status pw::protobuf::CallbackDecoder::ReadSint64 ( int64_t *  out)
inline

Reads a proto sint64 value from the current cursor.

Parameters
[out]outPointer to store the read sint64 value.

◆ ReadString()

Status pw::protobuf::CallbackDecoder::ReadString ( std::string_view *  out)
inline

Reads a proto string value from the current cursor as a view.

Note
The raw protobuf data must outlive out. If the string field is invalid, out is not modified.
Parameters
[out]outPointer to store the std::string_view.

◆ ReadUint32()

Status pw::protobuf::CallbackDecoder::ReadUint32 ( uint32_t *  out)
inline

Reads a proto uint32 value from the current cursor.

Parameters
[out]outPointer to store the read uint32 value.

◆ ReadUint64()

Status pw::protobuf::CallbackDecoder::ReadUint64 ( uint64_t *  out)
inline

Reads a proto uint64 value from the current cursor.

Parameters
[out]outPointer to store the read uint64 value.

◆ set_handler()

void pw::protobuf::CallbackDecoder::set_handler ( DecodeHandler handler)
inline

Sets the DecodeHandler to process encountered fields.

Parameters
[in]handlerPointer to the decode handler instance.

The documentation for this class was generated from the following file: