[wpilib] Deprecate getInstance() in favor of static functions (#3440)

Co-authored-by: Noam Zaks <imnoamzaks@gmail.com>
This commit is contained in:
Peter Johnson
2021-06-15 23:06:03 -07:00
committed by GitHub
parent 26ff9371d9
commit 362066a9b7
105 changed files with 1500 additions and 1539 deletions

View File

@@ -15,133 +15,152 @@
using namespace frc;
struct LiveWindow::Impl {
Impl();
namespace {
struct Component {
bool firstTime = true;
bool telemetryEnabled = true;
};
struct Component {
bool firstTime = true;
bool telemetryEnabled = true;
};
struct Instance {
Instance() {
wpi::SendableRegistry::SetLiveWindowBuilderFactory(
[] { return std::make_unique<SendableBuilderImpl>(); });
}
wpi::mutex mutex;
wpi::SendableRegistry& registry;
int dataHandle;
int dataHandle = wpi::SendableRegistry::GetDataHandle();
std::shared_ptr<nt::NetworkTable> liveWindowTable;
std::shared_ptr<nt::NetworkTable> statusTable;
nt::NetworkTableEntry enabledEntry;
std::shared_ptr<nt::NetworkTable> liveWindowTable =
nt::NetworkTableInstance::GetDefault().GetTable("LiveWindow");
std::shared_ptr<nt::NetworkTable> statusTable =
liveWindowTable->GetSubTable(".status");
nt::NetworkTableEntry enabledEntry = statusTable->GetEntry("LW Enabled");
bool startLiveWindow = false;
bool liveWindowEnabled = false;
bool telemetryEnabled = true;
std::function<void()> enabled;
std::function<void()> disabled;
std::shared_ptr<Component> GetOrAdd(wpi::Sendable* sendable);
};
} // namespace
LiveWindow::Impl::Impl()
: registry(wpi::SendableRegistry::GetInstance()),
dataHandle(registry.GetDataHandle()),
liveWindowTable(
nt::NetworkTableInstance::GetDefault().GetTable("LiveWindow")) {
registry.SetLiveWindowBuilderFactory(
[] { return std::make_unique<SendableBuilderImpl>(); });
statusTable = liveWindowTable->GetSubTable(".status");
enabledEntry = statusTable->GetEntry("LW Enabled");
static Instance& GetInstance() {
static Instance instance;
return instance;
}
std::shared_ptr<LiveWindow::Impl::Component> LiveWindow::Impl::GetOrAdd(
wpi::Sendable* sendable) {
std::shared_ptr<Component> Instance::GetOrAdd(wpi::Sendable* sendable) {
auto data = std::static_pointer_cast<Component>(
registry.GetData(sendable, dataHandle));
wpi::SendableRegistry::GetData(sendable, dataHandle));
if (!data) {
data = std::make_shared<Component>();
registry.SetData(sendable, dataHandle, data);
wpi::SendableRegistry::SetData(sendable, dataHandle, data);
}
return data;
}
LiveWindow* LiveWindow::GetInstance() {
::GetInstance();
static LiveWindow instance;
return &instance;
}
void LiveWindow::SetEnabledCallback(std::function<void()> func) {
::GetInstance().enabled = func;
}
void LiveWindow::SetDisabledCallback(std::function<void()> func) {
::GetInstance().disabled = func;
}
void LiveWindow::EnableTelemetry(wpi::Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
// Re-enable global setting in case DisableAllTelemetry() was called.
m_impl->telemetryEnabled = true;
m_impl->GetOrAdd(sendable)->telemetryEnabled = true;
inst.telemetryEnabled = true;
inst.GetOrAdd(sendable)->telemetryEnabled = true;
}
void LiveWindow::DisableTelemetry(wpi::Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
m_impl->GetOrAdd(sendable)->telemetryEnabled = false;
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
inst.GetOrAdd(sendable)->telemetryEnabled = false;
}
void LiveWindow::DisableAllTelemetry() {
std::scoped_lock lock(m_impl->mutex);
m_impl->telemetryEnabled = false;
m_impl->registry.ForeachLiveWindow(m_impl->dataHandle, [&](auto& cbdata) {
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
inst.telemetryEnabled = false;
wpi::SendableRegistry::ForeachLiveWindow(inst.dataHandle, [&](auto& cbdata) {
if (!cbdata.data) {
cbdata.data = std::make_shared<Impl::Component>();
cbdata.data = std::make_shared<Component>();
}
std::static_pointer_cast<Impl::Component>(cbdata.data)->telemetryEnabled =
false;
std::static_pointer_cast<Component>(cbdata.data)->telemetryEnabled = false;
});
}
bool LiveWindow::IsEnabled() const {
std::scoped_lock lock(m_impl->mutex);
return m_impl->liveWindowEnabled;
bool LiveWindow::IsEnabled() {
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
return inst.liveWindowEnabled;
}
void LiveWindow::SetEnabled(bool enabled) {
std::scoped_lock lock(m_impl->mutex);
if (m_impl->liveWindowEnabled == enabled) {
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
if (inst.liveWindowEnabled == enabled) {
return;
}
m_impl->startLiveWindow = enabled;
m_impl->liveWindowEnabled = enabled;
inst.startLiveWindow = enabled;
inst.liveWindowEnabled = enabled;
// Force table generation now to make sure everything is defined
UpdateValuesUnsafe();
if (enabled) {
if (this->enabled) {
this->enabled();
if (inst.enabled) {
inst.enabled();
}
} else {
m_impl->registry.ForeachLiveWindow(m_impl->dataHandle, [&](auto& cbdata) {
static_cast<SendableBuilderImpl&>(cbdata.builder).StopLiveWindowMode();
});
if (this->disabled) {
this->disabled();
wpi::SendableRegistry::ForeachLiveWindow(
inst.dataHandle, [&](auto& cbdata) {
static_cast<SendableBuilderImpl&>(cbdata.builder)
.StopLiveWindowMode();
});
if (inst.disabled) {
inst.disabled();
}
}
m_impl->enabledEntry.SetBoolean(enabled);
inst.enabledEntry.SetBoolean(enabled);
}
void LiveWindow::UpdateValues() {
std::scoped_lock lock(m_impl->mutex);
auto& inst = ::GetInstance();
std::scoped_lock lock(inst.mutex);
UpdateValuesUnsafe();
}
void LiveWindow::UpdateValuesUnsafe() {
auto& inst = ::GetInstance();
// Only do this if either LiveWindow mode or telemetry is enabled.
if (!m_impl->liveWindowEnabled && !m_impl->telemetryEnabled) {
if (!inst.liveWindowEnabled && !inst.telemetryEnabled) {
return;
}
m_impl->registry.ForeachLiveWindow(m_impl->dataHandle, [&](auto& cbdata) {
wpi::SendableRegistry::ForeachLiveWindow(inst.dataHandle, [&](auto& cbdata) {
if (!cbdata.sendable || cbdata.parent) {
return;
}
if (!cbdata.data) {
cbdata.data = std::make_shared<Impl::Component>();
cbdata.data = std::make_shared<Component>();
}
auto& comp = *std::static_pointer_cast<Impl::Component>(cbdata.data);
auto& comp = *std::static_pointer_cast<Component>(cbdata.data);
if (!m_impl->liveWindowEnabled && !comp.telemetryEnabled) {
if (!inst.liveWindowEnabled && !comp.telemetryEnabled) {
return;
}
@@ -153,7 +172,7 @@ void LiveWindow::UpdateValuesUnsafe() {
if (cbdata.name.empty()) {
return;
}
auto ssTable = m_impl->liveWindowTable->GetSubTable(cbdata.subsystem);
auto ssTable = inst.liveWindowTable->GetSubTable(cbdata.subsystem);
std::shared_ptr<nt::NetworkTable> table;
// Treat name==subsystem as top level of subsystem
if (cbdata.name == cbdata.subsystem) {
@@ -169,13 +188,11 @@ void LiveWindow::UpdateValuesUnsafe() {
comp.firstTime = false;
}
if (m_impl->startLiveWindow) {
if (inst.startLiveWindow) {
static_cast<SendableBuilderImpl&>(cbdata.builder).StartLiveWindowMode();
}
cbdata.builder.Update();
});
m_impl->startLiveWindow = false;
inst.startLiveWindow = false;
}
LiveWindow::LiveWindow() : m_impl(new Impl) {}