mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpilibc] Remove Alert magic static with map lookup (#7712)
The magic static adds yet another thing that needs to be reset if you want to run a unit test with a completely reset wpilib state. Use the existing Sendable static map to store the data instead. This leaks memory if ResetSmartDashboardInstance is called.
This commit is contained in:
@@ -82,17 +82,18 @@ class Alert::SendableAlerts : public nt::NTSendable,
|
||||
* @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, inserted] = groups.try_emplace(group);
|
||||
SendableAlerts& sendable = iter->second;
|
||||
if (inserted) {
|
||||
frc::SmartDashboard::PutData(group, &iter->second);
|
||||
SendableAlerts* salert = nullptr;
|
||||
try {
|
||||
auto* sendable = frc::SmartDashboard::GetData(group);
|
||||
salert = dynamic_cast<SendableAlerts*>(sendable);
|
||||
} catch (frc::RuntimeError&) {
|
||||
}
|
||||
return sendable;
|
||||
if (!salert) {
|
||||
// this leaks if ResetSmartDashboardInstance is called, but that's fine
|
||||
salert = new Alert::SendableAlerts;
|
||||
frc::SmartDashboard::PutData(group, salert);
|
||||
}
|
||||
return *salert;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in New Issue
Block a user