[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

@@ -41,7 +41,9 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_AddressableLEDJNI_free
(JNIEnv* env, jclass, jint handle)
{
HAL_FreeAddressableLED(static_cast<HAL_AddressableLEDHandle>(handle));
if (handle != HAL_kInvalidHandle) {
HAL_FreeAddressableLED(static_cast<HAL_AddressableLEDHandle>(handle));
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -58,7 +58,9 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CANAPIJNI_cleanCAN
(JNIEnv* env, jclass, jint handle)
{
HAL_CleanCAN(static_cast<HAL_CANHandle>(handle));
if (handle != HAL_kInvalidHandle) {
HAL_CleanCAN(static_cast<HAL_CANHandle>(handle));
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -37,7 +37,9 @@ JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_DutyCycleJNI_free
(JNIEnv*, jclass, jint handle)
{
HAL_FreeDutyCycle(static_cast<HAL_DutyCycleHandle>(handle));
if (handle != HAL_kInvalidHandle) {
HAL_FreeDutyCycle(static_cast<HAL_DutyCycleHandle>(handle));
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -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);
}
}
/*

View File

@@ -298,7 +298,9 @@ Java_edu_wpi_first_hal_SPIJNI_spiFreeAuto
(JNIEnv* env, jclass, jint port)
{
int32_t status = 0;
HAL_FreeSPIAuto(static_cast<HAL_SPIPort>(port), &status);
if (port != HAL_kInvalidHandle) {
HAL_FreeSPIAuto(static_cast<HAL_SPIPort>(port), &status);
}
CheckStatus(env, status);
}

View File

@@ -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<HAL_SerialPortHandle>(handle), &status);
CheckStatus(env, status);
if (handle != HAL_kInvalidHandle) {
HAL_CloseSerial(static_cast<HAL_SerialPortHandle>(handle));
}
}
} // extern "C"

View File

@@ -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);
}
}
/*