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).
This commit is contained in:
Peter Johnson
2019-12-29 23:37:14 -06:00
committed by GitHub
parent d91796f8d2
commit 96348e835a
2 changed files with 23 additions and 0 deletions

View File

@@ -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 {