From 496e7c1bba01eeab7e312c1f2757692ec38671ad Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Sat, 7 Sep 2024 13:58:15 -0400 Subject: [PATCH] [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. --- .../main/native/include/LifetimeWrappers.h | 183 ++++-------------- hal/src/main/native/athena/AnalogTrigger.cpp | 3 +- hal/src/main/native/athena/Counter.cpp | 2 +- hal/src/main/native/athena/DIO.cpp | 2 +- hal/src/main/native/athena/Encoder.cpp | 8 +- hal/src/main/native/athena/FPGAEncoder.cpp | 3 +- hal/src/main/native/athena/FPGAEncoder.h | 3 +- hal/src/main/native/athena/Notifier.cpp | 6 +- hal/src/main/native/athena/PWM.cpp | 14 +- hal/src/main/native/athena/SerialPort.cpp | 2 +- .../main/native/cpp/jni/AddressableLEDJNI.cpp | 4 +- hal/src/main/native/cpp/jni/AnalogGyroJNI.cpp | 4 +- hal/src/main/native/cpp/jni/AnalogJNI.cpp | 14 +- hal/src/main/native/cpp/jni/CANAPIJNI.cpp | 4 +- hal/src/main/native/cpp/jni/CTREPCMJNI.cpp | 4 +- hal/src/main/native/cpp/jni/CounterJNI.cpp | 6 +- hal/src/main/native/cpp/jni/DIOJNI.cpp | 10 +- hal/src/main/native/cpp/jni/DMAJNI.cpp | 4 +- hal/src/main/native/cpp/jni/DutyCycleJNI.cpp | 4 +- hal/src/main/native/cpp/jni/EncoderJNI.cpp | 6 +- hal/src/main/native/cpp/jni/InterruptJNI.cpp | 4 +- hal/src/main/native/cpp/jni/NotifierJNI.cpp | 6 +- hal/src/main/native/cpp/jni/PWMJNI.cpp | 6 +- .../native/cpp/jni/PowerDistributionJNI.cpp | 4 +- hal/src/main/native/cpp/jni/RelayJNI.cpp | 4 +- hal/src/main/native/cpp/jni/SPIJNI.cpp | 4 +- hal/src/main/native/cpp/jni/SerialPortJNI.cpp | 6 +- hal/src/main/native/cpp/jni/SimDeviceJNI.cpp | 4 +- .../main/native/include/hal/AnalogTrigger.h | 4 +- hal/src/main/native/include/hal/Counter.h | 3 +- hal/src/main/native/include/hal/DIO.h | 3 +- hal/src/main/native/include/hal/Encoder.h | 3 +- hal/src/main/native/include/hal/I2CTypes.h | 2 +- hal/src/main/native/include/hal/Notifier.h | 3 +- hal/src/main/native/include/hal/PWM.h | 3 +- hal/src/main/native/include/hal/SPITypes.h | 2 +- hal/src/main/native/include/hal/SerialPort.h | 3 +- hal/src/main/native/include/hal/Types.h | 26 ++- hal/src/main/native/sim/AnalogTrigger.cpp | 3 +- hal/src/main/native/sim/Counter.cpp | 2 +- hal/src/main/native/sim/DIO.cpp | 2 +- hal/src/main/native/sim/Encoder.cpp | 2 +- hal/src/main/native/sim/Notifier.cpp | 2 +- hal/src/main/native/sim/PWM.cpp | 3 +- hal/src/main/native/sim/SerialPort.cpp | 2 +- .../src/main/native/cpp/AddressableLED.cpp | 9 +- wpilibc/src/main/native/cpp/AnalogGyro.cpp | 4 - wpilibc/src/main/native/cpp/AnalogInput.cpp | 4 - wpilibc/src/main/native/cpp/AnalogOutput.cpp | 4 - wpilibc/src/main/native/cpp/AnalogTrigger.cpp | 6 - wpilibc/src/main/native/cpp/CAN.cpp | 7 - wpilibc/src/main/native/cpp/Counter.cpp | 4 - wpilibc/src/main/native/cpp/DMA.cpp | 4 - wpilibc/src/main/native/cpp/DigitalInput.cpp | 4 - wpilibc/src/main/native/cpp/DigitalOutput.cpp | 5 +- wpilibc/src/main/native/cpp/DutyCycle.cpp | 4 - wpilibc/src/main/native/cpp/Encoder.cpp | 6 - wpilibc/src/main/native/cpp/I2C.cpp | 4 - wpilibc/src/main/native/cpp/Notifier.cpp | 2 +- wpilibc/src/main/native/cpp/PWM.cpp | 4 - .../src/main/native/cpp/PowerDistribution.cpp | 7 - wpilibc/src/main/native/cpp/Relay.cpp | 7 - wpilibc/src/main/native/cpp/SPI.cpp | 4 +- wpilibc/src/main/native/cpp/SerialPort.cpp | 6 - .../main/native/cpp/SynchronousInterrupt.cpp | 4 - wpilibc/src/main/native/cpp/TimedRobot.cpp | 2 - wpilibc/src/main/native/cpp/Watchdog.cpp | 2 +- .../cpp/counter/ExternalDirectionCounter.cpp | 5 - .../main/native/cpp/counter/Tachometer.cpp | 5 - .../main/native/cpp/counter/UpDownCounter.cpp | 5 - .../main/native/include/frc/AddressableLED.h | 8 +- .../src/main/native/include/frc/AnalogGyro.h | 7 +- .../src/main/native/include/frc/AnalogInput.h | 7 +- .../main/native/include/frc/AnalogOutput.h | 7 +- .../main/native/include/frc/AnalogTrigger.h | 5 +- wpilibc/src/main/native/include/frc/CAN.h | 9 +- wpilibc/src/main/native/include/frc/Counter.h | 7 +- wpilibc/src/main/native/include/frc/DMA.h | 5 +- .../main/native/include/frc/DigitalInput.h | 7 +- .../main/native/include/frc/DigitalOutput.h | 7 +- .../src/main/native/include/frc/DutyCycle.h | 11 +- wpilibc/src/main/native/include/frc/Encoder.h | 7 +- wpilibc/src/main/native/include/frc/I2C.h | 2 - wpilibc/src/main/native/include/frc/PWM.h | 9 +- .../native/include/frc/PowerDistribution.h | 6 +- wpilibc/src/main/native/include/frc/Relay.h | 11 +- wpilibc/src/main/native/include/frc/SPI.h | 4 +- .../src/main/native/include/frc/SerialPort.h | 5 +- .../native/include/frc/SynchronousInterrupt.h | 5 +- .../src/main/native/include/frc/TimedRobot.h | 7 +- .../frc/counter/ExternalDirectionCounter.h | 7 +- .../native/include/frc/counter/Tachometer.h | 7 +- .../include/frc/counter/UpDownCounter.h | 7 +- .../src/main/cpp/examples/HAL/c/Robot.c | 5 +- 94 files changed, 247 insertions(+), 425 deletions(-) diff --git a/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h b/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h index 7546bfa0595..09a2f7b904c 100644 --- a/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h +++ b/crossConnIntegrationTests/src/main/native/include/LifetimeWrappers.h @@ -10,184 +10,69 @@ namespace hlt { -struct InterruptHandle { +struct InterruptHandle : hal::Handle { public: - explicit InterruptHandle(int32_t* status) { - handle = HAL_InitializeInterrupts(status); - } - InterruptHandle(const InterruptHandle&) = delete; - InterruptHandle operator=(const InterruptHandle&) = delete; - - InterruptHandle(InterruptHandle&&) = default; - InterruptHandle& operator=(InterruptHandle&&) = default; - - ~InterruptHandle() { HAL_CleanInterrupts(handle); } - - operator HAL_InterruptHandle() const { return handle; } - - private: - HAL_InterruptHandle handle = 0; + explicit InterruptHandle(int32_t* status) + : Handle{HAL_InitializeInterrupts(status)} {} }; -struct DMAHandle { +struct DMAHandle : hal::Handle { public: - explicit DMAHandle(int32_t* status) { handle = HAL_InitializeDMA(status); } - DMAHandle(const DMAHandle&) = delete; - DMAHandle operator=(const DMAHandle&) = delete; - - DMAHandle(DMAHandle&&) = default; - DMAHandle& operator=(DMAHandle&&) = default; - - ~DMAHandle() { HAL_FreeDMA(handle); } - - operator HAL_DMAHandle() const { return handle; } - - private: - HAL_DMAHandle handle = 0; + explicit DMAHandle(int32_t* status) : Handle{HAL_InitializeDMA(status)} {} }; -struct AnalogInputHandle { +struct AnalogInputHandle + : hal::Handle { public: - AnalogInputHandle(int32_t port, int32_t* status) { - handle = HAL_InitializeAnalogInputPort(HAL_GetPort(port), nullptr, status); - } - AnalogInputHandle(const AnalogInputHandle&) = delete; - AnalogInputHandle operator=(const AnalogInputHandle&) = delete; - - AnalogInputHandle(AnalogInputHandle&&) = default; - AnalogInputHandle& operator=(AnalogInputHandle&&) = default; - - ~AnalogInputHandle() { HAL_FreeAnalogInputPort(handle); } - - operator HAL_AnalogInputHandle() const { return handle; } - - private: - HAL_AnalogInputHandle handle = 0; + AnalogInputHandle(int32_t port, int32_t* status) + : Handle{HAL_InitializeAnalogInputPort(HAL_GetPort(port), nullptr, + status)} {} }; -struct AnalogOutputHandle { +struct AnalogOutputHandle + : hal::Handle { public: - AnalogOutputHandle(int32_t port, int32_t* status) { - handle = HAL_InitializeAnalogOutputPort(HAL_GetPort(port), nullptr, status); - } - AnalogOutputHandle(const AnalogOutputHandle&) = delete; - AnalogOutputHandle operator=(const AnalogOutputHandle&) = delete; - - AnalogOutputHandle(AnalogOutputHandle&&) = default; - AnalogOutputHandle& operator=(AnalogOutputHandle&&) = default; - - ~AnalogOutputHandle() { HAL_FreeAnalogOutputPort(handle); } - - operator HAL_AnalogOutputHandle() const { return handle; } - - private: - HAL_AnalogOutputHandle handle = 0; + AnalogOutputHandle(int32_t port, int32_t* status) + : Handle{HAL_InitializeAnalogOutputPort(HAL_GetPort(port), nullptr, + status)} {} }; -struct AnalogTriggerHandle { +struct AnalogTriggerHandle + : hal::Handle { public: - explicit AnalogTriggerHandle(HAL_AnalogInputHandle port, int32_t* status) { - handle = HAL_InitializeAnalogTrigger(port, status); - } - AnalogTriggerHandle(const AnalogTriggerHandle&) = delete; - AnalogTriggerHandle operator=(const AnalogTriggerHandle&) = delete; - - AnalogTriggerHandle(AnalogTriggerHandle&&) = default; - AnalogTriggerHandle& operator=(AnalogTriggerHandle&&) = default; - - ~AnalogTriggerHandle() { - int32_t status = 0; - HAL_CleanAnalogTrigger(handle, &status); - } - - operator HAL_AnalogTriggerHandle() const { return handle; } - - private: - HAL_AnalogTriggerHandle handle = 0; + explicit AnalogTriggerHandle(HAL_AnalogInputHandle port, int32_t* status) + : Handle{HAL_InitializeAnalogTrigger(port, status)} {} }; -struct DutyCycleHandle { +struct DutyCycleHandle : hal::Handle { public: - DutyCycleHandle(HAL_DigitalHandle port, int32_t* status) { - handle = HAL_InitializeDutyCycle( - port, HAL_AnalogTriggerType::HAL_Trigger_kInWindow, status); - } - DutyCycleHandle(const DutyCycleHandle&) = delete; - DutyCycleHandle operator=(const DutyCycleHandle&) = delete; - - DutyCycleHandle(DutyCycleHandle&&) = default; - DutyCycleHandle& operator=(DutyCycleHandle&&) = default; - - ~DutyCycleHandle() { HAL_FreeDutyCycle(handle); } - - operator HAL_DutyCycleHandle() const { return handle; } - - private: - HAL_DutyCycleHandle handle = 0; + DutyCycleHandle(HAL_DigitalHandle port, int32_t* status) + : Handle{HAL_InitializeDutyCycle( + port, HAL_AnalogTriggerType::HAL_Trigger_kInWindow, status)} {} }; -struct DIOHandle { +struct DIOHandle : hal::Handle { public: DIOHandle() {} - DIOHandle(const DIOHandle&) = delete; - DIOHandle operator=(const DIOHandle&) = delete; - - DIOHandle(DIOHandle&&) = default; - DIOHandle& operator=(DIOHandle&&) = default; - - DIOHandle(int32_t port, HAL_Bool input, int32_t* status) { - handle = HAL_InitializeDIOPort(HAL_GetPort(port), input, nullptr, status); - } - - ~DIOHandle() { HAL_FreeDIOPort(handle); } - operator HAL_DigitalHandle() const { return handle; } - - private: - HAL_DigitalHandle handle = 0; + DIOHandle(int32_t port, HAL_Bool input, int32_t* status) + : Handle{ + HAL_InitializeDIOPort(HAL_GetPort(port), input, nullptr, status)} {} }; -struct PWMHandle { +struct PWMHandle : hal::Handle { public: PWMHandle() {} - PWMHandle(const PWMHandle&) = delete; - PWMHandle operator=(const PWMHandle&) = delete; - - PWMHandle(PWMHandle&&) = default; - PWMHandle& operator=(PWMHandle&&) = default; - - PWMHandle(int32_t port, int32_t* status) { - handle = HAL_InitializePWMPort(HAL_GetPort(port), nullptr, status); - } - - ~PWMHandle() { - int32_t status = 0; - HAL_FreePWMPort(handle, &status); - } - - operator HAL_DigitalHandle() const { return handle; } - private: - HAL_DigitalHandle handle = 0; + PWMHandle(int32_t port, int32_t* status) + : Handle{HAL_InitializePWMPort(HAL_GetPort(port), nullptr, status)} {} }; -struct RelayHandle { +struct RelayHandle : hal::Handle { public: - RelayHandle(int32_t port, HAL_Bool fwd, int32_t* status) { - handle = HAL_InitializeRelayPort(HAL_GetPort(port), fwd, nullptr, status); - } - RelayHandle(const RelayHandle&) = delete; - RelayHandle operator=(const RelayHandle&) = delete; - - RelayHandle(RelayHandle&&) = default; - RelayHandle& operator=(RelayHandle&&) = default; - - ~RelayHandle() { HAL_FreeRelayPort(handle); } - - operator HAL_RelayHandle() const { return handle; } - - private: - HAL_RelayHandle handle = 0; + RelayHandle(int32_t port, HAL_Bool fwd, int32_t* status) + : Handle{ + HAL_InitializeRelayPort(HAL_GetPort(port), fwd, nullptr, status)} {} }; #define ASSERT_LAST_ERROR_STATUS(status, x) \ diff --git a/hal/src/main/native/athena/AnalogTrigger.cpp b/hal/src/main/native/athena/AnalogTrigger.cpp index 47ef4f2fc29..2e72a43a395 100644 --- a/hal/src/main/native/athena/AnalogTrigger.cpp +++ b/hal/src/main/native/athena/AnalogTrigger.cpp @@ -99,8 +99,7 @@ HAL_AnalogTriggerHandle HAL_InitializeAnalogTriggerDutyCycle( return handle; } -void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle, - int32_t* status) { +void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle) { analogTriggerHandles->Free(analogTriggerHandle); // caller owns the input handle. } diff --git a/hal/src/main/native/athena/Counter.cpp b/hal/src/main/native/athena/Counter.cpp index b0c96a26ff1..bb8da37098e 100644 --- a/hal/src/main/native/athena/Counter.cpp +++ b/hal/src/main/native/athena/Counter.cpp @@ -63,7 +63,7 @@ HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index, return handle; } -void HAL_FreeCounter(HAL_CounterHandle counterHandle, int32_t* status) { +void HAL_FreeCounter(HAL_CounterHandle counterHandle) { counterHandles->Free(counterHandle); } diff --git a/hal/src/main/native/athena/DIO.cpp b/hal/src/main/native/athena/DIO.cpp index d75f32b9c9f..00d37cd6c52 100644 --- a/hal/src/main/native/athena/DIO.cpp +++ b/hal/src/main/native/athena/DIO.cpp @@ -188,7 +188,7 @@ HAL_DigitalPWMHandle HAL_AllocateDigitalPWM(int32_t* status) { return handle; } -void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator, int32_t* status) { +void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator) { digitalPWMHandles->Free(pwmGenerator); } diff --git a/hal/src/main/native/athena/Encoder.cpp b/hal/src/main/native/athena/Encoder.cpp index 6edfea3e1e0..1f50eb8b244 100644 --- a/hal/src/main/native/athena/Encoder.cpp +++ b/hal/src/main/native/athena/Encoder.cpp @@ -94,11 +94,9 @@ void Encoder::SetupCounter(HAL_Handle digitalSourceHandleA, Encoder::~Encoder() { if (m_counter != HAL_kInvalidHandle) { - int32_t status = 0; - HAL_FreeCounter(m_counter, &status); + HAL_FreeCounter(m_counter); } else { - int32_t status = 0; - HAL_FreeFPGAEncoder(m_encoder, &status); + HAL_FreeFPGAEncoder(m_encoder); } } @@ -285,7 +283,7 @@ HAL_EncoderHandle HAL_InitializeEncoder( return handle; } -void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) { +void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle) { encoderHandles->Free(encoderHandle); } diff --git a/hal/src/main/native/athena/FPGAEncoder.cpp b/hal/src/main/native/athena/FPGAEncoder.cpp index c73667454b2..a0f4c289786 100644 --- a/hal/src/main/native/athena/FPGAEncoder.cpp +++ b/hal/src/main/native/athena/FPGAEncoder.cpp @@ -96,8 +96,7 @@ HAL_FPGAEncoderHandle HAL_InitializeFPGAEncoder( return handle; } -void HAL_FreeFPGAEncoder(HAL_FPGAEncoderHandle fpgaEncoderHandle, - int32_t* status) { +void HAL_FreeFPGAEncoder(HAL_FPGAEncoderHandle fpgaEncoderHandle) { fpgaEncoderHandles->Free(fpgaEncoderHandle); } diff --git a/hal/src/main/native/athena/FPGAEncoder.h b/hal/src/main/native/athena/FPGAEncoder.h index a819d96b8a1..e0d296e6603 100644 --- a/hal/src/main/native/athena/FPGAEncoder.h +++ b/hal/src/main/native/athena/FPGAEncoder.h @@ -15,8 +15,7 @@ HAL_FPGAEncoderHandle HAL_InitializeFPGAEncoder( HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA, HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB, HAL_Bool reverseDirection, int32_t* index, int32_t* status); -void HAL_FreeFPGAEncoder(HAL_FPGAEncoderHandle fpgaEncoderHandle, - int32_t* status); +void HAL_FreeFPGAEncoder(HAL_FPGAEncoderHandle fpgaEncoderHandle); /** * Reset the Encoder distance to zero. diff --git a/hal/src/main/native/athena/Notifier.cpp b/hal/src/main/native/athena/Notifier.cpp index 29f5806c287..f62e310843f 100644 --- a/hal/src/main/native/athena/Notifier.cpp +++ b/hal/src/main/native/athena/Notifier.cpp @@ -216,7 +216,7 @@ void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { notifier->cond.notify_all(); // wake up any waiting threads } -void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { +void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle) { auto notifier = notifierHandles->Free(notifierHandle); if (!notifier) { return; @@ -236,9 +236,9 @@ void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { // the notifier can call back into our callback, so don't hold the lock // here (the atomic fetch_sub will prevent multiple parallel entries // into this function) - + int32_t status = 0; if (notifierAlarm) { - notifierAlarm->writeEnable(false, status); + notifierAlarm->writeEnable(false, &status); } notifierRunning = false; hal::ReleaseFPGAInterrupt(kTimerInterruptNumber); diff --git a/hal/src/main/native/athena/PWM.cpp b/hal/src/main/native/athena/PWM.cpp index bb6f7dd0ddd..e891d12e9cc 100644 --- a/hal/src/main/native/athena/PWM.cpp +++ b/hal/src/main/native/athena/PWM.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "ConstantsInternal.h" #include "DigitalInternal.h" @@ -126,10 +127,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle, return handle; } -void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) { +void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) { auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); if (port == nullptr) { - *status = HAL_HANDLE_ERROR; return; } @@ -148,11 +148,17 @@ void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) { } if (port->channel > tPWM::kNumHdrRegisters - 1) { + int32_t status = 0; int32_t bitToUnset = 1 << remapMXPPWMChannel(port->channel); uint16_t specialFunctions = - digitalSystem->readEnableMXPSpecialFunction(status); + digitalSystem->readEnableMXPSpecialFunction(&status); digitalSystem->writeEnableMXPSpecialFunction(specialFunctions & ~bitToUnset, - status); + &status); + if (status != 0) { + wpi::println( + "HAL_FreePWMPort: failed to free MXP PWM port {}, status code {}", + port->channel, status); + } } } diff --git a/hal/src/main/native/athena/SerialPort.cpp b/hal/src/main/native/athena/SerialPort.cpp index 9a25d879c31..04ee21336c1 100644 --- a/hal/src/main/native/athena/SerialPort.cpp +++ b/hal/src/main/native/athena/SerialPort.cpp @@ -141,7 +141,7 @@ HAL_SerialPortHandle HAL_InitializeSerialPortDirect(HAL_SerialPort port, return handle; } -void HAL_CloseSerial(HAL_SerialPortHandle handle, int32_t* status) { +void HAL_CloseSerial(HAL_SerialPortHandle handle) { auto port = serialPortHandles->Get(handle); serialPortHandles->Free(handle); diff --git a/hal/src/main/native/cpp/jni/AddressableLEDJNI.cpp b/hal/src/main/native/cpp/jni/AddressableLEDJNI.cpp index 66e3ed09c77..1d9017da1c8 100644 --- a/hal/src/main/native/cpp/jni/AddressableLEDJNI.cpp +++ b/hal/src/main/native/cpp/jni/AddressableLEDJNI.cpp @@ -41,7 +41,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_AddressableLEDJNI_free (JNIEnv* env, jclass, jint handle) { - HAL_FreeAddressableLED(static_cast(handle)); + if (handle != HAL_kInvalidHandle) { + HAL_FreeAddressableLED(static_cast(handle)); + } } /* diff --git a/hal/src/main/native/cpp/jni/AnalogGyroJNI.cpp b/hal/src/main/native/cpp/jni/AnalogGyroJNI.cpp index 154301f8e79..f630c29da38 100644 --- a/hal/src/main/native/cpp/jni/AnalogGyroJNI.cpp +++ b/hal/src/main/native/cpp/jni/AnalogGyroJNI.cpp @@ -57,7 +57,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_AnalogGyroJNI_freeAnalogGyro (JNIEnv* env, jclass, jint id) { - HAL_FreeAnalogGyro((HAL_GyroHandle)id); + if (id != HAL_kInvalidHandle) { + HAL_FreeAnalogGyro((HAL_GyroHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/AnalogJNI.cpp b/hal/src/main/native/cpp/jni/AnalogJNI.cpp index a2cccb67e98..24d48f39c84 100644 --- a/hal/src/main/native/cpp/jni/AnalogJNI.cpp +++ b/hal/src/main/native/cpp/jni/AnalogJNI.cpp @@ -47,7 +47,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_AnalogJNI_freeAnalogInputPort (JNIEnv* env, jclass, jint id) { - HAL_FreeAnalogInputPort((HAL_AnalogInputHandle)id); + if (id != HAL_kInvalidHandle) { + HAL_FreeAnalogInputPort((HAL_AnalogInputHandle)id); + } } /* @@ -76,7 +78,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_AnalogJNI_freeAnalogOutputPort (JNIEnv* env, jclass, jint id) { - HAL_FreeAnalogOutputPort((HAL_AnalogOutputHandle)id); + if (id != HAL_kInvalidHandle) { + HAL_FreeAnalogOutputPort((HAL_AnalogOutputHandle)id); + } } /* @@ -540,9 +544,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_AnalogJNI_cleanAnalogTrigger (JNIEnv* env, jclass, jint id) { - int32_t status = 0; - HAL_CleanAnalogTrigger((HAL_AnalogTriggerHandle)id, &status); - CheckStatus(env, status); + if (id != HAL_kInvalidHandle) { + HAL_CleanAnalogTrigger((HAL_AnalogTriggerHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/CANAPIJNI.cpp b/hal/src/main/native/cpp/jni/CANAPIJNI.cpp index 929f7da1efe..33b3a5f420b 100644 --- a/hal/src/main/native/cpp/jni/CANAPIJNI.cpp +++ b/hal/src/main/native/cpp/jni/CANAPIJNI.cpp @@ -58,7 +58,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_CANAPIJNI_cleanCAN (JNIEnv* env, jclass, jint handle) { - HAL_CleanCAN(static_cast(handle)); + if (handle != HAL_kInvalidHandle) { + HAL_CleanCAN(static_cast(handle)); + } } /* diff --git a/hal/src/main/native/cpp/jni/CTREPCMJNI.cpp b/hal/src/main/native/cpp/jni/CTREPCMJNI.cpp index 4d22f664b14..8cc4e7db06b 100644 --- a/hal/src/main/native/cpp/jni/CTREPCMJNI.cpp +++ b/hal/src/main/native/cpp/jni/CTREPCMJNI.cpp @@ -41,7 +41,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_CTREPCMJNI_free (JNIEnv* env, jclass, jint handle) { - HAL_FreeCTREPCM(handle); + if (handle != HAL_kInvalidHandle) { + HAL_FreeCTREPCM(handle); + } } /* diff --git a/hal/src/main/native/cpp/jni/CounterJNI.cpp b/hal/src/main/native/cpp/jni/CounterJNI.cpp index 6fc12df8032..225e3e0ba82 100644 --- a/hal/src/main/native/cpp/jni/CounterJNI.cpp +++ b/hal/src/main/native/cpp/jni/CounterJNI.cpp @@ -53,9 +53,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_CounterJNI_freeCounter (JNIEnv* env, jclass, jint id) { - int32_t status = 0; - HAL_FreeCounter((HAL_CounterHandle)id, &status); - CheckStatus(env, status); + if (id != HAL_kInvalidHandle) { + HAL_FreeCounter((HAL_CounterHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/DIOJNI.cpp b/hal/src/main/native/cpp/jni/DIOJNI.cpp index dee34c7f46c..788fe3414eb 100644 --- a/hal/src/main/native/cpp/jni/DIOJNI.cpp +++ b/hal/src/main/native/cpp/jni/DIOJNI.cpp @@ -57,7 +57,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_DIOJNI_freeDIOPort (JNIEnv* env, jclass, jint id) { - HAL_FreeDIOPort((HAL_DigitalHandle)id); + if (id != HAL_kInvalidHandle) { + HAL_FreeDIOPort((HAL_DigitalHandle)id); + } } /* @@ -227,9 +229,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_DIOJNI_freeDigitalPWM (JNIEnv* env, jclass, jint id) { - int32_t status = 0; - HAL_FreeDigitalPWM((HAL_DigitalPWMHandle)id, &status); - CheckStatus(env, status); + if (id != HAL_kInvalidHandle) { + HAL_FreeDigitalPWM((HAL_DigitalPWMHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/DMAJNI.cpp b/hal/src/main/native/cpp/jni/DMAJNI.cpp index 79dc14b0244..77900e304ab 100644 --- a/hal/src/main/native/cpp/jni/DMAJNI.cpp +++ b/hal/src/main/native/cpp/jni/DMAJNI.cpp @@ -46,7 +46,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_DMAJNI_free (JNIEnv* env, jclass, jint handle) { - HAL_FreeDMA(handle); + if (handle != HAL_kInvalidHandle) { + HAL_FreeDMA(handle); + } } /* diff --git a/hal/src/main/native/cpp/jni/DutyCycleJNI.cpp b/hal/src/main/native/cpp/jni/DutyCycleJNI.cpp index f83e13c82c3..e9a186b349a 100644 --- a/hal/src/main/native/cpp/jni/DutyCycleJNI.cpp +++ b/hal/src/main/native/cpp/jni/DutyCycleJNI.cpp @@ -37,7 +37,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_DutyCycleJNI_free (JNIEnv*, jclass, jint handle) { - HAL_FreeDutyCycle(static_cast(handle)); + if (handle != HAL_kInvalidHandle) { + HAL_FreeDutyCycle(static_cast(handle)); + } } /* diff --git a/hal/src/main/native/cpp/jni/EncoderJNI.cpp b/hal/src/main/native/cpp/jni/EncoderJNI.cpp index f93e064ac48..e15c36f8484 100644 --- a/hal/src/main/native/cpp/jni/EncoderJNI.cpp +++ b/hal/src/main/native/cpp/jni/EncoderJNI.cpp @@ -46,9 +46,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_EncoderJNI_freeEncoder (JNIEnv* env, jclass, jint id) { - int32_t status = 0; - HAL_FreeEncoder((HAL_EncoderHandle)id, &status); - CheckStatus(env, status); + if (id != HAL_kInvalidHandle) { + HAL_FreeEncoder((HAL_EncoderHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/InterruptJNI.cpp b/hal/src/main/native/cpp/jni/InterruptJNI.cpp index e2201258a02..3654cb8ac10 100644 --- a/hal/src/main/native/cpp/jni/InterruptJNI.cpp +++ b/hal/src/main/native/cpp/jni/InterruptJNI.cpp @@ -44,7 +44,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_InterruptJNI_cleanInterrupts (JNIEnv* env, jclass, jint interruptHandle) { - HAL_CleanInterrupts((HAL_InterruptHandle)interruptHandle); + if (interruptHandle != HAL_kInvalidHandle) { + HAL_CleanInterrupts((HAL_InterruptHandle)interruptHandle); + } } /* diff --git a/hal/src/main/native/cpp/jni/NotifierJNI.cpp b/hal/src/main/native/cpp/jni/NotifierJNI.cpp index 4e7069e6946..d4e4ec66307 100644 --- a/hal/src/main/native/cpp/jni/NotifierJNI.cpp +++ b/hal/src/main/native/cpp/jni/NotifierJNI.cpp @@ -87,9 +87,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_NotifierJNI_cleanNotifier (JNIEnv* env, jclass, jint notifierHandle) { - int32_t status = 0; - HAL_CleanNotifier((HAL_NotifierHandle)notifierHandle, &status); - CheckStatus(env, status); + if (notifierHandle != HAL_kInvalidHandle) { + HAL_CleanNotifier((HAL_NotifierHandle)notifierHandle); + } } /* diff --git a/hal/src/main/native/cpp/jni/PWMJNI.cpp b/hal/src/main/native/cpp/jni/PWMJNI.cpp index 2f90ded87dc..b7a8b1e1022 100644 --- a/hal/src/main/native/cpp/jni/PWMJNI.cpp +++ b/hal/src/main/native/cpp/jni/PWMJNI.cpp @@ -56,9 +56,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_PWMJNI_freePWMPort (JNIEnv* env, jclass, jint id) { - int32_t status = 0; - HAL_FreePWMPort((HAL_DigitalHandle)id, &status); - CheckStatus(env, status); + if (id != HAL_kInvalidHandle) { + HAL_FreePWMPort((HAL_DigitalHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp index 1d505283400..1fc245c8e8c 100644 --- a/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp +++ b/hal/src/main/native/cpp/jni/PowerDistributionJNI.cpp @@ -51,7 +51,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_PowerDistributionJNI_free (JNIEnv*, jclass, jint handle) { - HAL_CleanPowerDistribution(handle); + if (handle != HAL_kInvalidHandle) { + HAL_CleanPowerDistribution(handle); + } } /* diff --git a/hal/src/main/native/cpp/jni/RelayJNI.cpp b/hal/src/main/native/cpp/jni/RelayJNI.cpp index 9e31a9da10c..c388999e607 100644 --- a/hal/src/main/native/cpp/jni/RelayJNI.cpp +++ b/hal/src/main/native/cpp/jni/RelayJNI.cpp @@ -44,7 +44,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_RelayJNI_freeRelayPort (JNIEnv* env, jclass, jint id) { - HAL_FreeRelayPort((HAL_RelayHandle)id); + if (id != HAL_kInvalidHandle) { + HAL_FreeRelayPort((HAL_RelayHandle)id); + } } /* diff --git a/hal/src/main/native/cpp/jni/SPIJNI.cpp b/hal/src/main/native/cpp/jni/SPIJNI.cpp index f8c5f7e3c38..0940de6da73 100644 --- a/hal/src/main/native/cpp/jni/SPIJNI.cpp +++ b/hal/src/main/native/cpp/jni/SPIJNI.cpp @@ -298,7 +298,9 @@ Java_edu_wpi_first_hal_SPIJNI_spiFreeAuto (JNIEnv* env, jclass, jint port) { int32_t status = 0; - HAL_FreeSPIAuto(static_cast(port), &status); + if (port != HAL_kInvalidHandle) { + HAL_FreeSPIAuto(static_cast(port), &status); + } CheckStatus(env, status); } diff --git a/hal/src/main/native/cpp/jni/SerialPortJNI.cpp b/hal/src/main/native/cpp/jni/SerialPortJNI.cpp index 281e8ef4592..e141e249f57 100644 --- a/hal/src/main/native/cpp/jni/SerialPortJNI.cpp +++ b/hal/src/main/native/cpp/jni/SerialPortJNI.cpp @@ -306,9 +306,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_SerialPortJNI_serialClose (JNIEnv* env, jclass, jint handle) { - int32_t status = 0; - HAL_CloseSerial(static_cast(handle), &status); - CheckStatus(env, status); + if (handle != HAL_kInvalidHandle) { + HAL_CloseSerial(static_cast(handle)); + } } } // extern "C" diff --git a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp index 2cb6c9c7625..d489bdd36e3 100644 --- a/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp +++ b/hal/src/main/native/cpp/jni/SimDeviceJNI.cpp @@ -60,7 +60,9 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_hal_SimDeviceJNI_freeSimDevice (JNIEnv*, jclass, jint handle) { - HAL_FreeSimDevice(handle); + if (handle != HAL_kInvalidHandle) { + HAL_FreeSimDevice(handle); + } } /* diff --git a/hal/src/main/native/include/hal/AnalogTrigger.h b/hal/src/main/native/include/hal/AnalogTrigger.h index 1a35eea4aa1..8c470f12a01 100644 --- a/hal/src/main/native/include/hal/AnalogTrigger.h +++ b/hal/src/main/native/include/hal/AnalogTrigger.h @@ -52,10 +52,8 @@ HAL_AnalogTriggerHandle HAL_InitializeAnalogTriggerDutyCycle( * Frees an analog trigger. * * @param[in] analogTriggerHandle the trigger handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle, - int32_t* status); +void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle); /** * Sets the raw ADC upper and lower limits of the analog trigger. diff --git a/hal/src/main/native/include/hal/Counter.h b/hal/src/main/native/include/hal/Counter.h index 246562c207d..4d6daea2f1b 100644 --- a/hal/src/main/native/include/hal/Counter.h +++ b/hal/src/main/native/include/hal/Counter.h @@ -48,9 +48,8 @@ HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index, * Frees a counter. * * @param[in] counterHandle the counter handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_FreeCounter(HAL_CounterHandle counterHandle, int32_t* status); +void HAL_FreeCounter(HAL_CounterHandle counterHandle); /** * Sets the average sample size of a counter. diff --git a/hal/src/main/native/include/hal/DIO.h b/hal/src/main/native/include/hal/DIO.h index f5313843e8d..b81044cde83 100644 --- a/hal/src/main/native/include/hal/DIO.h +++ b/hal/src/main/native/include/hal/DIO.h @@ -68,9 +68,8 @@ HAL_DigitalPWMHandle HAL_AllocateDigitalPWM(int32_t* status); * Frees the resource associated with a DO PWM generator. * * @param[in] pwmGenerator the digital PWM handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator, int32_t* status); +void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator); /** * Changes the frequency of the DO PWM generator. diff --git a/hal/src/main/native/include/hal/Encoder.h b/hal/src/main/native/include/hal/Encoder.h index 451f1aafcbe..749c282c1b4 100644 --- a/hal/src/main/native/include/hal/Encoder.h +++ b/hal/src/main/native/include/hal/Encoder.h @@ -65,9 +65,8 @@ HAL_EncoderHandle HAL_InitializeEncoder( * Frees an encoder. * * @param[in] encoderHandle the encoder handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status); +void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle); /** * Indicates the encoder is used by a simulated device. diff --git a/hal/src/main/native/include/hal/I2CTypes.h b/hal/src/main/native/include/hal/I2CTypes.h index 8c658eee228..e1339f0140a 100644 --- a/hal/src/main/native/include/hal/I2CTypes.h +++ b/hal/src/main/native/include/hal/I2CTypes.h @@ -27,7 +27,7 @@ namespace hal { * A move-only C++ wrapper around HAL_I2CPort. * Does not ensure destruction. */ -using I2CPort = Handle; +using I2CPort = Handle; } // namespace hal #endif diff --git a/hal/src/main/native/include/hal/Notifier.h b/hal/src/main/native/include/hal/Notifier.h index 9b451e46f5f..2528f712895 100644 --- a/hal/src/main/native/include/hal/Notifier.h +++ b/hal/src/main/native/include/hal/Notifier.h @@ -77,9 +77,8 @@ void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status); * Note this also stops a notifier if it is already running. * * @param[in] notifierHandle the notifier handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status); +void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle); /** * Updates the trigger time for a notifier. diff --git a/hal/src/main/native/include/hal/PWM.h b/hal/src/main/native/include/hal/PWM.h index 14566fcd7a1..ad67535e75f 100644 --- a/hal/src/main/native/include/hal/PWM.h +++ b/hal/src/main/native/include/hal/PWM.h @@ -35,9 +35,8 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle, * Frees a PWM port. * * @param[in] pwmPortHandle the pwm handle - * @param[out] status Error status variable. 0 on success. */ -void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status); +void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle); /** * Checks if a pwm channel is valid. diff --git a/hal/src/main/native/include/hal/SPITypes.h b/hal/src/main/native/include/hal/SPITypes.h index b032c0313a8..b27c38828e9 100644 --- a/hal/src/main/native/include/hal/SPITypes.h +++ b/hal/src/main/native/include/hal/SPITypes.h @@ -49,7 +49,7 @@ namespace hal { * A move-only C++ wrapper around HAL_SPIPort. * Does not ensure destruction. */ -using SPIPort = Handle; +using SPIPort = Handle; } // namespace hal #endif diff --git a/hal/src/main/native/include/hal/SerialPort.h b/hal/src/main/native/include/hal/SerialPort.h index c6b80c7b44c..16d561eb95e 100644 --- a/hal/src/main/native/include/hal/SerialPort.h +++ b/hal/src/main/native/include/hal/SerialPort.h @@ -255,9 +255,8 @@ void HAL_ClearSerial(HAL_SerialPortHandle handle, int32_t* status); * Closes a serial port. * * @param[in] handle the serial port handle to close - * @param[out] status the error code, or 0 for success */ -void HAL_CloseSerial(HAL_SerialPortHandle handle, int32_t* status); +void HAL_CloseSerial(HAL_SerialPortHandle handle); #ifdef __cplusplus } // extern "C" #endif diff --git a/hal/src/main/native/include/hal/Types.h b/hal/src/main/native/include/hal/Types.h index ac7c7481aac..1abb16e4774 100644 --- a/hal/src/main/native/include/hal/Types.h +++ b/hal/src/main/native/include/hal/Types.h @@ -87,12 +87,12 @@ typedef int32_t HAL_Bool; #ifdef __cplusplus namespace hal { - /** * A move-only C++ wrapper around a HAL handle. - * Does not ensure destruction. + * Will free the handle if FreeFunction is provided */ -template +template class Handle { public: Handle() = default; @@ -109,6 +109,26 @@ class Handle { return *this; } + ~Handle() { +// FIXME: GCC gives the false positive "the address of will never +// be NULL" because it doesn't realize the default template parameter can make +// GetDefault nullptr. Fixed in GCC 13. +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554 +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105885 +#if __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Waddress" +#endif // __GNUC__ + if constexpr (FreeFunction != nullptr) { +#if __GNUC__ +#pragma GCC diagnostic pop +#endif // __GNUC__ + if (m_handle != CInvalid) { + FreeFunction(m_handle); + } + } + } + operator CType() const { return m_handle; } // NOLINT private: diff --git a/hal/src/main/native/sim/AnalogTrigger.cpp b/hal/src/main/native/sim/AnalogTrigger.cpp index 50c4ab49282..92f0397ac4e 100644 --- a/hal/src/main/native/sim/AnalogTrigger.cpp +++ b/hal/src/main/native/sim/AnalogTrigger.cpp @@ -93,8 +93,7 @@ HAL_AnalogTriggerHandle HAL_InitializeAnalogTriggerDutyCycle( return HAL_kInvalidHandle; } -void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle, - int32_t* status) { +void HAL_CleanAnalogTrigger(HAL_AnalogTriggerHandle analogTriggerHandle) { auto trigger = analogTriggerHandles->Get(analogTriggerHandle); analogTriggerHandles->Free(analogTriggerHandle); if (trigger == nullptr) { diff --git a/hal/src/main/native/sim/Counter.cpp b/hal/src/main/native/sim/Counter.cpp index a56b646c81d..22c482c3a8b 100644 --- a/hal/src/main/native/sim/Counter.cpp +++ b/hal/src/main/native/sim/Counter.cpp @@ -31,7 +31,7 @@ HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index, hal::init::CheckInit(); return 0; } -void HAL_FreeCounter(HAL_CounterHandle counterHandle, int32_t* status) {} +void HAL_FreeCounter(HAL_CounterHandle counterHandle) {} void HAL_SetCounterAverageSize(HAL_CounterHandle counterHandle, int32_t size, int32_t* status) {} void HAL_SetCounterUpSource(HAL_CounterHandle counterHandle, diff --git a/hal/src/main/native/sim/DIO.cpp b/hal/src/main/native/sim/DIO.cpp index 5a211227672..1148b75d608 100644 --- a/hal/src/main/native/sim/DIO.cpp +++ b/hal/src/main/native/sim/DIO.cpp @@ -113,7 +113,7 @@ HAL_DigitalPWMHandle HAL_AllocateDigitalPWM(int32_t* status) { return handle; } -void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator, int32_t* status) { +void HAL_FreeDigitalPWM(HAL_DigitalPWMHandle pwmGenerator) { auto port = digitalPWMHandles->Get(pwmGenerator); digitalPWMHandles->Free(pwmGenerator); if (port == nullptr) { diff --git a/hal/src/main/native/sim/Encoder.cpp b/hal/src/main/native/sim/Encoder.cpp index 964777dece9..66ad87d2743 100644 --- a/hal/src/main/native/sim/Encoder.cpp +++ b/hal/src/main/native/sim/Encoder.cpp @@ -113,7 +113,7 @@ HAL_EncoderHandle HAL_InitializeEncoder( return handle; } -void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status) { +void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle) { auto encoder = encoderHandles->Get(encoderHandle); encoderHandles->Free(encoderHandle); if (encoder == nullptr) { diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index 3d2c802cd16..225798c0775 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -204,7 +204,7 @@ void HAL_StopNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { notifier->cond.notify_all(); } -void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { +void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle) { auto notifier = notifierHandles->Free(notifierHandle); if (!notifier) { return; diff --git a/hal/src/main/native/sim/PWM.cpp b/hal/src/main/native/sim/PWM.cpp index c668039b2ea..333cf0fc685 100644 --- a/hal/src/main/native/sim/PWM.cpp +++ b/hal/src/main/native/sim/PWM.cpp @@ -111,10 +111,9 @@ HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle, return handle; } -void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) { +void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) { auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); if (port == nullptr) { - *status = HAL_HANDLE_ERROR; return; } diff --git a/hal/src/main/native/sim/SerialPort.cpp b/hal/src/main/native/sim/SerialPort.cpp index 83ba9710a91..4712b3048c0 100644 --- a/hal/src/main/native/sim/SerialPort.cpp +++ b/hal/src/main/native/sim/SerialPort.cpp @@ -80,5 +80,5 @@ void HAL_FlushSerial(HAL_SerialPortHandle handle, int32_t* status) {} void HAL_ClearSerial(HAL_SerialPortHandle handle, int32_t* status) {} -void HAL_CloseSerial(HAL_SerialPortHandle handle, int32_t* status) {} +void HAL_CloseSerial(HAL_SerialPortHandle handle) {} } // extern "C" diff --git a/wpilibc/src/main/native/cpp/AddressableLED.cpp b/wpilibc/src/main/native/cpp/AddressableLED.cpp index 0baae3c1c8b..7c70033fe37 100644 --- a/wpilibc/src/main/native/cpp/AddressableLED.cpp +++ b/wpilibc/src/main/native/cpp/AddressableLED.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/AnalogGyro.cpp b/wpilibc/src/main/native/cpp/AnalogGyro.cpp index 8c0c4709c40..36d70b9daad 100644 --- a/wpilibc/src/main/native/cpp/AnalogGyro.cpp +++ b/wpilibc/src/main/native/cpp/AnalogGyro.cpp @@ -58,10 +58,6 @@ AnalogGyro::AnalogGyro(std::shared_ptr 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); diff --git a/wpilibc/src/main/native/cpp/AnalogInput.cpp b/wpilibc/src/main/native/cpp/AnalogInput.cpp index 2ecb2df6b93..9960d3704af 100644 --- a/wpilibc/src/main/native/cpp/AnalogInput.cpp +++ b/wpilibc/src/main/native/cpp/AnalogInput.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/AnalogOutput.cpp b/wpilibc/src/main/native/cpp/AnalogOutput.cpp index 90f538bf79b..d5857abded2 100644 --- a/wpilibc/src/main/native/cpp/AnalogOutput.cpp +++ b/wpilibc/src/main/native/cpp/AnalogOutput.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp index e3f117b8e7f..81bd3c5f8e9 100644 --- a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp +++ b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp @@ -57,12 +57,6 @@ AnalogTrigger::AnalogTrigger(std::shared_ptr 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); diff --git a/wpilibc/src/main/native/cpp/CAN.cpp b/wpilibc/src/main/native/cpp/CAN.cpp index aa0370f8611..2e7b8b747c5 100644 --- a/wpilibc/src/main/native/cpp/CAN.cpp +++ b/wpilibc/src/main/native/cpp/CAN.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/Counter.cpp b/wpilibc/src/main/native/cpp/Counter.cpp index ab69f4ffde0..f6506b98fb3 100644 --- a/wpilibc/src/main/native/cpp/Counter.cpp +++ b/wpilibc/src/main/native/cpp/Counter.cpp @@ -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) { diff --git a/wpilibc/src/main/native/cpp/DMA.cpp b/wpilibc/src/main/native/cpp/DMA.cpp index da45aa3562e..806e18b4071 100644 --- a/wpilibc/src/main/native/cpp/DMA.cpp +++ b/wpilibc/src/main/native/cpp/DMA.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/DigitalInput.cpp b/wpilibc/src/main/native/cpp/DigitalInput.cpp index e3f183dc658..b3c110875ea 100644 --- a/wpilibc/src/main/native/cpp/DigitalInput.cpp +++ b/wpilibc/src/main/native/cpp/DigitalInput.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/DigitalOutput.cpp b/wpilibc/src/main/native/cpp/DigitalOutput.cpp index 9f65faf3fca..01d3ef74fd2 100644 --- a/wpilibc/src/main/native/cpp/DigitalOutput.cpp +++ b/wpilibc/src/main/native/cpp/DigitalOutput.cpp @@ -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; } diff --git a/wpilibc/src/main/native/cpp/DutyCycle.cpp b/wpilibc/src/main/native/cpp/DutyCycle.cpp index 88f6b56a07f..943baa8b1bf 100644 --- a/wpilibc/src/main/native/cpp/DutyCycle.cpp +++ b/wpilibc/src/main/native/cpp/DutyCycle.cpp @@ -35,10 +35,6 @@ DutyCycle::DutyCycle(std::shared_ptr source) InitDutyCycle(); } -DutyCycle::~DutyCycle() { - HAL_FreeDutyCycle(m_handle); -} - void DutyCycle::InitDutyCycle() { int32_t status = 0; m_handle = diff --git a/wpilibc/src/main/native/cpp/Encoder.cpp b/wpilibc/src/main/native/cpp/Encoder.cpp index 1e15ed87bb6..795183f3ab8 100644 --- a/wpilibc/src/main/native/cpp/Encoder.cpp +++ b/wpilibc/src/main/native/cpp/Encoder.cpp @@ -59,12 +59,6 @@ Encoder::Encoder(std::shared_ptr 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); diff --git a/wpilibc/src/main/native/cpp/I2C.cpp b/wpilibc/src/main/native/cpp/I2C.cpp index dbc624dcc18..68241444e91 100644 --- a/wpilibc/src/main/native/cpp/I2C.cpp +++ b/wpilibc/src/main/native/cpp/I2C.cpp @@ -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(static_cast(m_port)); } diff --git a/wpilibc/src/main/native/cpp/Notifier.cpp b/wpilibc/src/main/native/cpp/Notifier.cpp index 81cd017c24e..9536acc7e10 100644 --- a/wpilibc/src/main/native/cpp/Notifier.cpp +++ b/wpilibc/src/main/native/cpp/Notifier.cpp @@ -128,7 +128,7 @@ Notifier::~Notifier() { m_thread.join(); } - HAL_CleanNotifier(handle, &status); + HAL_CleanNotifier(handle); } Notifier::Notifier(Notifier&& rhs) diff --git a/wpilibc/src/main/native/cpp/PWM.cpp b/wpilibc/src/main/native/cpp/PWM.cpp index 6a4c0f58c73..c9d77d45113 100644 --- a/wpilibc/src/main/native/cpp/PWM.cpp +++ b/wpilibc/src/main/native/cpp/PWM.cpp @@ -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) { diff --git a/wpilibc/src/main/native/cpp/PowerDistribution.cpp b/wpilibc/src/main/native/cpp/PowerDistribution.cpp index e50e2b355f7..df5f6ddeed0 100644 --- a/wpilibc/src/main/native/cpp/PowerDistribution.cpp +++ b/wpilibc/src/main/native/cpp/PowerDistribution.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/Relay.cpp b/wpilibc/src/main/native/cpp/Relay.cpp index 3dfbe64930d..7c523711a50 100644 --- a/wpilibc/src/main/native/cpp/Relay.cpp +++ b/wpilibc/src/main/native/cpp/Relay.cpp @@ -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) { diff --git a/wpilibc/src/main/native/cpp/SPI.cpp b/wpilibc/src/main/native/cpp/SPI.cpp index 26e26b71e6a..88f8f1a9fde 100644 --- a/wpilibc/src/main/native/cpp/SPI.cpp +++ b/wpilibc/src/main/native/cpp/SPI.cpp @@ -165,9 +165,7 @@ SPI::SPI(Port port) : m_port(static_cast(port)) { static_cast(port) + 1); } -SPI::~SPI() { - HAL_CloseSPI(m_port); -} +SPI::~SPI() = default; SPI::Port SPI::GetPort() const { return static_cast(static_cast(m_port)); diff --git a/wpilibc/src/main/native/cpp/SerialPort.cpp b/wpilibc/src/main/native/cpp/SerialPort.cpp index 698a4403cd5..0d96a9e9e64 100644 --- a/wpilibc/src/main/native/cpp/SerialPort.cpp +++ b/wpilibc/src/main/native/cpp/SerialPort.cpp @@ -74,12 +74,6 @@ SerialPort::SerialPort(int baudRate, std::string_view portName, Port port, static_cast(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); diff --git a/wpilibc/src/main/native/cpp/SynchronousInterrupt.cpp b/wpilibc/src/main/native/cpp/SynchronousInterrupt.cpp index a6ab212925e..298ecdd864b 100644 --- a/wpilibc/src/main/native/cpp/SynchronousInterrupt.cpp +++ b/wpilibc/src/main/native/cpp/SynchronousInterrupt.cpp @@ -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) { diff --git a/wpilibc/src/main/native/cpp/TimedRobot.cpp b/wpilibc/src/main/native/cpp/TimedRobot.cpp index 538d1b2cf47..4b0ebff3424 100644 --- a/wpilibc/src/main/native/cpp/TimedRobot.cpp +++ b/wpilibc/src/main/native/cpp/TimedRobot.cpp @@ -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 callback, diff --git a/wpilibc/src/main/native/cpp/Watchdog.cpp b/wpilibc/src/main/native/cpp/Watchdog.cpp index e626d273524..91d999d6c41 100644 --- a/wpilibc/src/main/native/cpp/Watchdog.cpp +++ b/wpilibc/src/main/native/cpp/Watchdog.cpp @@ -65,7 +65,7 @@ Watchdog::Impl::~Impl() { m_thread.join(); } - HAL_CleanNotifier(handle, &status); + HAL_CleanNotifier(handle); } void Watchdog::Impl::UpdateAlarm() { diff --git a/wpilibc/src/main/native/cpp/counter/ExternalDirectionCounter.cpp b/wpilibc/src/main/native/cpp/counter/ExternalDirectionCounter.cpp index 071d55063be..3bea166ec66 100644 --- a/wpilibc/src/main/native/cpp/counter/ExternalDirectionCounter.cpp +++ b/wpilibc/src/main/native/cpp/counter/ExternalDirectionCounter.cpp @@ -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); diff --git a/wpilibc/src/main/native/cpp/counter/Tachometer.cpp b/wpilibc/src/main/native/cpp/counter/Tachometer.cpp index 42dd22a3e15..b5a32b807ed 100644 --- a/wpilibc/src/main/native/cpp/counter/Tachometer.cpp +++ b/wpilibc/src/main/native/cpp/counter/Tachometer.cpp @@ -37,11 +37,6 @@ Tachometer::Tachometer(std::shared_ptr 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) { diff --git a/wpilibc/src/main/native/cpp/counter/UpDownCounter.cpp b/wpilibc/src/main/native/cpp/counter/UpDownCounter.cpp index 706b2d96733..30b9ea6afea 100644 --- a/wpilibc/src/main/native/cpp/counter/UpDownCounter.cpp +++ b/wpilibc/src/main/native/cpp/counter/UpDownCounter.cpp @@ -55,11 +55,6 @@ UpDownCounter::UpDownCounter(std::shared_ptr 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); diff --git a/wpilibc/src/main/native/include/frc/AddressableLED.h b/wpilibc/src/main/native/include/frc/AddressableLED.h index 5f1208608ef..d395ab0d924 100644 --- a/wpilibc/src/main/native/include/frc/AddressableLED.h +++ b/wpilibc/src/main/native/include/frc/AddressableLED.h @@ -7,7 +7,9 @@ #include #include +#include #include +#include #include #include @@ -93,8 +95,6 @@ class AddressableLED { AddressableLED(AddressableLED&&) = default; AddressableLED& operator=(AddressableLED&&) = default; - ~AddressableLED(); - /** * Sets the length of the LED strip. * @@ -165,8 +165,8 @@ class AddressableLED { void Stop(); private: - hal::Handle m_pwmHandle; - hal::Handle m_handle; + hal::Handle m_pwmHandle; + hal::Handle m_handle; int m_port; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/AnalogGyro.h b/wpilibc/src/main/native/include/frc/AnalogGyro.h index 3913b404884..a822eea33b8 100644 --- a/wpilibc/src/main/native/include/frc/AnalogGyro.h +++ b/wpilibc/src/main/native/include/frc/AnalogGyro.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -100,11 +101,11 @@ class AnalogGyro : public wpi::Sendable, */ AnalogGyro(std::shared_ptr channel, int center, double offset); - ~AnalogGyro() override; - AnalogGyro(AnalogGyro&& rhs) = default; AnalogGyro& operator=(AnalogGyro&& rhs) = default; + ~AnalogGyro() override = default; + /** * Return the actual angle in degrees that the robot is currently facing. * @@ -220,7 +221,7 @@ class AnalogGyro : public wpi::Sendable, private: std::shared_ptr m_analog; - hal::Handle m_gyroHandle; + hal::Handle m_gyroHandle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/AnalogInput.h b/wpilibc/src/main/native/include/frc/AnalogInput.h index ebc4e701503..8e46126771b 100644 --- a/wpilibc/src/main/native/include/frc/AnalogInput.h +++ b/wpilibc/src/main/native/include/frc/AnalogInput.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -47,11 +48,11 @@ class AnalogInput : public wpi::Sendable, */ explicit AnalogInput(int channel); - ~AnalogInput() override; - AnalogInput(AnalogInput&&) = default; AnalogInput& operator=(AnalogInput&&) = default; + ~AnalogInput() override = default; + /** * Get a sample straight from this channel. * @@ -284,7 +285,7 @@ class AnalogInput : public wpi::Sendable, private: int m_channel; - hal::Handle m_port; + hal::Handle m_port; int64_t m_accumulatorOffset; }; diff --git a/wpilibc/src/main/native/include/frc/AnalogOutput.h b/wpilibc/src/main/native/include/frc/AnalogOutput.h index 4151ae67557..e5c1c594731 100644 --- a/wpilibc/src/main/native/include/frc/AnalogOutput.h +++ b/wpilibc/src/main/native/include/frc/AnalogOutput.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -25,11 +26,11 @@ class AnalogOutput : public wpi::Sendable, */ explicit AnalogOutput(int channel); - ~AnalogOutput() override; - AnalogOutput(AnalogOutput&&) = default; AnalogOutput& operator=(AnalogOutput&&) = default; + ~AnalogOutput() override = default; + /** * Set the value of the analog output. * @@ -53,7 +54,7 @@ class AnalogOutput : public wpi::Sendable, protected: int m_channel; - hal::Handle m_port; + hal::Handle m_port; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/AnalogTrigger.h b/wpilibc/src/main/native/include/frc/AnalogTrigger.h index d987ec037d6..d38ec13107e 100644 --- a/wpilibc/src/main/native/include/frc/AnalogTrigger.h +++ b/wpilibc/src/main/native/include/frc/AnalogTrigger.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -84,7 +85,7 @@ class AnalogTrigger : public wpi::Sendable, AnalogTrigger(AnalogTrigger&&) = default; AnalogTrigger& operator=(AnalogTrigger&&) = default; - ~AnalogTrigger() override; + ~AnalogTrigger() override = default; /** * Set the upper and lower limits of the analog trigger. @@ -186,7 +187,7 @@ class AnalogTrigger : public wpi::Sendable, std::shared_ptr m_analogInput; std::shared_ptr m_dutyCycle; - hal::Handle m_trigger; + hal::Handle m_trigger; bool m_ownsAnalog = false; }; diff --git a/wpilibc/src/main/native/include/frc/CAN.h b/wpilibc/src/main/native/include/frc/CAN.h index 1f5d1682a0f..c9c35f51bd4 100644 --- a/wpilibc/src/main/native/include/frc/CAN.h +++ b/wpilibc/src/main/native/include/frc/CAN.h @@ -6,7 +6,7 @@ #include -#include +#include namespace frc { struct CANData { @@ -50,11 +50,6 @@ class CAN { */ CAN(int deviceId, int deviceManufacturer, int deviceType); - /** - * Closes the CAN communication. - */ - ~CAN(); - CAN(CAN&&) = default; CAN& operator=(CAN&&) = default; @@ -178,6 +173,6 @@ class CAN { HAL_CAN_Dev_kMiscellaneous; private: - hal::Handle m_handle; + hal::Handle m_handle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/Counter.h b/wpilibc/src/main/native/include/frc/Counter.h index 3f79a707f45..eb8fe8835b9 100644 --- a/wpilibc/src/main/native/include/frc/Counter.h +++ b/wpilibc/src/main/native/include/frc/Counter.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -142,11 +143,11 @@ class Counter : public CounterBase, Counter(EncodingType encodingType, std::shared_ptr upSource, std::shared_ptr downSource, bool inverted); - ~Counter() override; - Counter(Counter&&) = default; Counter& operator=(Counter&&) = default; + ~Counter() override; + /** * Set the up source for the counter as a digital input channel. * @@ -458,7 +459,7 @@ class Counter : public CounterBase, std::shared_ptr m_downSource; /// The FPGA counter object - hal::Handle m_counter; + hal::Handle m_counter; private: /// The index of this counter. diff --git a/wpilibc/src/main/native/include/frc/DMA.h b/wpilibc/src/main/native/include/frc/DMA.h index d09368fb37f..d3d46ed8c9e 100644 --- a/wpilibc/src/main/native/include/frc/DMA.h +++ b/wpilibc/src/main/native/include/frc/DMA.h @@ -4,7 +4,7 @@ #pragma once -#include +#include #include namespace frc { @@ -25,7 +25,6 @@ class DMA { public: DMA(); - ~DMA(); DMA& operator=(DMA&& other) = default; DMA(DMA&& other) = default; @@ -189,6 +188,6 @@ class DMA { void Stop(); private: - hal::Handle dmaHandle; + hal::Handle dmaHandle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/DigitalInput.h b/wpilibc/src/main/native/include/frc/DigitalInput.h index cee31fdc39c..e850c56ae2c 100644 --- a/wpilibc/src/main/native/include/frc/DigitalInput.h +++ b/wpilibc/src/main/native/include/frc/DigitalInput.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include @@ -35,11 +36,11 @@ class DigitalInput : public DigitalSource, */ explicit DigitalInput(int channel); - ~DigitalInput() override; - DigitalInput(DigitalInput&&) = default; DigitalInput& operator=(DigitalInput&&) = default; + ~DigitalInput() override = default; + /** * Get the value from a digital input channel. * @@ -79,7 +80,7 @@ class DigitalInput : public DigitalSource, private: int m_channel; - hal::Handle m_handle; + hal::Handle m_handle; friend class DigitalGlitchFilter; }; diff --git a/wpilibc/src/main/native/include/frc/DigitalOutput.h b/wpilibc/src/main/native/include/frc/DigitalOutput.h index d2981face0e..c9db4f6a6f4 100644 --- a/wpilibc/src/main/native/include/frc/DigitalOutput.h +++ b/wpilibc/src/main/native/include/frc/DigitalOutput.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -34,11 +35,11 @@ class DigitalOutput : public DigitalSource, */ explicit DigitalOutput(int channel); - ~DigitalOutput() override; - DigitalOutput(DigitalOutput&&) = default; DigitalOutput& operator=(DigitalOutput&&) = default; + ~DigitalOutput() override; + /** * Set the value of a digital output. * @@ -161,7 +162,7 @@ class DigitalOutput : public DigitalSource, private: int m_channel; - hal::Handle m_handle; + hal::Handle m_handle; hal::Handle m_pwmGenerator; }; diff --git a/wpilibc/src/main/native/include/frc/DutyCycle.h b/wpilibc/src/main/native/include/frc/DutyCycle.h index e2220457fe1..96d783687f1 100644 --- a/wpilibc/src/main/native/include/frc/DutyCycle.h +++ b/wpilibc/src/main/native/include/frc/DutyCycle.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -59,13 +60,13 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper { */ explicit DutyCycle(std::shared_ptr source); + DutyCycle(DutyCycle&&) = default; + DutyCycle& operator=(DutyCycle&&) = default; + /** * Close the DutyCycle and free all resources. */ - ~DutyCycle() override; - - DutyCycle(DutyCycle&&) = default; - DutyCycle& operator=(DutyCycle&&) = default; + ~DutyCycle() override = default; /** * Get the frequency of the duty cycle signal. @@ -121,6 +122,6 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper { private: void InitDutyCycle(); std::shared_ptr m_source; - hal::Handle m_handle; + hal::Handle m_handle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/Encoder.h b/wpilibc/src/main/native/include/frc/Encoder.h index 8d6c10a4556..9d3ceb4c996 100644 --- a/wpilibc/src/main/native/include/frc/Encoder.h +++ b/wpilibc/src/main/native/include/frc/Encoder.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -138,11 +139,11 @@ class Encoder : public CounterBase, std::shared_ptr bSource, bool reverseDirection = false, EncodingType encodingType = k4X); - ~Encoder() override; - Encoder(Encoder&&) = default; Encoder& operator=(Encoder&&) = default; + ~Encoder() override = default; + // CounterBase interface /** * Gets the current count. @@ -378,7 +379,7 @@ class Encoder : public CounterBase, std::shared_ptr m_aSource; // The A phase of the quad encoder std::shared_ptr m_bSource; // The B phase of the quad encoder std::shared_ptr m_indexSource = nullptr; - hal::Handle m_encoder; + hal::Handle m_encoder; friend class DigitalGlitchFilter; }; diff --git a/wpilibc/src/main/native/include/frc/I2C.h b/wpilibc/src/main/native/include/frc/I2C.h index 07f3d4a927c..f51b1f25f04 100644 --- a/wpilibc/src/main/native/include/frc/I2C.h +++ b/wpilibc/src/main/native/include/frc/I2C.h @@ -40,8 +40,6 @@ class I2C { */ I2C(Port port, int deviceAddress); - ~I2C(); - I2C(I2C&&) = default; I2C& operator=(I2C&&) = default; diff --git a/wpilibc/src/main/native/include/frc/PWM.h b/wpilibc/src/main/native/include/frc/PWM.h index c1f162ee816..2c49060693c 100644 --- a/wpilibc/src/main/native/include/frc/PWM.h +++ b/wpilibc/src/main/native/include/frc/PWM.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -59,6 +60,9 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper { */ explicit PWM(int channel, bool registerSendable = true); + PWM(PWM&&) = default; + PWM& operator=(PWM&&) = default; + /** * Free the PWM channel. * @@ -66,9 +70,6 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper { */ ~PWM() override; - PWM(PWM&&) = default; - PWM& operator=(PWM&&) = default; - /** * Set the PWM pulse time directly to the hardware. * @@ -206,7 +207,7 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper { private: int m_channel; - hal::Handle m_handle; + hal::Handle m_handle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/PowerDistribution.h b/wpilibc/src/main/native/include/frc/PowerDistribution.h index e3ab87b9cc6..8d647af542d 100644 --- a/wpilibc/src/main/native/include/frc/PowerDistribution.h +++ b/wpilibc/src/main/native/include/frc/PowerDistribution.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -48,10 +49,11 @@ class PowerDistribution : public wpi::Sendable, */ PowerDistribution(int module, ModuleType moduleType); - ~PowerDistribution() override; PowerDistribution(PowerDistribution&&) = default; PowerDistribution& operator=(PowerDistribution&&) = default; + ~PowerDistribution() override = default; + /** * Gets the number of channels for this power distribution object. * @@ -341,7 +343,7 @@ class PowerDistribution : public wpi::Sendable, void InitSendable(wpi::SendableBuilder& builder) override; private: - hal::Handle m_handle; + hal::Handle m_handle; int m_module; }; diff --git a/wpilibc/src/main/native/include/frc/Relay.h b/wpilibc/src/main/native/include/frc/Relay.h index d08a0007daf..72e10e0052e 100644 --- a/wpilibc/src/main/native/include/frc/Relay.h +++ b/wpilibc/src/main/native/include/frc/Relay.h @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -68,6 +69,9 @@ class Relay : public MotorSafety, */ explicit Relay(int channel, Direction direction = kBothDirections); + Relay(Relay&&) = default; + Relay& operator=(Relay&&) = default; + /** * Free the resource associated with a relay. * @@ -75,9 +79,6 @@ class Relay : public MotorSafety, */ ~Relay() override; - Relay(Relay&&) = default; - Relay& operator=(Relay&&) = default; - /** * Set the relay state. * @@ -120,8 +121,8 @@ class Relay : public MotorSafety, int m_channel; Direction m_direction; - hal::Handle m_forwardHandle; - hal::Handle m_reverseHandle; + hal::Handle m_forwardHandle; + hal::Handle m_reverseHandle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/SPI.h b/wpilibc/src/main/native/include/frc/SPI.h index d50214574db..6004b261398 100644 --- a/wpilibc/src/main/native/include/frc/SPI.h +++ b/wpilibc/src/main/native/include/frc/SPI.h @@ -62,11 +62,11 @@ class SPI { */ explicit SPI(Port port); - ~SPI(); - SPI(SPI&&) = default; SPI& operator=(SPI&&) = default; + ~SPI(); + /** * Returns the SPI port. * diff --git a/wpilibc/src/main/native/include/frc/SerialPort.h b/wpilibc/src/main/native/include/frc/SerialPort.h index a504a1e5103..35e075a92ce 100644 --- a/wpilibc/src/main/native/include/frc/SerialPort.h +++ b/wpilibc/src/main/native/include/frc/SerialPort.h @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -128,8 +129,6 @@ class SerialPort { int dataBits = 8, Parity parity = kParity_None, StopBits stopBits = kStopBits_One); - ~SerialPort(); - SerialPort(SerialPort&& rhs) = default; SerialPort& operator=(SerialPort&& rhs) = default; @@ -255,7 +254,7 @@ class SerialPort { void Reset(); private: - hal::Handle m_portHandle; + hal::Handle m_portHandle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/SynchronousInterrupt.h b/wpilibc/src/main/native/include/frc/SynchronousInterrupt.h index 3e396e85779..15ce7fb6cdd 100644 --- a/wpilibc/src/main/native/include/frc/SynchronousInterrupt.h +++ b/wpilibc/src/main/native/include/frc/SynchronousInterrupt.h @@ -6,6 +6,7 @@ #include +#include #include #include @@ -56,8 +57,6 @@ class SynchronousInterrupt { */ explicit SynchronousInterrupt(std::shared_ptr source); - ~SynchronousInterrupt(); - SynchronousInterrupt(SynchronousInterrupt&&) = default; SynchronousInterrupt& operator=(SynchronousInterrupt&&) = default; @@ -108,6 +107,6 @@ class SynchronousInterrupt { private: void InitSynchronousInterrupt(); std::shared_ptr m_source; - hal::Handle m_handle; + hal::Handle m_handle; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/TimedRobot.h b/wpilibc/src/main/native/include/frc/TimedRobot.h index 647285474ad..68d5ecf14c5 100644 --- a/wpilibc/src/main/native/include/frc/TimedRobot.h +++ b/wpilibc/src/main/native/include/frc/TimedRobot.h @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -50,11 +51,11 @@ class TimedRobot : public IterativeRobotBase { */ explicit TimedRobot(units::second_t period = kDefaultPeriod); - ~TimedRobot() override; - TimedRobot(TimedRobot&&) = default; TimedRobot& operator=(TimedRobot&&) = default; + ~TimedRobot() override; + /** * Add a callback to run at a specific period with a starting time offset. * @@ -100,7 +101,7 @@ class TimedRobot : public IterativeRobotBase { } }; - hal::Handle m_notifier; + hal::Handle m_notifier; std::chrono::microseconds m_startTime; wpi::priority_queue, std::greater> diff --git a/wpilibc/src/main/native/include/frc/counter/ExternalDirectionCounter.h b/wpilibc/src/main/native/include/frc/counter/ExternalDirectionCounter.h index 4d9faf21018..a2c4a0effd9 100644 --- a/wpilibc/src/main/native/include/frc/counter/ExternalDirectionCounter.h +++ b/wpilibc/src/main/native/include/frc/counter/ExternalDirectionCounter.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -43,11 +44,11 @@ class ExternalDirectionCounter ExternalDirectionCounter(std::shared_ptr countSource, std::shared_ptr directionSource); - ~ExternalDirectionCounter() override; - ExternalDirectionCounter(ExternalDirectionCounter&&) = default; ExternalDirectionCounter& operator=(ExternalDirectionCounter&&) = default; + ~ExternalDirectionCounter() override = default; + /** * Gets the current count. * @@ -78,7 +79,7 @@ class ExternalDirectionCounter private: std::shared_ptr m_countSource; std::shared_ptr m_directionSource; - hal::Handle m_handle; + hal::Handle m_handle; int32_t m_index = 0; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/counter/Tachometer.h b/wpilibc/src/main/native/include/frc/counter/Tachometer.h index dba1575d6e1..d8cf354563b 100644 --- a/wpilibc/src/main/native/include/frc/counter/Tachometer.h +++ b/wpilibc/src/main/native/include/frc/counter/Tachometer.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -42,11 +43,11 @@ class Tachometer : public wpi::Sendable, */ explicit Tachometer(std::shared_ptr source); - ~Tachometer() override; - Tachometer(Tachometer&&) = default; Tachometer& operator=(Tachometer&&) = default; + ~Tachometer() override = default; + /** * Gets the tachometer frequency. * @@ -133,7 +134,7 @@ class Tachometer : public wpi::Sendable, private: std::shared_ptr m_source; - hal::Handle m_handle; + hal::Handle m_handle; int m_edgesPerRevolution; int32_t m_index; }; diff --git a/wpilibc/src/main/native/include/frc/counter/UpDownCounter.h b/wpilibc/src/main/native/include/frc/counter/UpDownCounter.h index b01ce42642c..8fe8f4c090e 100644 --- a/wpilibc/src/main/native/include/frc/counter/UpDownCounter.h +++ b/wpilibc/src/main/native/include/frc/counter/UpDownCounter.h @@ -6,6 +6,7 @@ #include +#include #include #include #include @@ -41,11 +42,11 @@ class UpDownCounter : public wpi::Sendable, UpDownCounter(std::shared_ptr upSource, std::shared_ptr downSource); - ~UpDownCounter() override; - UpDownCounter(UpDownCounter&&) = default; UpDownCounter& operator=(UpDownCounter&&) = default; + ~UpDownCounter() override = default; + /** * Gets the current count. * @@ -84,7 +85,7 @@ class UpDownCounter : public wpi::Sendable, void InitUpDownCounter(); std::shared_ptr m_upSource; std::shared_ptr m_downSource; - hal::Handle m_handle; + hal::Handle m_handle; int32_t m_index = 0; }; } // namespace frc diff --git a/wpilibcExamples/src/main/cpp/examples/HAL/c/Robot.c b/wpilibcExamples/src/main/cpp/examples/HAL/c/Robot.c index a273e3306cc..3cebc20e05a 100644 --- a/wpilibcExamples/src/main/cpp/examples/HAL/c/Robot.c +++ b/wpilibcExamples/src/main/cpp/examples/HAL/c/Robot.c @@ -82,8 +82,7 @@ int main(void) { if (status != 0) { const char* message = HAL_GetLastError(&status); printf("%s\n", message); - status = 0; - HAL_FreePWMPort(pwmPort, &status); + HAL_FreePWMPort(pwmPort); return 1; } @@ -128,5 +127,5 @@ int main(void) { status = 0; HAL_FreeDIOPort(dio); - HAL_FreePWMPort(pwmPort, &status); + HAL_FreePWMPort(pwmPort); }