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>
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};
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>
350 std::span<T>
Items() noexcept {
return m_storedBuffer; }
357 std::span<const T>
Items() const noexcept {
return m_storedBuffer; }
379template <ProtoCallbackUnpackable T,
size_t N = 1>
396 std::span<T>
Items() noexcept {
return m_storedBuffer; }
403 std::span<const T>
Items() const noexcept {
return m_storedBuffer; }
410 std::vector<T>&
Vec() noexcept {
return m_storedBuffer; }
413 std::vector<T> m_storedBuffer;
420template <ProtoCallbackUnpackable T,
size_t N>
427 template <
typename... ArgTypes>
444template <ProtoCallbackUnpackable T,
size_t N>
460 bool IsFull() const noexcept {
return m_array.m_currentIndex == N; }
467 size_t Size() const noexcept {
return m_array.m_currentIndex; }
485template <ProtoCallbackPackable T>
494 m_callback.
arg =
this;
504 : m_buffer{
std::span<const T>{element, 1}} {
505 m_callback.funcs.encode = CallbackFunc;
506 m_callback.arg =
this;
525 std::span<const T>
Bufs()
const {
return m_buffer; }
528 static auto EncodeStreamTypeFinder() {
535 using EncodeStreamType =
decltype(EncodeStreamTypeFinder());
537 bool EncodeItem(EncodeStreamType& stream,
const pb_field_t* field,
538 const T& value)
const {
539 if constexpr (std::floating_point<T>) {
543 float flt =
static_cast<float>(
value);
547 double dbl =
static_cast<double>(
value);
553 }
else if constexpr (std::integral<T> || ProtoEnumeration<T>) {
573 }
else if constexpr (StringLike<T>) {
574 std::string_view view{
value};
576 reinterpret_cast<const pb_byte_t*
>(view.data()),
578 }
else if constexpr (ConstVectorLike<T>) {
579 std::span<const uint8_t> view{
value};
581 reinterpret_cast<const pb_byte_t*
>(view.data()),
583 }
else if constexpr (ProtobufSerializable<T>) {
589 bool writeTag)
const {
590 if constexpr (ProtobufSerializable<T>) {
591 ProtoOutputStream<T> ostream{stream};
592 for (
auto&& i : m_buffer) {
598 if (!EncodeItem(ostream, field, i)) {
603 for (
auto&& i : m_buffer) {
609 if (!EncodeItem(*stream, field, i)) {
623 if (!EncodeLoop(&substream, field,
false)) {
638 return EncodeLoop(stream, field,
false);
643 if (m_buffer.empty()) {
653 if constexpr (ProtoPackable<T>) {
654 return PackedEncode(stream, field);
656 return EncodeLoop(stream, field,
true);
662 return reinterpret_cast<const PackCallback*
>(*arg)->CallbackFunc(stream,
666 std::span<const T> m_buffer;
This file defines the SmallVector class.
A callback method that will directly unpack elements into the specified vector like data structure.
Definition ProtobufCallbacks.h:112
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.h:141
DirectUnpackCallback(DirectUnpackCallback &&)=delete
DirectUnpackCallback & operator=(const DirectUnpackCallback &)=delete
DirectUnpackCallback(U &storage)
Constructs a callback from a vector like type.
Definition ProtobufCallbacks.h:119
DirectUnpackCallback & operator=(DirectUnpackCallback &&)=delete
DirectUnpackCallback(const DirectUnpackCallback &)=delete
void SetLimits(DecodeLimits limit) noexcept
Set the limits on what happens if more elements exist in the buffer then expected.
Definition ProtobufCallbacks.h:134
A callback method that will pack elements when called.
Definition ProtobufCallbacks.h:486
PackCallback(const T *element)
Constructs a pack callback from a pointer to a single element.
Definition ProtobufCallbacks.h:503
PackCallback(std::span< const T > buffer)
Constructs a pack callback from a span of elements.
Definition ProtobufCallbacks.h:492
PackCallback(const PackCallback &)=delete
PackCallback & operator=(PackCallback &&)=delete
PackCallback(PackCallback &&)=delete
std::span< const T > Bufs() const
Gets a span pointing to the items.
Definition ProtobufCallbacks.h:525
PackCallback & operator=(const PackCallback &)=delete
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.h:518
Class for wrapping a nanopb ostream.
Definition Protobuf.h:119
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
A DirectUnpackCallback backed by a std::vector.
Definition ProtobufCallbacks.h:381
std::span< const T > Items() const noexcept
Gets a const span pointing to the storage buffer.
Definition ProtobufCallbacks.h:403
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.h:410
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.h:396
StdVectorUnpackCallback()
Constructs a StdVectorUnpackCallback.
Definition ProtobufCallbacks.h:386
A DirectUnpackCallback backed by a SmallVector<T, N>.
Definition ProtobufCallbacks.h:335
wpi::SmallVector< T, N > & Vec() noexcept
Gets a reference to the backing small vector.
Definition ProtobufCallbacks.h:364
std::span< const T > Items() const noexcept
Gets a const span pointing to the storage buffer.
Definition ProtobufCallbacks.h:357
UnpackCallback()
Constructs an UnpackCallback.
Definition ProtobufCallbacks.h:340
std::span< T > Items() noexcept
Gets a span pointing to the storage buffer.
Definition ProtobufCallbacks.h:350
This class is a wrapper around std::array that does compile time size checking.
Definition array.h:26
Definition ProtobufCallbacks.h:37
Definition ProtobufCallbacks.h:40
Definition ProtobufCallbacks.h:43
Definition ProtobufCallbacks.h:60
Definition ProtobufCallbacks.h:64
Definition ProtobufCallbacks.h:53
Definition ProtobufCallbacks.h:56
Specifies that a type is capable of protobuf serialization and deserialization.
Definition Protobuf.h:251
Definition ProtobufCallbacks.h:34
Definition ProtobufCallbacks.h:46
Definition ProtobufCallbacks.h:70
detail namespace with internal helper functions
Definition input_adapters.h:32
@ value
the parser finished reading a JSON value
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280
constexpr bool ValidateType(pb_type_t type)
Definition ProtobufCallbacks.h:73
Foonathan namespace.
Definition ntcore_cpp.h:26
DecodeLimits
The behavior to use when more elements are in the message then expected when decoding.
Definition ProtobufCallbacks.h:24
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
#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
#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)
bool(* decode)(pb_istream_t *stream, const pb_field_t *field, void **arg)
Definition pb.h:419
void * arg
Definition pb.h:424
union pb_callback_s::@20 funcs
bool(* encode)(pb_ostream_t *stream, const pb_field_t *field, void *const *arg)
Definition pb.h:420
pb_type_t type
Definition pb.h:359
pb_size_t tag
Definition pb.h:356
Definition pb_decode.h:27
size_t bytes_left
Definition pb_decode.h:49
Definition pb_encode.h:26
size_t bytes_written
Definition pb_encode.h:49
Protobuf serialization template.
Definition Protobuf.h:36
A wrapper around a wpi::array that lets us treat it as a limited sized vector.
Definition ProtobufCallbacks.h:421
T & emplace_back(ArgTypes &&... Args)
Definition ProtobufCallbacks.h:428
wpi::array< T, N > m_array
Definition ProtobufCallbacks.h:422
size_t m_currentIndex
Definition ProtobufCallbacks.h:423
size_t size() const
Definition ProtobufCallbacks.h:425
A DirectUnpackCallback backed by a wpi::array<T, N>.
Definition ProtobufCallbacks.h:446
bool IsFull() const noexcept
Returns if the buffer is completely filled up.
Definition ProtobufCallbacks.h:460
WpiArrayUnpackCallback()
Constructs a WpiArrayUnpackCallback.
Definition ProtobufCallbacks.h:450
size_t Size() const noexcept
Returns the number of elements in the buffer.
Definition ProtobufCallbacks.h:467
wpi::array< T, N > & Array() noexcept
Returns a reference to the backing array.
Definition ProtobufCallbacks.h:474
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:2775