WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
VectorProto.h
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>
12
13#include "frc/EigenCore.h"
15
16template <int Size, int Options, int MaxRows, int MaxCols>
17struct wpi::Protobuf<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>> {
23
24 static std::optional<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>> Unpack(
25 InputStream& stream) {
26 constexpr bool isSmall = Size * sizeof(double) < 256;
27 using UnpackType =
28 std::conditional_t<isSmall, wpi::UnpackCallback<double, Size>,
30 UnpackType rows;
31 rows.Vec().reserve(Size);
32 rows.SetLimits(wpi::DecodeLimits::Fail);
34 .rows = rows.Callback(),
35 };
36 if (!stream.Decode(msg)) {
37 return {};
38 }
39
40 auto items = rows.Items();
41
42 if (items.size() != Size) {
43 return {};
44 }
45
47 for (int i = 0; i < Size; i++) {
48 mat(i) = items[i];
49 }
50
51 return mat;
52 }
53
54 static bool Pack(
55 OutputStream& stream,
57 std::span<const double> rowsSpan{value.data(), static_cast<size_t>(Size)};
58 wpi::PackCallback<double> rows{rowsSpan};
60 .rows = rows.Callback(),
61 };
62 return stream.Encode(msg);
63 }
64};
A callback method that will pack elements when called.
Definition ProtobufCallbacks.h:486
Class for wrapping a nanopb istream.
Definition Protobuf.h:55
bool Decode(typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg, unsigned int flags=0)
Decodes a protobuf.
Definition Protobuf.h:104
Class for wrapping a nanopb ostream.
Definition Protobuf.h:119
bool Encode(const typename Protobuf< std::remove_cvref_t< T > >::MessageStruct &msg)
Decodes a protobuf.
Definition Protobuf.h:211
A DirectUnpackCallback backed by a std::vector.
Definition ProtobufCallbacks.h:381
std::vector< T > & Vec() noexcept
Gets a reference to the backing vector.
Definition ProtobufCallbacks.h:410
Definition CAN.h:11
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
Definition wpimath.npb.h:28
static std::optional< frc::Matrixd< Size, 1, Options, MaxRows, MaxCols > > Unpack(InputStream &stream)
Definition VectorProto.h:24
static bool Pack(OutputStream &stream, const frc::Matrixd< Size, 1, Options, MaxRows, MaxCols > &value)
Definition VectorProto.h:54
Protobuf serialization template.
Definition Protobuf.h:36
struct _wpi_proto_ProtobufVector wpi_proto_ProtobufVector