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:
@@ -34,10 +34,7 @@ void Gyro::InitGyro() {
|
||||
if (!m_analog->IsAccumulatorChannel()) {
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange,
|
||||
" channel (must be accumulator channel)");
|
||||
if (m_channelAllocated) {
|
||||
delete m_analog;
|
||||
m_analog = nullptr;
|
||||
}
|
||||
m_analog = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,7 +52,7 @@ void Gyro::InitGyro() {
|
||||
|
||||
int64_t value;
|
||||
uint32_t count;
|
||||
m_analog->GetAccumulatorOutput(&value, &count);
|
||||
m_analog->GetAccumulatorOutput(value, count);
|
||||
|
||||
m_center = (uint32_t)((float)value / (float)count + .5);
|
||||
|
||||
@@ -68,7 +65,7 @@ void Gyro::InitGyro() {
|
||||
SetPIDSourceParameter(kAngle);
|
||||
|
||||
HALReport(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel());
|
||||
LiveWindow::GetInstance()->AddSensor("Gyro", m_analog->GetChannel(), this);
|
||||
LiveWindow::GetInstance().AddSensor("Gyro", m_analog->GetChannel(), this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,8 +75,7 @@ void Gyro::InitGyro() {
|
||||
can only be used on on-board Analog Inputs 0-1.
|
||||
*/
|
||||
Gyro::Gyro(int32_t channel) {
|
||||
m_analog = new AnalogInput(channel);
|
||||
m_channelAllocated = true;
|
||||
m_analog = ::std::make_shared<AnalogInput>(channel);
|
||||
InitGyro();
|
||||
}
|
||||
|
||||
@@ -92,28 +88,24 @@ Gyro::Gyro(int32_t channel) {
|
||||
* @param channel A pointer to the AnalogInput object that the gyro is connected
|
||||
* to.
|
||||
*/
|
||||
Gyro::Gyro(AnalogInput *channel) {
|
||||
m_analog = channel;
|
||||
m_channelAllocated = false;
|
||||
if (channel == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
} else {
|
||||
InitGyro();
|
||||
}
|
||||
}
|
||||
Gyro::Gyro(AnalogInput *channel)
|
||||
: Gyro(::std::shared_ptr<AnalogInput>(channel,
|
||||
NullDeleter<AnalogInput>())) {}
|
||||
|
||||
/**
|
||||
* Gyro constructor with a precreated AnalogInput object.
|
||||
* Use this constructor when the analog channel needs to be shared.
|
||||
* This object will not clean up the AnalogInput object when using this
|
||||
* constructor
|
||||
* @param channel A reference to the AnalogInput object that the gyro is
|
||||
* @param channel A pointer to the AnalogInput object that the gyro is
|
||||
* connected to.
|
||||
*/
|
||||
Gyro::Gyro(AnalogInput &channel) {
|
||||
m_analog = &channel;
|
||||
m_channelAllocated = false;
|
||||
InitGyro();
|
||||
Gyro::Gyro(::std::shared_ptr<AnalogInput> channel) : m_analog(channel) {
|
||||
if (channel == nullptr) {
|
||||
wpi_setWPIError(NullParameter);
|
||||
} else {
|
||||
InitGyro();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,13 +116,6 @@ Gyro::Gyro(AnalogInput &channel) {
|
||||
*/
|
||||
void Gyro::Reset() { m_analog->ResetAccumulator(); }
|
||||
|
||||
/**
|
||||
* Delete (free) the accumulator and the analog components used for the gyro.
|
||||
*/
|
||||
Gyro::~Gyro() {
|
||||
if (m_channelAllocated) delete m_analog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the actual angle in degrees that the robot is currently facing.
|
||||
*
|
||||
@@ -149,7 +134,7 @@ Gyro::~Gyro() {
|
||||
float Gyro::GetAngle() const {
|
||||
int64_t rawValue;
|
||||
uint32_t count;
|
||||
m_analog->GetAccumulatorOutput(&rawValue, &count);
|
||||
m_analog->GetAccumulatorOutput(rawValue, count);
|
||||
|
||||
int64_t value = rawValue - (int64_t)((float)count * m_offset);
|
||||
|
||||
@@ -240,9 +225,9 @@ void Gyro::StopLiveWindowMode() {}
|
||||
|
||||
std::string Gyro::GetSmartDashboardType() const { return "Gyro"; }
|
||||
|
||||
void Gyro::InitTable(ITable *subTable) {
|
||||
void Gyro::InitTable(::std::shared_ptr<ITable> subTable) {
|
||||
m_table = subTable;
|
||||
UpdateTable();
|
||||
}
|
||||
|
||||
ITable *Gyro::GetTable() const { return m_table; }
|
||||
::std::shared_ptr<ITable> Gyro::GetTable() const { return m_table; }
|
||||
|
||||
Reference in New Issue
Block a user