mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpilibc] Mechanism2d: Store roots by value
This commit is contained in:
@@ -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) {
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user