Skip to content

Commit

Permalink
Final cleanup pass
Browse files Browse the repository at this point in the history
- move GetGroupSendable into SendableAlerts (out of public header) and rename to ForGroup
- move java close method above private impl
  • Loading branch information
rzblue committed Nov 6, 2024
1 parent f939bc1 commit 6efa0c3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 47 deletions.
37 changes: 22 additions & 15 deletions wpilibc/src/main/native/cpp/Alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ class Alert::SendableAlerts : public nt::NTSendable,
}
}

/**
* Returns the SendableAlerts for a given group, initializing and publishing
* if it does not already exist.
* @param group the group name
* @return the SendableAlerts for the group
*/
static SendableAlerts& ForGroup(std::string_view group) {
// Force initialization of SendableRegistry before our magic static to
// prevent incorrect destruction order.
wpi::SendableRegistry::EnsureInitialized();
static wpi::StringMap<Alert::SendableAlerts> groups;

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

private:
std::vector<std::string> GetStrings(AlertType type) const {
auto& set = GetActiveAlertsStorage(type);
Expand All @@ -95,7 +115,8 @@ Alert::Alert(std::string_view text, AlertType type)
Alert::Alert(std::string_view group, std::string_view text, AlertType type)
: m_type(type),
m_text(text),
m_activeAlerts{&GetGroupSendable(group).GetActiveAlertsStorage(m_type)} {}
m_activeAlerts{
&SendableAlerts::ForGroup(group).GetActiveAlertsStorage(m_type)} {}

Alert::Alert(Alert&& other)
: m_type{other.m_type},
Expand Down Expand Up @@ -151,20 +172,6 @@ void Alert::SetText(std::string_view text) {
}
}

Alert::SendableAlerts& Alert::GetGroupSendable(std::string_view group) {
// Force initialization of SendableRegistry before our magic static to prevent
// incorrect destruction order.
wpi::SendableRegistry::EnsureInitialized();
static wpi::StringMap<Alert::SendableAlerts> groups;

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

std::string frc::format_as(Alert::AlertType type) {
switch (type) {
case Alert::AlertType::kInfo:
Expand Down
8 changes: 0 additions & 8 deletions wpilibc/src/main/native/include/frc/Alert.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ class Alert {
std::set<PublishedAlert>* m_activeAlerts;
bool m_active = false;
uint64_t m_activeStartTime;

/**
* Returns the SendableAlerts for a given group, initializing and publishing
* if it does not already exist.
* @param group the group name
* @return the SendableAlerts for the group
*/
static SendableAlerts& GetGroupSendable(std::string_view group);
};

std::string format_as(Alert::AlertType type);
Expand Down
48 changes: 24 additions & 24 deletions wpilibj/src/main/java/edu/wpi/first/wpilibj/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public enum AlertType {
kInfo
}

private static final Map<String, SendableAlerts> groups = new HashMap<String, SendableAlerts>();

private final AlertType m_type;
private boolean m_active;
private long m_activeStartTime;
Expand Down Expand Up @@ -100,7 +98,7 @@ public Alert(String text, AlertType type) {
public Alert(String group, String text, AlertType type) {
m_type = type;
m_text = text;
m_activeAlerts = getGroupSendable(group).getActiveAlertsStorage(type);
m_activeAlerts = SendableAlerts.forGroup(group).getActiveAlertsStorage(type);
}

/**
Expand Down Expand Up @@ -168,6 +166,11 @@ public AlertType getType() {
return m_type;
}

@Override
public void close() {
set(false);
}

private record PublishedAlert(long timestamp, String text) implements Comparable<PublishedAlert> {
private static final Comparator<PublishedAlert> comparator =
Comparator.comparingLong((PublishedAlert alert) -> alert.timestamp())
Expand All @@ -181,6 +184,8 @@ public int compareTo(PublishedAlert o) {
}

private static final class SendableAlerts implements Sendable {
private static final Map<String, SendableAlerts> groups = new HashMap<String, SendableAlerts>();

private final Map<AlertType, Set<PublishedAlert>> m_alerts = new HashMap<>();

/**
Expand All @@ -204,27 +209,22 @@ public void initSendable(SendableBuilder builder) {
builder.addStringArrayProperty("warnings", () -> getStrings(AlertType.kWarning), null);
builder.addStringArrayProperty("infos", () -> getStrings(AlertType.kInfo), null);
}
}

@Override
public void close() {
set(false);
}

/**
* Returns the SendableAlerts for a given group, initializing and publishing if it does not
* already exist.
*
* @param group the group name
* @return the SendableAlerts for the group
*/
private static SendableAlerts getGroupSendable(String group) {
return groups.computeIfAbsent(
group,
_group -> {
var sendable = new SendableAlerts();
SmartDashboard.putData(_group, sendable);
return sendable;
});
/**
* Returns the SendableAlerts for a given group, initializing and publishing if it does not
* already exist.
*
* @param group the group name
* @return the SendableAlerts for the group
*/
private static SendableAlerts forGroup(String group) {
return groups.computeIfAbsent(
group,
_group -> {
var sendable = new SendableAlerts();
SmartDashboard.putData(_group, sendable);
return sendable;
});
}
}
}

0 comments on commit 6efa0c3

Please sign in to comment.