Skip to content

Commit

Permalink
shrink header by moving everything to cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Nov 1, 2024
1 parent 0375edc commit 639ebb1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 85 deletions.
113 changes: 66 additions & 47 deletions wpilibc/src/main/native/cpp/Alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,87 @@

#include "frc/Alert.h"

#include <frc/RobotController.h>
#include <stdint.h>

#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include <fmt/format.h>
#include <networktables/NTSendable.h>
#include <networktables/NTSendableBuilder.h>
#include <wpi/StringMap.h>
#include <wpi/sendable/SendableHelper.h>
#include <wpi/sendable/SendableRegistry.h>

#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<SendableAlerts> {
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<PublishedAlert>& GetActiveAlertsStorage(AlertType type) {
return const_cast<std::set<Alert::PublishedAlert>&>(
std::as_const(*this).GetActiveAlertsStorage(type));
}

const std::set<PublishedAlert>& GetActiveAlertsStorage(AlertType type) const {
switch (type) {
case AlertType::kInfo:
case AlertType::kWarning:
case AlertType::kError:
return m_alerts[static_cast<int32_t>(type)];
default:
throw FRC_MakeError(frc::err::InvalidParameter,
"Invalid Alert Type: {}", type);
}
}

private:
std::vector<std::string> GetStrings(AlertType type) const {
auto set = GetActiveAlertsStorage(type);
std::vector<std::string> output;
output.reserve(set.size());
for (auto& alert : set) {
output.emplace_back(alert.text);
}
return output;
}

std::array<std::set<PublishedAlert>, 3> m_alerts;
};

// TODO: throw if matching (group, text, type) already constructed
Alert::Alert(std::string_view text, AlertType type)
: Alert("Alerts", text, type) {}
Expand Down Expand Up @@ -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::PublishedAlert>& Alert::SendableAlerts::GetActiveAlertsStorage(
AlertType type) {
return const_cast<std::set<Alert::PublishedAlert>&>(
std::as_const(*this).GetActiveAlertsStorage(type));
}

const std::set<Alert::PublishedAlert>&
Alert::SendableAlerts::GetActiveAlertsStorage(AlertType type) const {
switch (type) {
case AlertType::kInfo:
case AlertType::kWarning:
case AlertType::kError:
return m_alerts[static_cast<int32_t>(type)];
default:
throw FRC_MakeError(frc::err::InvalidParameter, "Invalid Alert Type: {}",
type);
}
}

std::vector<std::string> Alert::SendableAlerts::GetStrings(
AlertType type) const {
auto set = GetActiveAlertsStorage(type);
std::vector<std::string> 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.
Expand Down
40 changes: 2 additions & 38 deletions wpilibc/src/main/native/include/frc/Alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@

#pragma once

#include <stdint.h>

#include <array>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include <networktables/NTSendable.h>
#include <wpi/SmallVector.h>
#include <wpi/StringMap.h>
#include <wpi/sendable/SendableHelper.h>

namespace frc {

Expand Down Expand Up @@ -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<SendableAlerts> {
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<PublishedAlert>& GetActiveAlertsStorage(AlertType type);
const std::set<PublishedAlert>& GetActiveAlertsStorage(
AlertType type) const;

private:
std::vector<std::string> GetStrings(AlertType type) const;
std::array<std::set<PublishedAlert>, 3> m_alerts;
};
class PublishedAlert;
class SendableAlerts;

AlertType m_type;
std::string m_text;
Expand Down

0 comments on commit 639ebb1

Please sign in to comment.