[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

@@ -4,31 +4,55 @@
#include "frc/kinematics/proto/MecanumDriveKinematicsProto.h"
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "kinematics.pb.h"
#include "wpimath/protobuf/kinematics.npb.h"
google::protobuf::Message* wpi::Protobuf<frc::MecanumDriveKinematics>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufMecanumDriveKinematics>(arena);
}
std::optional<frc::MecanumDriveKinematics>
wpi::Protobuf<frc::MecanumDriveKinematics>::Unpack(InputStream& stream) {
wpi::UnpackCallback<frc::Translation2d> frontLeft;
wpi::UnpackCallback<frc::Translation2d> frontRight;
wpi::UnpackCallback<frc::Translation2d> rearLeft;
wpi::UnpackCallback<frc::Translation2d> rearRight;
wpi_proto_ProtobufMecanumDriveKinematics msg{
.front_left = frontLeft.Callback(),
.front_right = frontRight.Callback(),
.rear_left = rearLeft.Callback(),
.rear_right = rearRight.Callback(),
};
if (!stream.Decode(msg)) {
return {};
}
auto ifrontLeft = frontLeft.Items();
auto ifrontRight = frontRight.Items();
auto irearLeft = rearLeft.Items();
auto irearRight = rearRight.Items();
if (ifrontLeft.empty() || ifrontRight.empty() || irearLeft.empty() ||
irearRight.empty()) {
return {};
}
frc::MecanumDriveKinematics wpi::Protobuf<frc::MecanumDriveKinematics>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufMecanumDriveKinematics*>(&msg);
return frc::MecanumDriveKinematics{
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_front_left()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_front_right()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_rear_left()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_rear_right()),
ifrontLeft[0],
ifrontRight[0],
irearLeft[0],
irearRight[0],
};
}
void wpi::Protobuf<frc::MecanumDriveKinematics>::Pack(
google::protobuf::Message* msg, const frc::MecanumDriveKinematics& value) {
auto m = static_cast<wpi::proto::ProtobufMecanumDriveKinematics*>(msg);
wpi::PackProtobuf(m->mutable_front_left(), value.GetFrontLeft());
wpi::PackProtobuf(m->mutable_front_right(), value.GetFrontRight());
wpi::PackProtobuf(m->mutable_rear_left(), value.GetRearLeft());
wpi::PackProtobuf(m->mutable_rear_right(), value.GetRearRight());
bool wpi::Protobuf<frc::MecanumDriveKinematics>::Pack(
OutputStream& stream, const frc::MecanumDriveKinematics& value) {
wpi::PackCallback frontLeft{&value.GetFrontLeft()};
wpi::PackCallback frontRight{&value.GetFrontRight()};
wpi::PackCallback rearLeft{&value.GetRearLeft()};
wpi::PackCallback rearRight{&value.GetRearRight()};
wpi_proto_ProtobufMecanumDriveKinematics msg{
.front_left = frontLeft.Callback(),
.front_right = frontRight.Callback(),
.rear_left = rearLeft.Callback(),
.rear_right = rearRight.Callback(),
};
return stream.Encode(msg);
}