Simplify Sendable interface (#1864)

This removes the name and subsystem from individual objects, and instead
puts this data into a new singleton class, SendableRegistry.  Much of
LiveWindow has been refactored into SendableRegistry.

In C++, a new CRTP helper class, SendableHelper, has been added to provide
move and destruction functionality.

Shims for GetName, SetName, GetSubsystem, and SetSubsystem have been added
to Command and Subsystem (both old and new), and also to SendableHelper to
prevent code breakage.

This deprecates SendableBase in preparation for future removal.
This commit is contained in:
Peter Johnson
2019-09-14 15:22:54 -05:00
committed by GitHub
parent 1d8c4d016f
commit 471f375a38
216 changed files with 2448 additions and 1433 deletions

View File

@@ -7,18 +7,14 @@
#include "frc/livewindow/LiveWindow.h"
#include <algorithm>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/DenseMap.h>
#include <wpi/SmallString.h>
#include <wpi/mutex.h>
#include <wpi/raw_ostream.h>
#include "frc/commands/Scheduler.h"
#include "frc/smartdashboard/SendableBuilderImpl.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -28,8 +24,6 @@ struct LiveWindow::Impl {
Impl();
struct Component {
std::shared_ptr<Sendable> sendable;
Sendable* parent = nullptr;
SendableBuilderImpl builder;
bool firstTime = true;
bool telemetryEnabled = true;
@@ -37,7 +31,8 @@ struct LiveWindow::Impl {
wpi::mutex mutex;
wpi::DenseMap<void*, Component> components;
SendableRegistry& registry;
int dataHandle;
std::shared_ptr<nt::NetworkTable> liveWindowTable;
std::shared_ptr<nt::NetworkTable> statusTable;
@@ -46,64 +41,57 @@ struct LiveWindow::Impl {
bool startLiveWindow = false;
bool liveWindowEnabled = false;
bool telemetryEnabled = true;
std::shared_ptr<Component> GetOrAdd(Sendable* sendable);
};
LiveWindow::Impl::Impl()
: liveWindowTable(
: registry(SendableRegistry::GetInstance()),
dataHandle(registry.GetDataHandle()),
liveWindowTable(
nt::NetworkTableInstance::GetDefault().GetTable("LiveWindow")) {
statusTable = liveWindowTable->GetSubTable(".status");
enabledEntry = statusTable->GetEntry("LW Enabled");
}
std::shared_ptr<LiveWindow::Impl::Component> LiveWindow::Impl::GetOrAdd(
Sendable* sendable) {
auto data = std::static_pointer_cast<Component>(
registry.GetData(sendable, dataHandle));
if (!data) {
data = std::make_shared<Component>();
registry.SetData(sendable, dataHandle, data);
}
return data;
}
LiveWindow* LiveWindow::GetInstance() {
static LiveWindow instance;
return &instance;
}
void LiveWindow::Add(std::shared_ptr<Sendable> sendable) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->components[sendable.get()];
comp.sendable = sendable;
}
void LiveWindow::Add(Sendable* sendable) {
Add(std::shared_ptr<Sendable>(sendable, NullDeleter<Sendable>()));
}
void LiveWindow::AddChild(Sendable* parent, std::shared_ptr<Sendable> child) {
AddChild(parent, child.get());
}
void LiveWindow::AddChild(Sendable* parent, void* child) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->components[child];
comp.parent = parent;
comp.telemetryEnabled = false;
}
bool LiveWindow::Remove(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
return m_impl->components.erase(sendable);
}
void LiveWindow::EnableTelemetry(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
// Re-enable global setting in case DisableAllTelemetry() was called.
m_impl->telemetryEnabled = true;
auto i = m_impl->components.find(sendable);
if (i != m_impl->components.end()) i->getSecond().telemetryEnabled = true;
m_impl->GetOrAdd(sendable)->telemetryEnabled = true;
}
void LiveWindow::DisableTelemetry(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto i = m_impl->components.find(sendable);
if (i != m_impl->components.end()) i->getSecond().telemetryEnabled = false;
m_impl->GetOrAdd(sendable)->telemetryEnabled = false;
}
void LiveWindow::DisableAllTelemetry() {
std::scoped_lock lock(m_impl->mutex);
m_impl->telemetryEnabled = false;
for (auto& i : m_impl->components) i.getSecond().telemetryEnabled = false;
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle, [&](Sendable*, wpi::StringRef, wpi::StringRef,
Sendable*, std::shared_ptr<void>& data) {
if (!data) data = std::make_shared<Impl::Component>();
std::static_pointer_cast<Impl::Component>(data)->telemetryEnabled =
false;
});
}
bool LiveWindow::IsEnabled() const {
@@ -123,9 +111,13 @@ void LiveWindow::SetEnabled(bool enabled) {
scheduler->SetEnabled(false);
scheduler->RemoveAll();
} else {
for (auto& i : m_impl->components) {
i.getSecond().builder.StopLiveWindowMode();
}
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle, [&](Sendable*, wpi::StringRef, wpi::StringRef,
Sendable*, std::shared_ptr<void>& data) {
if (data)
std::static_pointer_cast<Impl::Component>(data)
->builder.StopLiveWindowMode();
});
scheduler->SetEnabled(true);
}
m_impl->enabledEntry.SetBoolean(enabled);
@@ -140,37 +132,42 @@ void LiveWindow::UpdateValuesUnsafe() {
// Only do this if either LiveWindow mode or telemetry is enabled.
if (!m_impl->liveWindowEnabled && !m_impl->telemetryEnabled) return;
for (auto& i : m_impl->components) {
auto& comp = i.getSecond();
if (comp.sendable && !comp.parent &&
(m_impl->liveWindowEnabled || comp.telemetryEnabled)) {
if (comp.firstTime) {
// By holding off creating the NetworkTable entries, it allows the
// components to be redefined. This allows default sensor and actuator
// values to be created that are replaced with the custom names from
// users calling setName.
auto name = comp.sendable->GetName();
if (name.empty()) continue;
auto subsystem = comp.sendable->GetSubsystem();
auto ssTable = m_impl->liveWindowTable->GetSubTable(subsystem);
std::shared_ptr<NetworkTable> table;
// Treat name==subsystem as top level of subsystem
if (name == subsystem)
table = ssTable;
else
table = ssTable->GetSubTable(name);
table->GetEntry(".name").SetString(name);
comp.builder.SetTable(table);
comp.sendable->InitSendable(comp.builder);
ssTable->GetEntry(".type").SetString("LW Subsystem");
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle,
[&](Sendable* sendable, wpi::StringRef name, wpi::StringRef subsystem,
Sendable* parent, std::shared_ptr<void>& data) {
if (!sendable || parent) return;
comp.firstTime = false;
}
if (!data) data = std::make_shared<Impl::Component>();
if (m_impl->startLiveWindow) comp.builder.StartLiveWindowMode();
comp.builder.UpdateTable();
}
}
auto& comp = *std::static_pointer_cast<Impl::Component>(data);
if (!m_impl->liveWindowEnabled && !comp.telemetryEnabled) return;
if (comp.firstTime) {
// By holding off creating the NetworkTable entries, it allows the
// components to be redefined. This allows default sensor and actuator
// values to be created that are replaced with the custom names from
// users calling setName.
if (name.empty()) return;
auto ssTable = m_impl->liveWindowTable->GetSubTable(subsystem);
std::shared_ptr<NetworkTable> table;
// Treat name==subsystem as top level of subsystem
if (name == subsystem)
table = ssTable;
else
table = ssTable->GetSubTable(name);
table->GetEntry(".name").SetString(name);
comp.builder.SetTable(table);
sendable->InitSendable(comp.builder);
ssTable->GetEntry(".type").SetString("LW Subsystem");
comp.firstTime = false;
}
if (m_impl->startLiveWindow) comp.builder.StartLiveWindowMode();
comp.builder.UpdateTable();
});
m_impl->startLiveWindow = false;
}