mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
[wpiutil] Change Struct to allow non-constexpr implementation (#5992)
This required changing the constant values (e.g. kSize) into functions (e.g. GetSize()). Fixed implementations of ForEachNested to be inline (as these are actually templates). Also added a ntcore Struct test.
This commit is contained in:
@@ -12,7 +12,7 @@ constexpr size_t kOmegaOff = kVyOff + 8;
|
||||
|
||||
using StructType = wpi::Struct<frc::ChassisSpeeds>;
|
||||
|
||||
frc::ChassisSpeeds StructType::Unpack(std::span<const uint8_t, kSize> data) {
|
||||
frc::ChassisSpeeds StructType::Unpack(std::span<const uint8_t> data) {
|
||||
return frc::ChassisSpeeds{
|
||||
units::meters_per_second_t{wpi::UnpackStruct<double, kVxOff>(data)},
|
||||
units::meters_per_second_t{wpi::UnpackStruct<double, kVyOff>(data)},
|
||||
@@ -20,7 +20,7 @@ frc::ChassisSpeeds StructType::Unpack(std::span<const uint8_t, kSize> data) {
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::ChassisSpeeds& value) {
|
||||
wpi::PackStruct<kVxOff>(data, value.vx.value());
|
||||
wpi::PackStruct<kVyOff>(data, value.vy.value());
|
||||
|
||||
@@ -11,13 +11,13 @@ constexpr size_t kTrackWidthOff = 0;
|
||||
using StructType = wpi::Struct<frc::DifferentialDriveKinematics>;
|
||||
|
||||
frc::DifferentialDriveKinematics StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
std::span<const uint8_t> data) {
|
||||
return frc::DifferentialDriveKinematics{
|
||||
units::meter_t{wpi::UnpackStruct<double, kTrackWidthOff>(data)},
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::DifferentialDriveKinematics& value) {
|
||||
wpi::PackStruct<kTrackWidthOff>(data, value.trackWidth.value());
|
||||
}
|
||||
|
||||
@@ -12,14 +12,14 @@ constexpr size_t kRightOff = kLeftOff + 8;
|
||||
using StructType = wpi::Struct<frc::DifferentialDriveWheelSpeeds>;
|
||||
|
||||
frc::DifferentialDriveWheelSpeeds StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
std::span<const uint8_t> data) {
|
||||
return frc::DifferentialDriveWheelSpeeds{
|
||||
units::meters_per_second_t{wpi::UnpackStruct<double, kLeftOff>(data)},
|
||||
units::meters_per_second_t{wpi::UnpackStruct<double, kRightOff>(data)},
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::DifferentialDriveWheelSpeeds& value) {
|
||||
wpi::PackStruct<kLeftOff>(data, value.left.value());
|
||||
wpi::PackStruct<kRightOff>(data, value.right.value());
|
||||
|
||||
@@ -7,17 +7,16 @@
|
||||
namespace {
|
||||
constexpr size_t kFrontLeftOff = 0;
|
||||
constexpr size_t kFrontRightOff =
|
||||
kFrontLeftOff + wpi::Struct<frc::Translation2d>::kSize;
|
||||
kFrontLeftOff + wpi::GetStructSize<frc::Translation2d>();
|
||||
constexpr size_t kRearLeftOff =
|
||||
kFrontRightOff + wpi::Struct<frc::Translation2d>::kSize;
|
||||
kFrontRightOff + wpi::GetStructSize<frc::Translation2d>();
|
||||
constexpr size_t kRearRightOff =
|
||||
kRearLeftOff + wpi::Struct<frc::Translation2d>::kSize;
|
||||
kRearLeftOff + wpi::GetStructSize<frc::Translation2d>();
|
||||
} // namespace
|
||||
|
||||
using StructType = wpi::Struct<frc::MecanumDriveKinematics>;
|
||||
|
||||
frc::MecanumDriveKinematics StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
frc::MecanumDriveKinematics StructType::Unpack(std::span<const uint8_t> data) {
|
||||
return frc::MecanumDriveKinematics{
|
||||
wpi::UnpackStruct<frc::Translation2d, kFrontLeftOff>(data),
|
||||
wpi::UnpackStruct<frc::Translation2d, kFrontRightOff>(data),
|
||||
@@ -26,15 +25,10 @@ frc::MecanumDriveKinematics StructType::Unpack(
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::MecanumDriveKinematics& value) {
|
||||
wpi::PackStruct<kFrontLeftOff>(data, value.GetFrontLeftWheel());
|
||||
wpi::PackStruct<kFrontRightOff>(data, value.GetFrontRightWheel());
|
||||
wpi::PackStruct<kRearLeftOff>(data, value.GetRearLeftWheel());
|
||||
wpi::PackStruct<kRearRightOff>(data, value.GetRearRightWheel());
|
||||
}
|
||||
|
||||
void StructType::ForEachNested(
|
||||
std::invocable<std::string_view, std::string_view> auto fn) {
|
||||
wpi::ForEachStructSchema<frc::Translation2d>(fn);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ constexpr size_t kRearRightOff = kRearLeftOff + 8;
|
||||
using StructType = wpi::Struct<frc::MecanumDriveWheelPositions>;
|
||||
|
||||
frc::MecanumDriveWheelPositions StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
std::span<const uint8_t> data) {
|
||||
return frc::MecanumDriveWheelPositions{
|
||||
units::meter_t{wpi::UnpackStruct<double, kFrontLeftOff>(data)},
|
||||
units::meter_t{wpi::UnpackStruct<double, kFrontRightOff>(data)},
|
||||
@@ -23,7 +23,7 @@ frc::MecanumDriveWheelPositions StructType::Unpack(
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::MecanumDriveWheelPositions& value) {
|
||||
wpi::PackStruct<kFrontLeftOff>(data, value.frontLeft.value());
|
||||
wpi::PackStruct<kFrontRightOff>(data, value.frontRight.value());
|
||||
|
||||
@@ -13,8 +13,7 @@ constexpr size_t kRearRightOff = kRearLeftOff + 8;
|
||||
|
||||
using StructType = wpi::Struct<frc::MecanumDriveWheelSpeeds>;
|
||||
|
||||
frc::MecanumDriveWheelSpeeds StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
frc::MecanumDriveWheelSpeeds StructType::Unpack(std::span<const uint8_t> data) {
|
||||
return frc::MecanumDriveWheelSpeeds{
|
||||
units::meters_per_second_t{
|
||||
wpi::UnpackStruct<double, kFrontLeftOff>(data)},
|
||||
@@ -26,7 +25,7 @@ frc::MecanumDriveWheelSpeeds StructType::Unpack(
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::MecanumDriveWheelSpeeds& value) {
|
||||
wpi::PackStruct<kFrontLeftOff>(data, value.frontLeft.value());
|
||||
wpi::PackStruct<kFrontRightOff>(data, value.frontRight.value());
|
||||
|
||||
@@ -11,21 +11,15 @@ constexpr size_t kAngleOff = kDistanceOff + 8;
|
||||
|
||||
using StructType = wpi::Struct<frc::SwerveModulePosition>;
|
||||
|
||||
frc::SwerveModulePosition StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
frc::SwerveModulePosition StructType::Unpack(std::span<const uint8_t> data) {
|
||||
return frc::SwerveModulePosition{
|
||||
units::meter_t{wpi::UnpackStruct<double, kDistanceOff>(data)},
|
||||
wpi::UnpackStruct<frc::Rotation2d, kAngleOff>(data),
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::SwerveModulePosition& value) {
|
||||
wpi::PackStruct<kDistanceOff>(data, value.distance.value());
|
||||
wpi::PackStruct<kAngleOff>(data, value.angle);
|
||||
}
|
||||
|
||||
void StructType::ForEachNested(
|
||||
std::invocable<std::string_view, std::string_view> auto fn) {
|
||||
wpi::ForEachStructSchema<frc::Rotation2d>(fn);
|
||||
}
|
||||
|
||||
@@ -11,21 +11,15 @@ constexpr size_t kAngleOff = kSpeedOff + 8;
|
||||
|
||||
using StructType = wpi::Struct<frc::SwerveModuleState>;
|
||||
|
||||
frc::SwerveModuleState StructType::Unpack(
|
||||
std::span<const uint8_t, kSize> data) {
|
||||
frc::SwerveModuleState StructType::Unpack(std::span<const uint8_t> data) {
|
||||
return frc::SwerveModuleState{
|
||||
units::meters_per_second_t{wpi::UnpackStruct<double, kSpeedOff>(data)},
|
||||
wpi::UnpackStruct<frc::Rotation2d, kAngleOff>(data),
|
||||
};
|
||||
}
|
||||
|
||||
void StructType::Pack(std::span<uint8_t, kSize> data,
|
||||
void StructType::Pack(std::span<uint8_t> data,
|
||||
const frc::SwerveModuleState& value) {
|
||||
wpi::PackStruct<kSpeedOff>(data, value.speed.value());
|
||||
wpi::PackStruct<kAngleOff>(data, value.angle);
|
||||
}
|
||||
|
||||
void StructType::ForEachNested(
|
||||
std::invocable<std::string_view, std::string_view> auto fn) {
|
||||
wpi::ForEachStructSchema<frc::Rotation2d>(fn);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user