From 639ebb1903749f1e5f08a756a72f0dda4a1bc326 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Fri, 1 Nov 2024 03:47:00 -0400 Subject: [PATCH] shrink header by moving everything to cpp --- wpilibc/src/main/native/cpp/Alert.cpp | 113 ++++++++++++-------- wpilibc/src/main/native/include/frc/Alert.h | 40 +------ 2 files changed, 68 insertions(+), 85 deletions(-) diff --git a/wpilibc/src/main/native/cpp/Alert.cpp b/wpilibc/src/main/native/cpp/Alert.cpp index 79037ae1248..3b1f775b687 100644 --- a/wpilibc/src/main/native/cpp/Alert.cpp +++ b/wpilibc/src/main/native/cpp/Alert.cpp @@ -4,23 +4,87 @@ #include "frc/Alert.h" -#include +#include -#include #include #include #include #include #include +#include #include +#include +#include #include #include "frc/Errors.h" +#include "frc/RobotController.h" #include "frc/smartdashboard/SmartDashboard.h" using namespace frc; +class Alert::PublishedAlert { + public: + PublishedAlert(uint64_t timestamp, std::string_view text) + : timestamp{timestamp}, text{text} {} + uint64_t timestamp; + std::string text; + auto operator<=>(const PublishedAlert&) const = default; +}; + +class Alert::SendableAlerts : public nt::NTSendable, + public wpi::SendableHelper { + public: + SendableAlerts() { m_alerts.fill({}); } + + void InitSendable(nt::NTSendableBuilder& builder) override { + builder.SetSmartDashboardType("Alerts"); + builder.AddStringArrayProperty( + "errors", [this]() { return GetStrings(AlertType::kError); }, nullptr); + builder.AddStringArrayProperty( + "warnings", [this]() { return GetStrings(AlertType::kWarning); }, + nullptr); + builder.AddStringArrayProperty( + "infos", [this]() { return GetStrings(AlertType::kInfo); }, nullptr); + } + + /** + * Returns a reference to the set of active alerts for the given type. + * @param type the type + * @return reference to the set of active alerts for the type + */ + std::set& GetActiveAlertsStorage(AlertType type) { + return const_cast&>( + std::as_const(*this).GetActiveAlertsStorage(type)); + } + + const std::set& GetActiveAlertsStorage(AlertType type) const { + switch (type) { + case AlertType::kInfo: + case AlertType::kWarning: + case AlertType::kError: + return m_alerts[static_cast(type)]; + default: + throw FRC_MakeError(frc::err::InvalidParameter, + "Invalid Alert Type: {}", type); + } + } + + private: + std::vector GetStrings(AlertType type) const { + auto set = GetActiveAlertsStorage(type); + std::vector output; + output.reserve(set.size()); + for (auto& alert : set) { + output.emplace_back(alert.text); + } + return output; + } + + std::array, 3> m_alerts; +}; + // TODO: throw if matching (group, text, type) already constructed Alert::Alert(std::string_view text, AlertType type) : Alert("Alerts", text, type) {} @@ -84,51 +148,6 @@ void Alert::SetText(std::string_view text) { } } -Alert::SendableAlerts::SendableAlerts() { - m_alerts.fill({}); -} - -void Alert::SendableAlerts::InitSendable(nt::NTSendableBuilder& builder) { - builder.SetSmartDashboardType("Alerts"); - builder.AddStringArrayProperty( - "errors", [this]() { return GetStrings(AlertType::kError); }, nullptr); - builder.AddStringArrayProperty( - "warnings", [this]() { return GetStrings(AlertType::kWarning); }, - nullptr); - builder.AddStringArrayProperty( - "infos", [this]() { return GetStrings(AlertType::kInfo); }, nullptr); -} - -std::set& Alert::SendableAlerts::GetActiveAlertsStorage( - AlertType type) { - return const_cast&>( - std::as_const(*this).GetActiveAlertsStorage(type)); -} - -const std::set& -Alert::SendableAlerts::GetActiveAlertsStorage(AlertType type) const { - switch (type) { - case AlertType::kInfo: - case AlertType::kWarning: - case AlertType::kError: - return m_alerts[static_cast(type)]; - default: - throw FRC_MakeError(frc::err::InvalidParameter, "Invalid Alert Type: {}", - type); - } -} - -std::vector Alert::SendableAlerts::GetStrings( - AlertType type) const { - auto set = GetActiveAlertsStorage(type); - std::vector output; - output.reserve(set.size()); - for (auto& alert : set) { - output.emplace_back(alert.text); - } - return output; -} - Alert::SendableAlerts& Alert::GetGroupSendable(std::string_view group) { // Force initialization of SendableRegistry before our magic static to prevent // incorrect destruction order. diff --git a/wpilibc/src/main/native/include/frc/Alert.h b/wpilibc/src/main/native/include/frc/Alert.h index fda9bafdaad..24aa379b987 100644 --- a/wpilibc/src/main/native/include/frc/Alert.h +++ b/wpilibc/src/main/native/include/frc/Alert.h @@ -4,18 +4,8 @@ #pragma once -#include - -#include -#include #include #include -#include - -#include -#include -#include -#include namespace frc { @@ -131,34 +121,8 @@ class Alert { AlertType GetType() const { return m_type; } private: - class PublishedAlert { - public: - PublishedAlert(uint64_t timestamp, std::string_view text) - : timestamp{timestamp}, text{text} {} - uint64_t timestamp; - std::string text; - auto operator<=>(const PublishedAlert&) const = default; - }; - - class SendableAlerts : public nt::NTSendable, - public wpi::SendableHelper { - public: - SendableAlerts(); - void InitSendable(nt::NTSendableBuilder& builder) override; - - /** - * Returns a reference to the set of active alerts for the given type. - * @param type the type - * @return reference to the set of active alerts for the type - */ - std::set& GetActiveAlertsStorage(AlertType type); - const std::set& GetActiveAlertsStorage( - AlertType type) const; - - private: - std::vector GetStrings(AlertType type) const; - std::array, 3> m_alerts; - }; + class PublishedAlert; + class SendableAlerts; AlertType m_type; std::string m_text;