64 : m_streamMsg{stream},
76 reinterpret_cast<const
pb_byte_t*>(stream.data()), stream.size())},
87 return m_streamMsg ? m_streamMsg : &m_streamLocal;
105 unsigned int flags = 0) {
131 : m_streamMsg{stream},
147 m_streamLocal.state = &out;
148 m_streamLocal.max_size = SIZE_MAX;
149 m_streamLocal.bytes_written = 0;
150 m_streamLocal.errmsg =
nullptr;
164 m_streamLocal.state = &out;
165 m_streamLocal.max_size = SIZE_MAX;
166 m_streamLocal.bytes_written = 0;
167 m_streamLocal.errmsg =
nullptr;
187 return m_streamMsg ? m_streamMsg : &m_streamLocal;
212 const typename Protobuf<std::remove_cvref_t<T>>::MessageStruct& msg) {
217 return pb_encode(&m_streamLocal, m_msgDesc, &msg);
258 } -> std::same_as<std::optional<typename std::remove_cvref_t<T>>>;
261 } -> std::same_as<bool>;
265 } -> std::same_as<const pb_msgdesc_t*>;
268 } -> std::same_as<std::string_view>;
271 } -> std::same_as<pb_filedesc_t>;
288 } -> std::same_as<bool>;
297 std::span<const uint8_t> descriptor)>
307template <ProtobufSerializable T>
316 std::optional<std::remove_cvref_t<T>>
Unpack(std::span<const uint8_t> data) {
333 auto unpacked =
Unpack(data);
337 *out = std::move(unpacked.value());
361 bool Pack(std::vector<uint8_t>& out,
const T& value) {
373 Protobuf<std::remove_cvref_t<T>>::MessageStruct::msg_descriptor());
387 std::span<const uint8_t> descriptor)>
390 Protobuf<std::remove_cvref_t<T>>::MessageStruct::msg_descriptor(),
typename std::remove_cv< remove_reference_t< T > >::type remove_cvref_t
Definition base.h:316
Class for wrapping a nanopb ostream.
Definition Protobuf.hpp:119
ProtoOutputStream(pb_ostream_t *stream)
Constructs a nanopb ostream from an existing ostream object Generally used internally for encoding me...
Definition Protobuf.hpp:130
pb_ostream_t * Stream() noexcept
Gets the backing nanopb stream object.
Definition Protobuf.hpp:186
ProtoOutputStream(detail::StdVectorType &out)
Constructs a nanopb ostream from a buffer.
Definition Protobuf.hpp:160
const pb_msgdesc_t * MsgDesc() const noexcept
Gets the nanopb message descriptor.
Definition Protobuf.hpp:203
ProtoOutputStream()
Constructs a empty nanopb stream.
Definition Protobuf.hpp:177
bool IsSubmessage() const noexcept
Gets if this stream points to a submessage, and will call pb_encode_submessage instead of pb_encode.
Definition Protobuf.hpp:196
ProtoOutputStream(detail::SmallVectorType &out)
Constructs a nanopb ostream from a buffer.
Definition Protobuf.hpp:143
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.hpp:211
Ease of use wrapper to make nanopb streams more opaque to the user.
Definition Protobuf.hpp:308
bool UnpackInto(T *out, std::span< const uint8_t > data)
Unpacks from a byte array into an existing object.
Definition Protobuf.hpp:328
void ForEachProtobufDescriptor(function_ref< bool(std::string_view filename)> exists, function_ref< void(std::string_view filename, std::span< const uint8_t > descriptor)> fn)
Loops over all protobuf descriptors including nested/referenced descriptors.
Definition Protobuf.hpp:384
bool Pack(std::vector< uint8_t > &out, const T &value)
Packs object into a std::vector.
Definition Protobuf.hpp:361
std::optional< std::remove_cvref_t< T > > Unpack(std::span< const uint8_t > data)
Unpacks from a byte array.
Definition Protobuf.hpp:316
std::string GetTypeString() const
Gets the type string for the message.
Definition Protobuf.hpp:371
bool Pack(wpi::util::SmallVectorImpl< uint8_t > &out, const T &value)
Packs object into a SmallVector.
Definition Protobuf.hpp:349
Definition BooleanTopic.hpp:24
An efficient, type-erasing, non-owning reference to a callable.
Definition function_ref.hpp:31
Specifies that a type is capable of in-place protobuf deserialization.
Definition Protobuf.hpp:283
Specifies that a type is capable of protobuf serialization and deserialization.
Definition Protobuf.hpp:251
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
Definition StringMap.hpp:773
std::vector< uint8_t > StdVectorType
Definition Protobuf.hpp:40
std::string GetTypeString(const pb_msgdesc_t *msg)
bool WriteFromSmallVector(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
void ForEachProtobufDescriptor(const pb_msgdesc_t *msg, function_ref< bool(std::string_view filename)> wants, function_ref< void(std::string_view filename, std::span< const uint8_t > descriptor)> fn)
bool WriteFromStdVector(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
wpi::util::SmallVectorImpl< uint8_t > SmallVectorType
Definition Protobuf.hpp:39
bool WriteSubmessage(pb_ostream_t *stream, const pb_msgdesc_t *desc, const void *msg)
Definition raw_os_ostream.hpp:19
struct pb_ostream_s pb_ostream_t
Definition pb.h:318
uint_least8_t pb_byte_t
Definition pb.h:228
struct pb_msgdesc_s pb_msgdesc_t
Definition pb.h:330
struct pb_istream_s pb_istream_t
Definition pb.h:317
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
bool pb_decode_ex(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct, unsigned int flags)
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Protobuf serialization template.
Definition Protobuf.hpp:36