WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
VectorProto.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdexcept>
8
9#include <fmt/format.h>
10
15
16template <int Size, int Options, int MaxRows, int MaxCols>
18 wpi::math::Matrixd<Size, 1, Options, MaxRows, MaxCols>> {
24
25 static std::optional<wpi::math::Matrixd<Size, 1, Options, MaxRows, MaxCols>>
27 constexpr bool isSmall = Size * sizeof(double) < 256;
28 using UnpackType =
29 std::conditional_t<isSmall, wpi::util::UnpackCallback<double, Size>,
31 UnpackType rows;
32 rows.Vec().reserve(Size);
33 rows.SetLimits(wpi::util::DecodeLimits::Fail);
35 .rows = rows.Callback(),
36 };
37 if (!stream.Decode(msg)) {
38 return {};
39 }
40
41 auto items = rows.Items();
42
43 if (items.size() != Size) {
44 return {};
45 }
46
48 for (int i = 0; i < Size; i++) {
49 mat(i) = items[i];
50 }
51
52 return mat;
53 }
54
55 static bool Pack(
56 OutputStream& stream,
58 std::span<const double> rowsSpan{value.data(), static_cast<size_t>(Size)};
61 .rows = rows.Callback(),
62 };
63 return stream.Encode(msg);
64 }
65};
A callback method that will pack elements when called.
Definition ProtobufCallbacks.hpp:487
pb_callback_t Callback() const
Gets the nanopb callback pointing to this object.
Definition ProtobufCallbacks.hpp:519
Class for wrapping a nanopb istream.
Definition Protobuf.hpp:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.hpp:104
Class for wrapping a nanopb ostream.
Definition Protobuf.hpp:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.hpp:211
A DirectUnpackCallback backed by a std::vector.
Definition ProtobufCallbacks.hpp:382
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.hpp:411
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.hpp:21
@ Fail
Definition ProtobufCallbacks.hpp:30
Definition CvSource.hpp:15
wpi_proto_ProtobufVector MessageStruct
Definition VectorProto.hpp:19
wpi::util::ProtoOutputStream< wpi::math::Matrixd< Size, 1, Options, MaxRows, MaxCols > > OutputStream
Definition VectorProto.hpp:22
wpi::util::ProtoInputStream< wpi::math::Matrixd< Size, 1, Options, MaxRows, MaxCols > > InputStream
Definition VectorProto.hpp:20
static bool Pack(OutputStream &stream, const wpi::math::Matrixd< Size, 1, Options, MaxRows, MaxCols > &value)
Definition VectorProto.hpp:55
static std::optional< wpi::math::Matrixd< Size, 1, Options, MaxRows, MaxCols > > Unpack(InputStream &stream)
Definition VectorProto.hpp:26
Protobuf serialization template.
Definition Protobuf.hpp:36
struct _wpi_proto_ProtobufVector wpi_proto_ProtobufVector