mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code in wpilibC++Devices and a substantial portion of it in wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests are untouched except where it is necessary to make them work with the rest of the libraries. There is still a lot to do in the following areas: -The HAL (which we may not want to touch at all). -The I2C, Serial, and SPI interfaces in wpilibC++Devices, which I haven't gotten around to doing yet. -Most wpilibC++Devices classes have void* pointers for interacting with the HAL. -InterruptableSensorBase passes a void *params for the interrupt handler. -I haven't converted all the const char* to std::strings. -There are plenty of other cases of raw pointers still existing. -This doesn't fall directly under raw pointer stuff, but move syntax and rvalue references could be introduced in many places. -I haven't touched vision code. -The Resource classes conflict (one is in the hal, the other in wpilibC++). Someone should figure out a more permanent fix (eg, just renaming them), then doing what I did (making a new namespace for one of them, essentially the same as renaming it). A few other things: -I created a NullDeleter class which is marked as deprecated. What this does is it can be passed as the deleter to a std::shared_ptr so that when you are converting raw pointers to shared_ptrs the shared_ptr doesn't do any deletion if someone else owns the raw pointer. This should only be used in making old raw pointer UIs. -I had to alter the build.gradle so that it did not emit errors when deprecated functions called deprecated functions. Unfortunately, gradle doesn't appear to be actually printing out gcc warnigns for some reason. The best way I have found to fix this is to patch the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff) so that a deprecated function calling a deprecated function is fine but a non-deprecated function calling a deprecated function will throw a warning (which we then elevate with -Werror). I believe that clang deals with this properly, although I have not tried it myself. Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
This commit is contained in:
@@ -9,19 +9,18 @@
|
||||
* regardless of
|
||||
* how many times GetInstance is called.
|
||||
*/
|
||||
LiveWindow *LiveWindow::GetInstance() {
|
||||
LiveWindow &LiveWindow::GetInstance() {
|
||||
static LiveWindow instance;
|
||||
return &instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* LiveWindow constructor.
|
||||
* Allocate the necessary tables.
|
||||
*/
|
||||
LiveWindow::LiveWindow() {
|
||||
m_liveWindowTable = NetworkTable::GetTable("LiveWindow");
|
||||
m_statusTable = m_liveWindowTable->GetSubTable("~STATUS~");
|
||||
m_scheduler = Scheduler::GetInstance();
|
||||
LiveWindow::LiveWindow() : m_scheduler(Scheduler::GetInstance()) {
|
||||
m_liveWindowTable.reset(NetworkTable::GetTable("LiveWindow"));
|
||||
m_statusTable.reset(m_liveWindowTable->GetSubTable("~STATUS~"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -35,8 +34,8 @@ void LiveWindow::SetEnabled(bool enabled) {
|
||||
InitializeLiveWindowComponents();
|
||||
m_firstTime = false;
|
||||
}
|
||||
m_scheduler->SetEnabled(false);
|
||||
m_scheduler->RemoveAll();
|
||||
m_scheduler.SetEnabled(false);
|
||||
m_scheduler.RemoveAll();
|
||||
for (auto& elem : m_components) {
|
||||
elem.first->StartLiveWindowMode();
|
||||
}
|
||||
@@ -44,7 +43,7 @@ void LiveWindow::SetEnabled(bool enabled) {
|
||||
for (auto& elem : m_components) {
|
||||
elem.first->StopLiveWindowMode();
|
||||
}
|
||||
m_scheduler->SetEnabled(true);
|
||||
m_scheduler.SetEnabled(true);
|
||||
}
|
||||
m_enabled = enabled;
|
||||
m_statusTable->PutBoolean("LW Enabled", m_enabled);
|
||||
@@ -58,12 +57,17 @@ void LiveWindow::SetEnabled(bool enabled) {
|
||||
* @param component A LiveWindowSendable component that represents a sensor.
|
||||
*/
|
||||
void LiveWindow::AddSensor(const char *subsystem, const char *name,
|
||||
LiveWindowSendable *component) {
|
||||
::std::shared_ptr<LiveWindowSendable> component) {
|
||||
m_components[component].subsystem = subsystem;
|
||||
m_components[component].name = name;
|
||||
m_components[component].isSensor = true;
|
||||
}
|
||||
|
||||
void LiveWindow::AddSensor(const char *subsystem, const char *name, LiveWindowSendable *component) {
|
||||
AddSensor(subsystem, name, ::std::shared_ptr<LiveWindowSendable>(
|
||||
component, NullDeleter<LiveWindowSendable>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an Actuator associated with the subsystem and with call it by the given
|
||||
* name.
|
||||
@@ -72,15 +76,23 @@ void LiveWindow::AddSensor(const char *subsystem, const char *name,
|
||||
* @param component A LiveWindowSendable component that represents a actuator.
|
||||
*/
|
||||
void LiveWindow::AddActuator(const char *subsystem, const char *name,
|
||||
LiveWindowSendable *component) {
|
||||
::std::shared_ptr<LiveWindowSendable> component) {
|
||||
m_components[component].subsystem = subsystem;
|
||||
m_components[component].name = name;
|
||||
m_components[component].isSensor = false;
|
||||
}
|
||||
|
||||
void LiveWindow::AddActuator(const char *subsystem, const char *name,
|
||||
LiveWindowSendable *component) {
|
||||
AddActuator(subsystem, name,
|
||||
::std::shared_ptr<LiveWindowSendable>(
|
||||
component, NullDeleter<LiveWindowSendable>()));
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL
|
||||
*/
|
||||
[[deprecated]]
|
||||
void LiveWindow::AddSensor(std::string type, int channel,
|
||||
LiveWindowSendable *component) {
|
||||
std::ostringstream oss;
|
||||
@@ -90,9 +102,11 @@ void LiveWindow::AddSensor(std::string type, int channel,
|
||||
types.copy(cc, types.size());
|
||||
cc[types.size()] = '\0';
|
||||
AddSensor("Ungrouped", cc, component);
|
||||
if (std::find(m_sensors.begin(), m_sensors.end(), component) ==
|
||||
::std::shared_ptr<LiveWindowSendable> component_stl(
|
||||
component, NullDeleter<LiveWindowSendable>());
|
||||
if (std::find(m_sensors.begin(), m_sensors.end(), component_stl) ==
|
||||
m_sensors.end())
|
||||
m_sensors.push_back(component);
|
||||
m_sensors.push_back(component_stl);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,7 +120,8 @@ void LiveWindow::AddActuator(std::string type, int channel,
|
||||
auto cc = new char[types.size() + 1];
|
||||
types.copy(cc, types.size());
|
||||
cc[types.size()] = '\0';
|
||||
AddActuator("Ungrouped", cc, component);
|
||||
AddActuator("Ungrouped", cc, ::std::shared_ptr<LiveWindowSendable>(
|
||||
component, [](LiveWindowSendable *) {}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,7 +135,8 @@ void LiveWindow::AddActuator(std::string type, int module, int channel,
|
||||
auto cc = new char[types.size() + 1];
|
||||
types.copy(cc, types.size());
|
||||
cc[types.size()] = '\0';
|
||||
AddActuator("Ungrouped", cc, component);
|
||||
AddActuator("Ungrouped", cc, ::std::shared_ptr<LiveWindowSendable>(
|
||||
component, [](LiveWindowSendable *) {}));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,25 +162,21 @@ void LiveWindow::Run() {
|
||||
|
||||
/**
|
||||
* Initialize all the LiveWindow elements the first time we enter LiveWindow
|
||||
* mode.
|
||||
* By holding off creating the NetworkTable entries, it allows them to be
|
||||
* redefined
|
||||
* before the first time in LiveWindow mode. This allows default sensor and
|
||||
* actuator
|
||||
* values to be created that are replaced with the custom names from users
|
||||
* calling
|
||||
* addActuator and addSensor.
|
||||
* mode. By holding off creating the NetworkTable entries, it allows them to be
|
||||
* redefined before the first time in LiveWindow mode. This allows default
|
||||
* sensor and actuator values to be created that are replaced with the custom
|
||||
* names from users calling addActuator and addSensor.
|
||||
*/
|
||||
void LiveWindow::InitializeLiveWindowComponents() {
|
||||
for (auto& elem : m_components) {
|
||||
LiveWindowSendable *component = elem.first;
|
||||
::std::shared_ptr<LiveWindowSendable> component = elem.first;
|
||||
LiveWindowComponent c = elem.second;
|
||||
std::string subsystem = c.subsystem;
|
||||
std::string name = c.name;
|
||||
m_liveWindowTable->GetSubTable(subsystem)
|
||||
->PutString("~TYPE~", "LW Subsystem");
|
||||
ITable *table =
|
||||
m_liveWindowTable->GetSubTable(subsystem)->GetSubTable(name);
|
||||
::std::shared_ptr<ITable> table(
|
||||
m_liveWindowTable->GetSubTable(subsystem)->GetSubTable(name));
|
||||
table->PutString("~TYPE~", component->GetSmartDashboardType());
|
||||
table->PutString("Name", name);
|
||||
table->PutString("Subsystem", subsystem);
|
||||
|
||||
Reference in New Issue
Block a user