mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[wpimath] Merge .inc files into headers (#7209)
Splitting the files didn't help readability or save compilation time and it confused contributors. Merging them is also in line with how C++ modules will be written.
This commit is contained in:
@@ -4,17 +4,49 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/proto/MatrixProto.h"
|
||||
#include "frc/system/LinearSystem.h"
|
||||
#include "system.pb.h"
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
struct wpi::Protobuf<frc::LinearSystem<States, Inputs, Outputs>> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::LinearSystem<States, Inputs, Outputs> Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value);
|
||||
};
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufLinearSystem>(arena);
|
||||
}
|
||||
|
||||
#include "frc/system/proto/LinearSystemProto.inc"
|
||||
static frc::LinearSystem<States, Inputs, Outputs> Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m = static_cast<const wpi::proto::ProtobufLinearSystem*>(&msg);
|
||||
if (m->num_states() != States || m->num_inputs() != Inputs ||
|
||||
m->num_outputs() != Outputs) {
|
||||
throw std::invalid_argument(fmt::format(
|
||||
"Tried to unpack message with {} states and {} inputs and {} outputs "
|
||||
"into LinearSystem with {} states and {} inputs and {} outputs",
|
||||
m->num_states(), m->num_inputs(), m->num_outputs(), States, Inputs,
|
||||
Outputs));
|
||||
}
|
||||
return frc::LinearSystem<States, Inputs, Outputs>{
|
||||
wpi::UnpackProtobuf<frc::Matrixd<States, States>>(m->wpi_a()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<States, Inputs>>(m->wpi_b()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<Outputs, States>>(m->wpi_c()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<Outputs, Inputs>>(m->wpi_d())};
|
||||
}
|
||||
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufLinearSystem*>(msg);
|
||||
m->set_num_states(States);
|
||||
m->set_num_inputs(Inputs);
|
||||
m->set_num_outputs(Outputs);
|
||||
wpi::PackProtobuf(m->mutable_a(), value.A());
|
||||
wpi::PackProtobuf(m->mutable_b(), value.B());
|
||||
wpi::PackProtobuf(m->mutable_c(), value.C());
|
||||
wpi::PackProtobuf(m->mutable_d(), value.D());
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/ProtoHelper.h>
|
||||
|
||||
#include "frc/proto/MatrixProto.h"
|
||||
#include "frc/system/proto/LinearSystemProto.h"
|
||||
#include "system.pb.h"
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
google::protobuf::Message*
|
||||
wpi::Protobuf<frc::LinearSystem<States, Inputs, Outputs>>::New(
|
||||
google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufLinearSystem>(arena);
|
||||
}
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
frc::LinearSystem<States, Inputs, Outputs>
|
||||
wpi::Protobuf<frc::LinearSystem<States, Inputs, Outputs>>::Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m = static_cast<const wpi::proto::ProtobufLinearSystem*>(&msg);
|
||||
if (m->num_states() != States || m->num_inputs() != Inputs ||
|
||||
m->num_outputs() != Outputs) {
|
||||
throw std::invalid_argument(fmt::format(
|
||||
"Tried to unpack message with {} states and {} inputs and {} outputs "
|
||||
"into LinearSystem with {} states and {} inputs and {} outputs",
|
||||
m->num_states(), m->num_inputs(), m->num_outputs(), States, Inputs,
|
||||
Outputs));
|
||||
}
|
||||
return frc::LinearSystem<States, Inputs, Outputs>{
|
||||
wpi::UnpackProtobuf<frc::Matrixd<States, States>>(m->wpi_a()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<States, Inputs>>(m->wpi_b()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<Outputs, States>>(m->wpi_c()),
|
||||
wpi::UnpackProtobuf<frc::Matrixd<Outputs, Inputs>>(m->wpi_d())};
|
||||
}
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
void wpi::Protobuf<frc::LinearSystem<States, Inputs, Outputs>>::Pack(
|
||||
google::protobuf::Message* msg,
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufLinearSystem*>(msg);
|
||||
m->set_num_states(States);
|
||||
m->set_num_inputs(Inputs);
|
||||
m->set_num_outputs(Outputs);
|
||||
wpi::PackProtobuf(m->mutable_a(), value.A());
|
||||
wpi::PackProtobuf(m->mutable_b(), value.B());
|
||||
wpi::PackProtobuf(m->mutable_c(), value.C());
|
||||
wpi::PackProtobuf(m->mutable_d(), value.D());
|
||||
}
|
||||
@@ -32,9 +32,36 @@ struct wpi::Struct<frc::LinearSystem<States, Inputs, Outputs>> {
|
||||
static constexpr std::string_view GetSchema() { return kSchema; }
|
||||
|
||||
static frc::LinearSystem<States, Inputs, Outputs> Unpack(
|
||||
std::span<const uint8_t> data);
|
||||
std::span<const uint8_t> data) {
|
||||
constexpr size_t kAOff = 0;
|
||||
constexpr size_t kBOff =
|
||||
kAOff + wpi::GetStructSize<frc::Matrixd<States, States>>();
|
||||
constexpr size_t kCOff =
|
||||
kBOff + wpi::GetStructSize<frc::Matrixd<States, Inputs>>();
|
||||
constexpr size_t kDOff =
|
||||
kCOff + wpi::GetStructSize<frc::Matrixd<Outputs, States>>();
|
||||
return frc::LinearSystem<States, Inputs, Outputs>{
|
||||
wpi::UnpackStruct<frc::Matrixd<States, States>, kAOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<States, Inputs>, kBOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<Outputs, States>, kCOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<Outputs, Inputs>, kDOff>(data)};
|
||||
}
|
||||
|
||||
static void Pack(std::span<uint8_t> data,
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value);
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value) {
|
||||
constexpr size_t kAOff = 0;
|
||||
constexpr size_t kBOff =
|
||||
kAOff + wpi::GetStructSize<frc::Matrixd<States, States>>();
|
||||
constexpr size_t kCOff =
|
||||
kBOff + wpi::GetStructSize<frc::Matrixd<States, Inputs>>();
|
||||
constexpr size_t kDOff =
|
||||
kCOff + wpi::GetStructSize<frc::Matrixd<Outputs, States>>();
|
||||
wpi::PackStruct<kAOff>(data, value.A());
|
||||
wpi::PackStruct<kBOff>(data, value.B());
|
||||
wpi::PackStruct<kCOff>(data, value.C());
|
||||
wpi::PackStruct<kDOff>(data, value.D());
|
||||
}
|
||||
|
||||
static void ForEachNested(
|
||||
std::invocable<std::string_view, std::string_view> auto fn) {
|
||||
wpi::ForEachStructSchema<frc::Matrixd<States, States>>(fn);
|
||||
@@ -48,5 +75,3 @@ static_assert(wpi::StructSerializable<frc::LinearSystem<4, 3, 2>>);
|
||||
static_assert(wpi::HasNestedStruct<frc::LinearSystem<4, 3, 2>>);
|
||||
static_assert(wpi::StructSerializable<frc::LinearSystem<2, 3, 4>>);
|
||||
static_assert(wpi::HasNestedStruct<frc::LinearSystem<2, 3, 4>>);
|
||||
|
||||
#include "frc/system/struct/LinearSystemStruct.inc"
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "frc/system/struct/LinearSystemStruct.h"
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
frc::LinearSystem<States, Inputs, Outputs>
|
||||
wpi::Struct<frc::LinearSystem<States, Inputs, Outputs>>::Unpack(
|
||||
std::span<const uint8_t> data) {
|
||||
constexpr size_t kAOff = 0;
|
||||
constexpr size_t kBOff =
|
||||
kAOff + wpi::GetStructSize<frc::Matrixd<States, States>>();
|
||||
constexpr size_t kCOff =
|
||||
kBOff + wpi::GetStructSize<frc::Matrixd<States, Inputs>>();
|
||||
constexpr size_t kDOff =
|
||||
kCOff + wpi::GetStructSize<frc::Matrixd<Outputs, States>>();
|
||||
return frc::LinearSystem<States, Inputs, Outputs>{
|
||||
wpi::UnpackStruct<frc::Matrixd<States, States>, kAOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<States, Inputs>, kBOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<Outputs, States>, kCOff>(data),
|
||||
wpi::UnpackStruct<frc::Matrixd<Outputs, Inputs>, kDOff>(data)};
|
||||
}
|
||||
|
||||
template <int States, int Inputs, int Outputs>
|
||||
void wpi::Struct<frc::LinearSystem<States, Inputs, Outputs>>::Pack(
|
||||
std::span<uint8_t> data,
|
||||
const frc::LinearSystem<States, Inputs, Outputs>& value) {
|
||||
constexpr size_t kAOff = 0;
|
||||
constexpr size_t kBOff =
|
||||
kAOff + wpi::GetStructSize<frc::Matrixd<States, States>>();
|
||||
constexpr size_t kCOff =
|
||||
kBOff + wpi::GetStructSize<frc::Matrixd<States, Inputs>>();
|
||||
constexpr size_t kDOff =
|
||||
kCOff + wpi::GetStructSize<frc::Matrixd<Outputs, States>>();
|
||||
wpi::PackStruct<kAOff>(data, value.A());
|
||||
wpi::PackStruct<kBOff>(data, value.B());
|
||||
wpi::PackStruct<kCOff>(data, value.C());
|
||||
wpi::PackStruct<kDOff>(data, value.D());
|
||||
}
|
||||
Reference in New Issue
Block a user