[wpiutil] Change C++ protobuf to nanopb (#7309)

The Google C++ protobuf implementation has issues with dynamic linkage across DLL boundaries because it uses global variables.  It also has a compile-time dependency because the protoc version must exactly match the libprotobuf version.  Using nanopb with a customized generator fixes both of these issues.

Co-authored-by: Gold856 <117957790+Gold856@users.noreply.github.com>
This commit is contained in:
Thad House
2024-11-07 22:42:50 -08:00
committed by GitHub
parent fd2e0c0427
commit 8b8b634f65
166 changed files with 17522 additions and 1571 deletions

View File

@@ -7,50 +7,65 @@
#include <stdexcept>
#include <fmt/format.h>
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/Protobuf.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "frc/EigenCore.h"
#include "wpimath.pb.h"
#include "wpimath/protobuf/wpimath.npb.h"
template <int Rows, int Cols, int Options, int MaxRows, int MaxCols>
requires(Cols != 1)
struct wpi::Protobuf<frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>> {
static google::protobuf::Message* New(google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufMatrix>(arena);
}
using MessageStruct = wpi_proto_ProtobufMatrix;
using InputStream = wpi::ProtoInputStream<
frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>>;
using OutputStream = wpi::ProtoOutputStream<
frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>>;
static frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols> Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufMatrix*>(&msg);
if (m->num_rows() != Rows || m->num_cols() != Cols) {
throw std::invalid_argument(fmt::format(
"Tried to unpack message with {} rows and {} columns into "
"Matrix with {} rows and {} columns",
m->num_rows(), m->num_cols(), Rows, Cols));
static std::optional<frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>>
Unpack(InputStream& stream) {
constexpr bool isSmall = Rows * Cols * sizeof(double) < 256;
using UnpackType =
std::conditional_t<isSmall, wpi::UnpackCallback<double, Rows * Cols>,
wpi::StdVectorUnpackCallback<double, Rows * Cols>>;
UnpackType data;
data.Vec().reserve(Rows * Cols);
data.SetLimits(wpi::DecodeLimits::Fail);
wpi_proto_ProtobufMatrix msg;
msg.data = data.Callback();
if (!stream.Decode(msg)) {
return {};
}
if (m->data_size() != Rows * Cols) {
throw std::invalid_argument(
fmt::format("Tried to unpack message with {} elements in data into "
"Matrix with {} elements",
m->data_size(), Rows * Cols));
if (msg.num_rows != Rows || msg.num_cols != Cols) {
return {};
}
auto items = data.Items();
if (items.size() != Rows * Cols) {
return {};
}
frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols> mat;
for (int i = 0; i < Rows * Cols; i++) {
mat(i) = m->data(i);
mat(i) = items[i];
}
return mat;
}
static void Pack(
google::protobuf::Message* msg,
static bool Pack(
OutputStream& stream,
const frc::Matrixd<Rows, Cols, Options, MaxRows, MaxCols>& value) {
auto m = static_cast<wpi::proto::ProtobufMatrix*>(msg);
m->set_num_rows(Rows);
m->set_num_cols(Cols);
m->clear_data();
for (int i = 0; i < Rows * Cols; i++) {
m->add_data(value(i));
}
std::span<const double> dataSpan{value.data(),
static_cast<size_t>(Rows * Cols)};
wpi::PackCallback<double> data{dataSpan};
wpi_proto_ProtobufMatrix msg{
.num_rows = Rows,
.num_cols = Cols,
.data = data.Callback(),
};
return stream.Encode(msg);
}
};

View File

@@ -7,41 +7,58 @@
#include <stdexcept>
#include <fmt/format.h>
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/Protobuf.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "frc/EigenCore.h"
#include "wpimath.pb.h"
#include "wpimath/protobuf/wpimath.npb.h"
template <int Size, int Options, int MaxRows, int MaxCols>
struct wpi::Protobuf<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>> {
static google::protobuf::Message* New(google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufVector>(arena);
}
using MessageStruct = wpi_proto_ProtobufVector;
using InputStream =
wpi::ProtoInputStream<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>>;
using OutputStream =
wpi::ProtoOutputStream<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>>;
static frc::Matrixd<Size, 1, Options, MaxRows, MaxCols> Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufVector*>(&msg);
if (m->rows_size() != Size) {
throw std::invalid_argument(
fmt::format("Tried to unpack message with {} elements in rows into "
"Vector with {} rows",
m->rows_size(), Size));
static std::optional<frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>> Unpack(
InputStream& stream) {
constexpr bool isSmall = Size * sizeof(double) < 256;
using UnpackType =
std::conditional_t<isSmall, wpi::UnpackCallback<double, Size>,
wpi::StdVectorUnpackCallback<double, Size>>;
UnpackType rows;
rows.Vec().reserve(Size);
rows.SetLimits(wpi::DecodeLimits::Fail);
wpi_proto_ProtobufVector msg{
.rows = rows.Callback(),
};
if (!stream.Decode(msg)) {
return {};
}
frc::Matrixd<Size, 1, Options, MaxRows, MaxCols> vec;
auto items = rows.Items();
if (items.size() != Size) {
return {};
}
frc::Matrixd<Size, 1, Options, MaxRows, MaxCols> mat;
for (int i = 0; i < Size; i++) {
vec(i) = m->rows(i);
mat(i) = items[i];
}
return vec;
return mat;
}
static void Pack(
google::protobuf::Message* msg,
static bool Pack(
OutputStream& stream,
const frc::Matrixd<Size, 1, Options, MaxRows, MaxCols>& value) {
auto m = static_cast<wpi::proto::ProtobufVector*>(msg);
m->clear_rows();
for (int i = 0; i < Size; i++) {
m->add_rows(value(i));
}
std::span<const double> rowsSpan{value.data(), static_cast<size_t>(Size)};
wpi::PackCallback<double> rows{rowsSpan};
wpi_proto_ProtobufVector msg{
.rows = rows.Callback(),
};
return stream.Encode(msg);
}
};