Skip to content

Commit

Permalink
Use magic static for groups container
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Oct 21, 2024
1 parent ab2daf0 commit 7e0248c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
26 changes: 16 additions & 10 deletions wpilibc/src/main/native/cpp/Alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@

using namespace frc;

wpi::StringMap<Alert::SendableAlerts> Alert::groups;
Alert::SendableAlerts& Alert::GetGroupSendable(std::string_view group) {
static wpi::StringMap<Alert::SendableAlerts> groups;

auto [iter, exists] = groups.try_emplace(group);
SendableAlerts& sendable = iter->getValue();
if (!exists) {
frc::SmartDashboard::PutData(group, &iter->getValue());
}
return sendable;
}

Alert::Alert(std::string_view text, AlertType type)
: Alert("Alerts", text, type) {}

Alert::Alert(std::string_view group, std::string_view text, AlertType type)
: m_type(type), m_text(text), m_group{group} {
if (!groups.contains(group)) {
frc::SmartDashboard::PutData(group,
&groups.try_emplace(group).first->getValue());
}
}
: m_type(type), m_text(text), m_group{group} {}

Alert::~Alert() {
Set(false);
Expand All @@ -44,9 +48,11 @@ void Alert::Set(bool active) {

if (active) {
m_activeStartTime = frc::RobotController::GetFPGATime();
groups[m_group].GetSetForType(m_type).emplace(m_activeStartTime, m_text);
GetGroupSendable(m_group).GetSetForType(m_type).emplace(m_activeStartTime,
m_text);
} else {
groups[m_group].GetSetForType(m_type).erase({m_activeStartTime, m_text});
GetGroupSendable(m_group).GetSetForType(m_type).erase(
{m_activeStartTime, m_text});
}
m_active = active;
}
Expand All @@ -57,7 +63,7 @@ void Alert::SetText(std::string_view text) {
}

if (m_active) {
auto set = groups[m_group].GetSetForType(m_type);
auto set = GetGroupSendable(m_group).GetSetForType(m_type);
auto iter = set.find({m_activeStartTime, m_text});
auto hint = set.erase(iter);
set.emplace_hint(hint, m_activeStartTime, m_text);
Expand Down
15 changes: 8 additions & 7 deletions wpilibc/src/main/native/include/frc/Alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,6 @@ class Alert {
void SetText(std::string_view text);

private:
AlertType m_type;
std::string m_text;
std::string m_group; // todo: could store iterator or reference to the actual
// SendableAlerts, would remove lookup
bool m_active = false;
uint64_t m_activeStartTime;
class PublishedAlert {
public:
PublishedAlert(uint64_t timestamp, const std::string_view text)
Expand All @@ -143,7 +137,14 @@ class Alert {
std::array<std::set<PublishedAlert>, 3> m_alerts;
};

static wpi::StringMap<SendableAlerts> groups;
AlertType m_type;
std::string m_text;
std::string m_group;

bool m_active = false;
uint64_t m_activeStartTime;

static SendableAlerts& GetGroupSendable(std::string_view group);
};

std::string format_as(Alert::AlertType type);
Expand Down
1 change: 1 addition & 0 deletions wpilibc/src/test/native/cpp/AlertsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <frc/Alert.h>

#include <chrono>
#include <string>

#include <fmt/format.h>
#include <gtest/gtest.h>
Expand Down

0 comments on commit 7e0248c

Please sign in to comment.