Skip to content

Commit

Permalink
[wpilibc] Mechanism2d: Store roots by value
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJohnson committed Oct 25, 2024
1 parent 813618e commit 4fd0e37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 7 additions & 12 deletions wpilibc/src/main/native/cpp/smartdashboard/Mechanism2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,12 @@ Mechanism2d::Mechanism2d(double width, double height,

MechanismRoot2d* Mechanism2d::GetRoot(std::string_view name, double x,
double y) {
auto& obj = m_roots[name];
if (obj) {
return obj.get();
auto [it, isNew] =
m_roots.try_emplace(name, name, x, y, MechanismRoot2d::private_init{});
if (isNew && m_table) {
it->second.Update(m_table->GetSubTable(name));
}
obj = std::make_unique<MechanismRoot2d>(name, x, y,
MechanismRoot2d::private_init{});
if (m_table) {
obj->Update(m_table->GetSubTable(name));
}
return obj.get();
return &it->second;
}

void Mechanism2d::SetBackgroundColor(const Color8Bit& color) {
Expand All @@ -50,8 +46,7 @@ void Mechanism2d::InitSendable(nt::NTSendableBuilder& builder) {
m_dimsPub.Set({{m_width, m_height}});
m_colorPub = m_table->GetStringTopic(kBackgroundColor).Publish();
m_colorPub.Set(m_color);
for (const auto& entry : m_roots) {
const auto& root = entry.second.get();
root->Update(m_table->GetSubTable(entry.first));
for (auto& entry : m_roots) {
entry.second.Update(m_table->GetSubTable(entry.first));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Mechanism2d : public nt::NTSendable,
std::string m_color;
mutable wpi::mutex m_mutex;
std::shared_ptr<nt::NetworkTable> m_table;
wpi::StringMap<std::unique_ptr<MechanismRoot2d>> m_roots;
wpi::StringMap<MechanismRoot2d> m_roots;
nt::DoubleArrayPublisher m_dimsPub;
nt::StringPublisher m_colorPub;
};
Expand Down

0 comments on commit 4fd0e37

Please sign in to comment.