[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:
Peter Johnson
2023-12-02 23:36:44 -08:00
committed by GitHub
parent ca272de400
commit a583ca01e1
62 changed files with 812 additions and 450 deletions

View File

@@ -11,12 +11,14 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::ChassisSpeeds> {
static constexpr std::string_view kTypeString = "struct:ChassisSpeeds";
static constexpr size_t kSize = 24;
static constexpr std::string_view kSchema =
"double vx;double vy;double omega";
static constexpr std::string_view GetTypeString() {
return "struct:ChassisSpeeds";
}
static constexpr size_t GetSize() { return 24; }
static constexpr std::string_view GetSchema() {
return "double vx;double vy;double omega";
}
static frc::ChassisSpeeds Unpack(std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
const frc::ChassisSpeeds& value);
static frc::ChassisSpeeds Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data, const frc::ChassisSpeeds& value);
};

View File

@@ -11,13 +11,13 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::DifferentialDriveKinematics> {
static constexpr std::string_view kTypeString =
"struct:DifferentialDriveKinematics";
static constexpr size_t kSize = 8;
static constexpr std::string_view kSchema = "double track_width";
static constexpr std::string_view GetTypeString() {
return "struct:DifferentialDriveKinematics";
}
static constexpr size_t GetSize() { return 8; }
static constexpr std::string_view GetSchema() { return "double track_width"; }
static frc::DifferentialDriveKinematics Unpack(
std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::DifferentialDriveKinematics Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::DifferentialDriveKinematics& value);
};

View File

@@ -11,13 +11,16 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::DifferentialDriveWheelSpeeds> {
static constexpr std::string_view kTypeString =
"struct:DifferentialDriveWheelSpeeds";
static constexpr size_t kSize = 16;
static constexpr std::string_view kSchema = "double left;double right";
static constexpr std::string_view GetTypeString() {
return "struct:DifferentialDriveWheelSpeeds";
}
static constexpr size_t GetSize() { return 16; }
static constexpr std::string_view GetSchema() {
return "double left;double right";
}
static frc::DifferentialDriveWheelSpeeds Unpack(
std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::DifferentialDriveWheelSpeeds& value);
};

View File

@@ -11,19 +11,24 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::MecanumDriveKinematics> {
static constexpr std::string_view kTypeString =
"struct:MecanumDriveKinematics";
static constexpr size_t kSize = 4 * wpi::Struct<frc::Translation2d>::kSize;
static constexpr std::string_view kSchema =
"Translation2d front_left;Translation2d front_right;Translation2d "
"rear_left;Translation2d rear_right";
static constexpr std::string_view GetTypeString() {
return "struct:MecanumDriveKinematics";
}
static constexpr size_t GetSize() {
return 4 * wpi::GetStructSize<frc::Translation2d>();
}
static constexpr std::string_view GetSchema() {
return "Translation2d front_left;Translation2d front_right;Translation2d "
"rear_left;Translation2d rear_right";
}
static frc::MecanumDriveKinematics Unpack(
std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::MecanumDriveKinematics Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::MecanumDriveKinematics& value);
static void ForEachNested(
std::invocable<std::string_view, std::string_view> auto fn);
std::invocable<std::string_view, std::string_view> auto fn) {
wpi::ForEachStructSchema<frc::Translation2d>(fn);
}
};
static_assert(wpi::HasNestedStruct<frc::MecanumDriveKinematics>);

View File

@@ -11,14 +11,16 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::MecanumDriveWheelPositions> {
static constexpr std::string_view kTypeString =
"struct:MecanumDriveWheelPositions";
static constexpr size_t kSize = 32;
static constexpr std::string_view kSchema =
"double front_left;double front_right;double rear_left;double rear_right";
static constexpr std::string_view GetTypeString() {
return "struct:MecanumDriveWheelPositions";
}
static constexpr size_t GetSize() { return 32; }
static constexpr std::string_view GetSchema() {
return "double front_left;double front_right;double rear_left;double "
"rear_right";
}
static frc::MecanumDriveWheelPositions Unpack(
std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::MecanumDriveWheelPositions Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::MecanumDriveWheelPositions& value);
};

View File

@@ -11,14 +11,16 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::MecanumDriveWheelSpeeds> {
static constexpr std::string_view kTypeString =
"struct:MecanumDriveWheelSpeeds";
static constexpr size_t kSize = 32;
static constexpr std::string_view kSchema =
"double front_left;double front_right;double rear_left;double rear_right";
static constexpr std::string_view GetTypeString() {
return "struct:MecanumDriveWheelSpeeds";
}
static constexpr size_t GetSize() { return 32; }
static constexpr std::string_view GetSchema() {
return "double front_left;double front_right;double rear_left;"
"double rear_right";
}
static frc::MecanumDriveWheelSpeeds Unpack(
std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::MecanumDriveWheelSpeeds Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::MecanumDriveWheelSpeeds& value);
};

View File

@@ -11,16 +11,23 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::SwerveModulePosition> {
static constexpr std::string_view kTypeString = "struct:SwerveModulePosition";
static constexpr size_t kSize = 8 + wpi::Struct<frc::Rotation2d>::kSize;
static constexpr std::string_view kSchema =
"double distance;Rotation2d angle";
static constexpr std::string_view GetTypeString() {
return "struct:SwerveModulePosition";
}
static constexpr size_t GetSize() {
return 8 + wpi::GetStructSize<frc::Rotation2d>();
}
static constexpr std::string_view GetSchema() {
return "double distance;Rotation2d angle";
}
static frc::SwerveModulePosition Unpack(std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::SwerveModulePosition Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::SwerveModulePosition& value);
static void ForEachNested(
std::invocable<std::string_view, std::string_view> auto fn);
std::invocable<std::string_view, std::string_view> auto fn) {
wpi::ForEachStructSchema<frc::Rotation2d>(fn);
}
};
static_assert(wpi::HasNestedStruct<frc::SwerveModulePosition>);

View File

@@ -11,15 +11,23 @@
template <>
struct WPILIB_DLLEXPORT wpi::Struct<frc::SwerveModuleState> {
static constexpr std::string_view kTypeString = "struct:SwerveModuleState";
static constexpr size_t kSize = 8 + wpi::Struct<frc::Rotation2d>::kSize;
static constexpr std::string_view kSchema = "double speed;Rotation2d angle";
static constexpr std::string_view GetTypeString() {
return "struct:SwerveModuleState";
}
static constexpr size_t GetSize() {
return 8 + wpi::GetStructSize<frc::Rotation2d>();
}
static constexpr std::string_view GetSchema() {
return "double speed;Rotation2d angle";
}
static frc::SwerveModuleState Unpack(std::span<const uint8_t, kSize> data);
static void Pack(std::span<uint8_t, kSize> data,
static frc::SwerveModuleState Unpack(std::span<const uint8_t> data);
static void Pack(std::span<uint8_t> data,
const frc::SwerveModuleState& value);
static void ForEachNested(
std::invocable<std::string_view, std::string_view> auto fn);
std::invocable<std::string_view, std::string_view> auto fn) {
wpi::ForEachStructSchema<frc::Rotation2d>(fn);
}
};
static_assert(wpi::HasNestedStruct<frc::SwerveModuleState>);