14#include "wpi/util/SmallVector.hpp"
34concept StringLike = std::is_convertible_v<T, std::string_view>;
47 { t.resize(
size_t()) };
48 { t.size() } -> std::same_as<size_t>;
49 { t.data() } -> std::convertible_to<void*>;
72template <Val
idatable T>
76 return std::integral<T>;
80 return std::unsigned_integral<T>;
82 return std::signed_integral<T>;
84 return std::integral<T> || std::floating_point<T>;
86 return std::integral<T> || std::floating_point<T>;
111template <ProtoCallbackUnpackable T,
typename U,
size_t N = 1>
120 m_callback.funcs.decode = CallbackFunc;
121 m_callback.arg =
this;
144 bool SizeCheck(
bool* retVal)
const {
145 if (m_storage.size() >= N) {
163 if constexpr (ProtoPackable<T>) {
166 if constexpr (std::integral<T>) {
171 m_storage.emplace_back(
static_cast<T
>(val));
177 if constexpr (std::signed_integral<T> || ProtoEnumeration<T>) {
182 m_storage.emplace_back(
static_cast<T
>(val));
188 if constexpr (std::unsigned_integral<T>) {
193 m_storage.emplace_back(
static_cast<T
>(val));
199 if constexpr (std::signed_integral<T>) {
204 m_storage.emplace_back(
static_cast<T
>(val));
210 if constexpr (std::signed_integral<T>) {
215 m_storage.emplace_back(
static_cast<T
>(val));
217 }
else if constexpr (std::unsigned_integral<T>) {
222 m_storage.emplace_back(
static_cast<T
>(val));
225 if constexpr (std::floating_point<T>) {
230 m_storage.emplace_back(
static_cast<T
>(val));
236 if constexpr (std::signed_integral<T>) {
241 m_storage.emplace_back(
static_cast<T
>(val));
243 }
else if constexpr (std::unsigned_integral<T>) {
248 m_storage.emplace_back(
static_cast<T
>(val));
251 if constexpr (std::floating_point<T>) {
256 m_storage.emplace_back(
static_cast<T
>(val));
264 }
else if constexpr (UnpackBytes<T>) {
265 T&
space = m_storage.emplace_back(T{});
269 }
else if constexpr (ProtobufSerializable<T>) {
270 ProtoInputStream<T> istream{stream};
271 auto decoded = wpi::util::Protobuf<T>::Unpack(istream);
272 if (decoded.has_value()) {
273 m_storage.emplace_back(std::move(decoded.value()));
288 if constexpr (ProtoPackable<T>) {
292 if (!SizeCheck(&sizeRetVal)) {
296 if (!Decode(stream, fieldType)) {
304 if (!SizeCheck(&sizeRetVal)) {
309 return Decode(stream, fieldType);
333template <ProtoCallbackUnpackable T,
size_t N = 1>
351 std::span<T>
Items() noexcept {
return m_storedBuffer; }
358 std::span<const T>
Items() const noexcept {
return m_storedBuffer; }
365 wpi::util::SmallVector<T, N>&
Vec() noexcept {
return m_storedBuffer; }
368 wpi::util::SmallVector<T, N> m_storedBuffer;
380template <ProtoCallbackUnpackable T,
size_t N = 1>
397 std::span<T>
Items() noexcept {
return m_storedBuffer; }
404 std::span<const T>
Items() const noexcept {
return m_storedBuffer; }
411 std::vector<T>&
Vec() noexcept {
return m_storedBuffer; }
414 std::vector<T> m_storedBuffer;
421template <ProtoCallbackUnpackable T,
size_t N>
428 template <
typename... ArgTypes>
445template <ProtoCallbackUnpackable T,
size_t N>
461 bool IsFull() const noexcept {
return m_array.m_currentIndex == N; }
468 size_t Size() const noexcept {
return m_array.m_currentIndex; }
486template <ProtoCallbackPackable T>
494 m_callback.funcs.encode = CallbackFunc;
495 m_callback.arg =
this;
505 : m_buffer{
std::span<const T>{element, 1}} {
506 m_callback.funcs.encode = CallbackFunc;
507 m_callback.arg =
this;
526 std::span<const T>
Bufs()
const {
return m_buffer; }
529 static auto EncodeStreamTypeFinder() {
536 using EncodeStreamType =
decltype(EncodeStreamTypeFinder());
538 bool EncodeItem(EncodeStreamType& stream,
const pb_field_t* field,
539 const T& value)
const {
540 if constexpr (std::floating_point<T>) {
544 float flt =
static_cast<float>(value);
548 double dbl =
static_cast<double>(value);
575 std::string_view view{value};
577 reinterpret_cast<const pb_byte_t*
>(view.data()),
580 std::span<const uint8_t> view{value};
582 reinterpret_cast<const pb_byte_t*
>(view.data()),
585 return wpi::util::Protobuf<T>::Pack(stream, value);
590 bool writeTag)
const {
593 for (
auto&& i : m_buffer) {
599 if (!EncodeItem(ostream, field, i)) {
604 for (
auto&& i : m_buffer) {
610 if (!EncodeItem(*stream, field, i)) {
624 if (!EncodeLoop(&substream, field,
false)) {
639 return EncodeLoop(stream, field,
false);
644 if (m_buffer.empty()) {
655 return PackedEncode(stream, field);
657 return EncodeLoop(stream, field,
true);
663 return reinterpret_cast<const PackCallback*
>(*arg)->CallbackFunc(stream,
667 std::span<const T> m_buffer;
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Returns a named argument to be used in a formatting function.
Definition base.h:2846
@ space
Definition base.h:689
void SetLimits(DecodeLimits limit) noexcept
Set the limits on what happens if more elements exist in the buffer then expected.
Definition ProtobufCallbacks.hpp:134
DirectUnpackCallback(U &storage)
Constructs a callback from a vector like type.
Definition ProtobufCallbacks.hpp:119
DirectUnpackCallback(DirectUnpackCallback &&)=delete
DirectUnpackCallback(const DirectUnpackCallback &)=delete
DirectUnpackCallback & operator=(DirectUnpackCallback &&)=delete
DirectUnpackCallback & operator=(const DirectUnpackCallback &)=delete
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:141
A callback method that will pack elements when called.
Definition ProtobufCallbacks.hpp:487
PackCallback(const PackCallback &)=delete
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:519
PackCallback & operator=(const PackCallback &)=delete
PackCallback(const T *element)
Constructs a pack callback from a pointer to a single element.
Definition ProtobufCallbacks.hpp:504
PackCallback(PackCallback &&)=delete
std::span< const T > Bufs() const
Gets a span pointing to the items.
Definition ProtobufCallbacks.hpp:526
PackCallback & operator=(PackCallback &&)=delete
PackCallback(std::span< const T > buffer)
Constructs a pack callback from a span of elements.
Definition ProtobufCallbacks.hpp:493
Class for wrapping a nanopb ostream.
Definition Protobuf.hpp:119
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.hpp:411
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.hpp:397
std::span< const T > Items() const noexcept
Gets a const span pointing to the storage buffer.
Definition ProtobufCallbacks.hpp:404
StdVectorUnpackCallback()
Constructs a StdVectorUnpackCallback.
Definition ProtobufCallbacks.hpp:387
UnpackCallback()
Constructs an UnpackCallback.
Definition ProtobufCallbacks.hpp:340
wpi::util::SmallVector< T, N > & Vec() noexcept
Gets a reference to the backing small vector.
Definition ProtobufCallbacks.hpp:365
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.hpp:351
std::span< const T > Items() const noexcept
Gets a const span pointing to the storage buffer.
Definition ProtobufCallbacks.hpp:358
This class is a wrapper around std::array that does compile time size checking.
Definition array.hpp:26
Definition ProtobufCallbacks.hpp:37
Definition ProtobufCallbacks.hpp:40
Definition ProtobufCallbacks.hpp:43
Definition ProtobufCallbacks.hpp:60
Definition ProtobufCallbacks.hpp:64
Definition ProtobufCallbacks.hpp:53
Definition ProtobufCallbacks.hpp:56
Specifies that a type is capable of protobuf serialization and deserialization.
Definition Protobuf.hpp:251
Definition ProtobufCallbacks.hpp:34
Definition ProtobufCallbacks.hpp:46
Definition ProtobufCallbacks.hpp:70
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
constexpr bool ValidateType(pb_type_t type)
Definition ProtobufCallbacks.hpp:73
Definition raw_os_ostream.hpp:19
DecodeLimits
The behavior to use when more elements are in the message then expected when decoding.
Definition ProtobufCallbacks.hpp:24
@ Fail
Definition ProtobufCallbacks.hpp:30
@ Add
Definition ProtobufCallbacks.hpp:28
@ Ignore
Definition ProtobufCallbacks.hpp:26
Definition CvSource.hpp:15
struct pb_ostream_s pb_ostream_t
Definition pb.h:318
uint_least8_t pb_byte_t
Definition pb.h:228
#define PB_LTYPE_STRING
Definition pb.h:256
#define PB_LTYPE_BOOL
Definition pb.h:240
#define PB_LTYPE_FIXED64
Definition pb.h:245
#define PB_LTYPE_SVARINT
Definition pb.h:243
pb_field_iter_t pb_field_t
Definition pb.h:369
#define PB_LTYPE_UVARINT
Definition pb.h:242
#define PB_LTYPE(x)
Definition pb.h:300
#define PB_LTYPE_VARINT
Definition pb.h:241
#define PB_LTYPE_BYTES
Definition pb.h:252
@ PB_WT_STRING
Definition pb.h:433
#define PB_LTYPE_FIXED32
Definition pb.h:244
struct pb_callback_s pb_callback_t
Definition pb.h:413
struct pb_istream_s pb_istream_t
Definition pb.h:317
#define PB_LTYPE_SUBMESSAGE
Definition pb.h:260
pb_byte_t pb_type_t
Definition pb.h:235
bool pb_read(pb_istream_t *stream, pb_byte_t *buf, size_t count)
bool pb_decode_varint(pb_istream_t *stream, uint64_t *dest)
bool pb_decode_fixed32(pb_istream_t *stream, void *dest)
bool pb_decode_bool(pb_istream_t *stream, bool *dest)
bool pb_decode_fixed64(pb_istream_t *stream, void *dest)
bool pb_decode_svarint(pb_istream_t *stream, int64_t *dest)
bool pb_encode_tag_for_field(pb_ostream_t *stream, const pb_field_iter_t *field)
#define PB_OSTREAM_SIZING
Definition pb_encode.h:124
bool pb_encode_varint(pb_ostream_t *stream, uint64_t value)
bool pb_encode_fixed64(pb_ostream_t *stream, const void *value)
bool pb_encode_tag(pb_ostream_t *stream, pb_wire_type_t wiretype, uint32_t field_number)
bool pb_encode_fixed32(pb_ostream_t *stream, const void *value)
bool pb_encode_svarint(pb_ostream_t *stream, int64_t value)
bool pb_encode_string(pb_ostream_t *stream, const pb_byte_t *buffer, size_t size)
pb_type_t type
Definition pb.h:359
pb_size_t tag
Definition pb.h:356
size_t bytes_left
Definition pb_decode.h:49
size_t bytes_written
Definition pb_encode.h:49
A wrapper around a wpi::util::array that lets us treat it as a limited sized vector.
Definition ProtobufCallbacks.hpp:422
size_t m_currentIndex
Definition ProtobufCallbacks.hpp:424
size_t size() const
Definition ProtobufCallbacks.hpp:426
T & emplace_back(ArgTypes &&... Args)
Definition ProtobufCallbacks.hpp:429
wpi::util::array< T, N > m_array
Definition ProtobufCallbacks.hpp:423
bool IsFull() const noexcept
Returns if the buffer is completely filled up.
Definition ProtobufCallbacks.hpp:461
size_t Size() const noexcept
Returns the number of elements in the buffer.
Definition ProtobufCallbacks.hpp:468
WpiArrayUnpackCallback()
Constructs a WpiArrayUnpackCallback.
Definition ProtobufCallbacks.hpp:451
wpi::util::array< T, N > & Array() noexcept
Returns a reference to the backing array.
Definition ProtobufCallbacks.hpp:475