From 96348e835ad2a4cf7bdac3376db8573a853b6f2d Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 29 Dec 2019 23:37:14 -0600 Subject: [PATCH] Fix C++ SendableRegistry::AddChild() (#2207) Add a Sendable* overload so pointers to sendable objects work appropriately. Otherwise an AddLW(this) in a child (which is a Sendable*) could be a different pointer than a void* to the same object. For example: AnalogInput constructor calls AddLW(this) AnalogPotentiometer constructor calls AddChild(analog input pointer) Also add handling for the child object moving (if it's Sendable). --- .../native/cpp/smartdashboard/SendableRegistry.cpp | 14 ++++++++++++++ .../include/frc/smartdashboard/SendableRegistry.h | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/wpilibc/src/main/native/cpp/smartdashboard/SendableRegistry.cpp b/wpilibc/src/main/native/cpp/smartdashboard/SendableRegistry.cpp index bce619642f..9d57ad6b0c 100644 --- a/wpilibc/src/main/native/cpp/smartdashboard/SendableRegistry.cpp +++ b/wpilibc/src/main/native/cpp/smartdashboard/SendableRegistry.cpp @@ -134,6 +134,12 @@ void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& subsystem, comp.subsystem = subsystem.str(); } +void SendableRegistry::AddChild(Sendable* parent, Sendable* child) { + std::scoped_lock lock(m_impl->mutex); + auto& comp = m_impl->GetOrAdd(child); + comp.parent = parent; +} + void SendableRegistry::AddChild(Sendable* parent, void* child) { std::scoped_lock lock(m_impl->mutex); auto& comp = m_impl->GetOrAdd(child); @@ -147,6 +153,10 @@ bool SendableRegistry::Remove(Sendable* sendable) { UID compUid = it->getSecond(); m_impl->components.erase(compUid - 1); m_impl->componentMap.erase(it); + // update any parent pointers + for (auto&& comp : m_impl->components) { + if (comp->parent == sendable) comp->parent = nullptr; + } return true; } @@ -164,6 +174,10 @@ void SendableRegistry::Move(Sendable* to, Sendable* from) { comp.builder.ClearProperties(); to->InitSendable(comp.builder); } + // update any parent pointers + for (auto&& comp : m_impl->components) { + if (comp->parent == from) comp->parent = to; + } } bool SendableRegistry::Contains(const Sendable* sendable) const { diff --git a/wpilibc/src/main/native/include/frc/smartdashboard/SendableRegistry.h b/wpilibc/src/main/native/include/frc/smartdashboard/SendableRegistry.h index 53dbba3ee0..78dabc6331 100644 --- a/wpilibc/src/main/native/include/frc/smartdashboard/SendableRegistry.h +++ b/wpilibc/src/main/native/include/frc/smartdashboard/SendableRegistry.h @@ -118,6 +118,15 @@ class SendableRegistry { void AddLW(Sendable* sendable, const wpi::Twine& subsystem, const wpi::Twine& name); + /** + * Adds a child object to an object. Adds the child object to the registry + * if it's not already present. + * + * @param parent parent object + * @param child child object + */ + void AddChild(Sendable* parent, Sendable* child); + /** * Adds a child object to an object. Adds the child object to the registry * if it's not already present.