[hal] Refactor C++ handle closing; check for invalid handle before closing (#7016)

Adds a close function pointer template parameter to hal::Handle.  This allows default destructors in many places.
The status parameter has been removed from close functions; in most places it was not used. Where it was, an error is printed instead.
This commit is contained in:
Ryan Blue
2024-09-07 13:58:15 -04:00
committed by GitHub
parent 80f3813908
commit 496e7c1bba
94 changed files with 247 additions and 425 deletions

View File

@@ -29,19 +29,12 @@ AddressableLED::AddressableLED(int port) : m_port{port} {
m_handle = HAL_InitializeAddressableLED(m_pwmHandle, &status);
FRC_CheckErrorStatus(status, "Port {}", port);
if (m_handle == HAL_kInvalidHandle) {
HAL_FreePWMPort(m_pwmHandle, &status);
HAL_FreePWMPort(m_pwmHandle);
}
HAL_Report(HALUsageReporting::kResourceType_AddressableLEDs, port + 1);
}
AddressableLED::~AddressableLED() {
HAL_FreeAddressableLED(m_handle);
int32_t status = 0;
HAL_FreePWMPort(m_pwmHandle, &status);
FRC_ReportError(status, "Port {}", m_port);
}
void AddressableLED::SetLength(int length) {
int32_t status = 0;
HAL_SetAddressableLEDLength(m_handle, length, &status);

View File

@@ -58,10 +58,6 @@ AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center,
Reset();
}
AnalogGyro::~AnalogGyro() {
HAL_FreeAnalogGyro(m_gyroHandle);
}
double AnalogGyro::GetAngle() const {
int32_t status = 0;
double value = HAL_GetAnalogGyroAngle(m_gyroHandle, &status);

View File

@@ -37,10 +37,6 @@ AnalogInput::AnalogInput(int channel) {
wpi::SendableRegistry::AddLW(this, "AnalogInput", channel);
}
AnalogInput::~AnalogInput() {
HAL_FreeAnalogInputPort(m_port);
}
int AnalogInput::GetValue() const {
int32_t status = 0;
int value = HAL_GetAnalogValue(m_port, &status);

View File

@@ -37,10 +37,6 @@ AnalogOutput::AnalogOutput(int channel) {
wpi::SendableRegistry::AddLW(this, "AnalogOutput", m_channel);
}
AnalogOutput::~AnalogOutput() {
HAL_FreeAnalogOutputPort(m_port);
}
void AnalogOutput::SetVoltage(double voltage) {
int32_t status = 0;
HAL_SetAnalogOutput(m_port, voltage, &status);

View File

@@ -57,12 +57,6 @@ AnalogTrigger::AnalogTrigger(std::shared_ptr<DutyCycle> input)
wpi::SendableRegistry::AddLW(this, "AnalogTrigger", index);
}
AnalogTrigger::~AnalogTrigger() {
int32_t status = 0;
HAL_CleanAnalogTrigger(m_trigger, &status);
FRC_ReportError(status, "Channel {}", GetSourceChannel());
}
void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {
int32_t status = 0;
HAL_SetAnalogTriggerLimitsVoltage(m_trigger, lower, upper, &status);

View File

@@ -35,13 +35,6 @@ CAN::CAN(int deviceId, int deviceManufacturer, int deviceType) {
HAL_Report(HALUsageReporting::kResourceType_CAN, deviceId + 1);
}
CAN::~CAN() {
if (m_handle != HAL_kInvalidHandle) {
HAL_CleanCAN(m_handle);
m_handle = HAL_kInvalidHandle;
}
}
void CAN::WritePacket(const uint8_t* data, int length, int apiId) {
int32_t status = 0;
HAL_WriteCANPacket(m_handle, data, length, apiId, &status);

View File

@@ -89,10 +89,6 @@ Counter::~Counter() {
} catch (const RuntimeError& e) {
e.Report();
}
int32_t status = 0;
HAL_FreeCounter(m_counter, &status);
FRC_ReportError(status, "Counter destructor");
}
void Counter::SetUpSource(int channel) {

View File

@@ -30,10 +30,6 @@ DMA::DMA() {
FRC_CheckErrorStatus(status, "InitializeDMA");
}
DMA::~DMA() {
HAL_FreeDMA(dmaHandle);
}
void DMA::SetPause(bool pause) {
int32_t status = 0;
HAL_SetDMAPause(dmaHandle, pause, &status);

View File

@@ -35,10 +35,6 @@ DigitalInput::DigitalInput(int channel) {
wpi::SendableRegistry::AddLW(this, "DigitalInput", channel);
}
DigitalInput::~DigitalInput() {
HAL_FreeDIOPort(m_handle);
}
bool DigitalInput::Get() const {
int32_t status = 0;
bool value = HAL_GetDIO(m_handle, &status);

View File

@@ -43,8 +43,6 @@ DigitalOutput::~DigitalOutput() {
} catch (const RuntimeError& e) {
e.Report();
}
HAL_FreeDIOPort(m_handle);
}
void DigitalOutput::Set(bool value) {
@@ -141,8 +139,7 @@ void DigitalOutput::DisablePWM() {
&status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
HAL_FreeDigitalPWM(m_pwmGenerator, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channel);
HAL_FreeDigitalPWM(m_pwmGenerator);
m_pwmGenerator = HAL_kInvalidHandle;
}

View File

@@ -35,10 +35,6 @@ DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source)
InitDutyCycle();
}
DutyCycle::~DutyCycle() {
HAL_FreeDutyCycle(m_handle);
}
void DutyCycle::InitDutyCycle() {
int32_t status = 0;
m_handle =

View File

@@ -59,12 +59,6 @@ Encoder::Encoder(std::shared_ptr<DigitalSource> aSource,
InitEncoder(reverseDirection, encodingType);
}
Encoder::~Encoder() {
int32_t status = 0;
HAL_FreeEncoder(m_encoder, &status);
FRC_ReportError(status, "FreeEncoder");
}
int Encoder::Get() const {
int32_t status = 0;
int value = HAL_GetEncoder(m_encoder, &status);

View File

@@ -31,10 +31,6 @@ I2C::I2C(Port port, int deviceAddress)
HAL_Report(HALUsageReporting::kResourceType_I2C, deviceAddress);
}
I2C::~I2C() {
HAL_CloseI2C(m_port);
}
I2C::Port I2C::GetPort() const {
return static_cast<Port>(static_cast<int>(m_port));
}

View File

@@ -128,7 +128,7 @@ Notifier::~Notifier() {
m_thread.join();
}
HAL_CleanNotifier(handle, &status);
HAL_CleanNotifier(handle);
}
Notifier::Notifier(Notifier&& rhs)

View File

@@ -46,12 +46,8 @@ PWM::PWM(int channel, bool registerSendable) {
PWM::~PWM() {
int32_t status = 0;
HAL_SetPWMDisabled(m_handle, &status);
FRC_ReportError(status, "Channel {}", m_channel);
HAL_FreePWMPort(m_handle, &status);
FRC_ReportError(status, "Channel {}", m_channel);
}
void PWM::SetPulseTime(units::microsecond_t time) {

View File

@@ -57,13 +57,6 @@ PowerDistribution::PowerDistribution(int module, ModuleType moduleType) {
wpi::SendableRegistry::AddLW(this, "PowerDistribution", m_module);
}
PowerDistribution::~PowerDistribution() {
if (m_handle != HAL_kInvalidHandle) {
HAL_CleanPowerDistribution(m_handle);
m_handle = HAL_kInvalidHandle;
}
}
int PowerDistribution::GetNumChannels() const {
int32_t status = 0;
int32_t size = HAL_GetPowerDistributionNumChannels(m_handle, &status);

View File

@@ -63,13 +63,6 @@ Relay::~Relay() {
int32_t status = 0;
HAL_SetRelay(m_forwardHandle, false, &status);
HAL_SetRelay(m_reverseHandle, false, &status);
// ignore errors, as we want to make sure a free happens.
if (m_forwardHandle != HAL_kInvalidHandle) {
HAL_FreeRelayPort(m_forwardHandle);
}
if (m_reverseHandle != HAL_kInvalidHandle) {
HAL_FreeRelayPort(m_reverseHandle);
}
}
void Relay::Set(Relay::Value value) {

View File

@@ -165,9 +165,7 @@ SPI::SPI(Port port) : m_port(static_cast<HAL_SPIPort>(port)) {
static_cast<uint8_t>(port) + 1);
}
SPI::~SPI() {
HAL_CloseSPI(m_port);
}
SPI::~SPI() = default;
SPI::Port SPI::GetPort() const {
return static_cast<Port>(static_cast<int>(m_port));

View File

@@ -74,12 +74,6 @@ SerialPort::SerialPort(int baudRate, std::string_view portName, Port port,
static_cast<uint8_t>(port) + 1);
}
SerialPort::~SerialPort() {
int32_t status = 0;
HAL_CloseSerial(m_portHandle, &status);
FRC_ReportError(status, "CloseSerial");
}
void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) {
int32_t status = 0;
HAL_SetSerialFlowControl(m_portHandle, flowControl, &status);

View File

@@ -49,10 +49,6 @@ void SynchronousInterrupt::InitSynchronousInterrupt() {
FRC_CheckErrorStatus(status, "Interrupt setting up source edge failed");
}
SynchronousInterrupt::~SynchronousInterrupt() {
HAL_CleanInterrupts(m_handle);
}
inline SynchronousInterrupt::WaitResult operator|(
SynchronousInterrupt::WaitResult lhs,
SynchronousInterrupt::WaitResult rhs) {

View File

@@ -94,8 +94,6 @@ TimedRobot::~TimedRobot() {
HAL_StopNotifier(m_notifier, &status);
FRC_ReportError(status, "StopNotifier");
HAL_CleanNotifier(m_notifier, &status);
}
void TimedRobot::AddPeriodic(std::function<void()> callback,

View File

@@ -65,7 +65,7 @@ Watchdog::Impl::~Impl() {
m_thread.join();
}
HAL_CleanNotifier(handle, &status);
HAL_CleanNotifier(handle);
}
void Watchdog::Impl::UpdateAlarm() {

View File

@@ -61,11 +61,6 @@ ExternalDirectionCounter::ExternalDirectionCounter(
wpi::SendableRegistry::AddLW(this, "External Direction Counter", m_index);
}
ExternalDirectionCounter::~ExternalDirectionCounter() {
int32_t status = 0;
HAL_FreeCounter(m_handle, &status);
}
int ExternalDirectionCounter::GetCount() const {
int32_t status = 0;
int val = HAL_GetCounter(m_handle, &status);

View File

@@ -37,11 +37,6 @@ Tachometer::Tachometer(std::shared_ptr<DigitalSource> source) {
wpi::SendableRegistry::AddLW(this, "Tachometer", m_index);
}
Tachometer::~Tachometer() {
int32_t status = 0;
HAL_FreeCounter(m_handle, &status);
}
units::hertz_t Tachometer::GetFrequency() const {
auto period = GetPeriod();
if (period.value() == 0) {

View File

@@ -55,11 +55,6 @@ UpDownCounter::UpDownCounter(std::shared_ptr<DigitalSource> upSource,
wpi::SendableRegistry::AddLW(this, "UpDown Counter", m_index);
}
UpDownCounter::~UpDownCounter() {
int32_t status = 0;
HAL_FreeCounter(m_handle, &status);
}
int UpDownCounter::GetCount() const {
int32_t status = 0;
int val = HAL_GetCounter(m_handle, &status);