mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user