mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[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:
@@ -6,31 +6,28 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <wpi/protobuf/ProtobufCallbacks.h>
|
||||
|
||||
#include "trajectory.pb.h"
|
||||
#include "wpimath/protobuf/trajectory.npb.h"
|
||||
|
||||
google::protobuf::Message* wpi::Protobuf<frc::Trajectory>::New(
|
||||
google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufTrajectory>(arena);
|
||||
}
|
||||
|
||||
frc::Trajectory wpi::Protobuf<frc::Trajectory>::Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m = static_cast<const wpi::proto::ProtobufTrajectory*>(&msg);
|
||||
std::vector<frc::Trajectory::State> states;
|
||||
states.reserve(m->states().size());
|
||||
for (const auto& protoState : m->states()) {
|
||||
states.push_back(wpi::UnpackProtobuf<frc::Trajectory::State>(protoState));
|
||||
std::optional<frc::Trajectory> wpi::Protobuf<frc::Trajectory>::Unpack(
|
||||
InputStream& stream) {
|
||||
wpi::StdVectorUnpackCallback<frc::Trajectory::State, SIZE_MAX> states;
|
||||
wpi_proto_ProtobufTrajectory msg{
|
||||
.states = states.Callback(),
|
||||
};
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
return frc::Trajectory{states};
|
||||
|
||||
return frc::Trajectory{states.Vec()};
|
||||
}
|
||||
|
||||
void wpi::Protobuf<frc::Trajectory>::Pack(google::protobuf::Message* msg,
|
||||
bool wpi::Protobuf<frc::Trajectory>::Pack(OutputStream& stream,
|
||||
const frc::Trajectory& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufTrajectory*>(msg);
|
||||
m->mutable_states()->Reserve(value.States().size());
|
||||
for (const auto& state : value.States()) {
|
||||
wpi::PackProtobuf(m->add_states(), state);
|
||||
}
|
||||
wpi::PackCallback<frc::Trajectory::State> states{value.States()};
|
||||
wpi_proto_ProtobufTrajectory msg{
|
||||
.states = states.Callback(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
|
||||
@@ -4,33 +4,46 @@
|
||||
|
||||
#include "frc/trajectory/proto/TrajectoryStateProto.h"
|
||||
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <utility>
|
||||
|
||||
#include "trajectory.pb.h"
|
||||
#include <wpi/protobuf/ProtobufCallbacks.h>
|
||||
|
||||
google::protobuf::Message* wpi::Protobuf<frc::Trajectory::State>::New(
|
||||
google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufTrajectoryState>(arena);
|
||||
}
|
||||
#include "wpimath/protobuf/trajectory.npb.h"
|
||||
|
||||
std::optional<frc::Trajectory::State>
|
||||
wpi::Protobuf<frc::Trajectory::State>::Unpack(InputStream& stream) {
|
||||
wpi::UnpackCallback<frc::Pose2d> pose;
|
||||
wpi_proto_ProtobufTrajectoryState msg;
|
||||
msg.pose = pose.Callback();
|
||||
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto ipose = pose.Items();
|
||||
|
||||
if (ipose.empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
frc::Trajectory::State wpi::Protobuf<frc::Trajectory::State>::Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m = static_cast<const wpi::proto::ProtobufTrajectoryState*>(&msg);
|
||||
return frc::Trajectory::State{
|
||||
units::second_t{m->time()},
|
||||
units::meters_per_second_t{m->velocity()},
|
||||
units::meters_per_second_squared_t{m->acceleration()},
|
||||
wpi::UnpackProtobuf<frc::Pose2d>(m->wpi_pose()),
|
||||
units::curvature_t{m->curvature()},
|
||||
units::second_t{msg.time},
|
||||
units::meters_per_second_t{msg.velocity},
|
||||
units::meters_per_second_squared_t{msg.acceleration},
|
||||
std::move(ipose[0]),
|
||||
units::curvature_t{msg.curvature},
|
||||
};
|
||||
}
|
||||
|
||||
void wpi::Protobuf<frc::Trajectory::State>::Pack(
|
||||
google::protobuf::Message* msg, const frc::Trajectory::State& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufTrajectoryState*>(msg);
|
||||
m->set_time(value.t.value());
|
||||
m->set_velocity(value.velocity.value());
|
||||
m->set_acceleration(value.acceleration.value());
|
||||
wpi::PackProtobuf(m->mutable_pose(), value.pose);
|
||||
m->set_curvature(value.curvature.value());
|
||||
bool wpi::Protobuf<frc::Trajectory::State>::Pack(
|
||||
OutputStream& stream, const frc::Trajectory::State& value) {
|
||||
wpi::PackCallback pose{&value.pose};
|
||||
wpi_proto_ProtobufTrajectoryState msg{
|
||||
.time = value.t.value(),
|
||||
.velocity = value.velocity.value(),
|
||||
.acceleration = value.acceleration.value(),
|
||||
.pose = pose.Callback(),
|
||||
.curvature = value.curvature.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user