[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

@@ -7,7 +7,9 @@
#include <initializer_list>
#include <span>
#include <hal/AddressableLED.h>
#include <hal/AddressableLEDTypes.h>
#include <hal/PWM.h>
#include <hal/Types.h>
#include <units/time.h>
@@ -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<HAL_DigitalHandle> m_pwmHandle;
hal::Handle<HAL_AddressableLEDHandle> m_handle;
hal::Handle<HAL_DigitalHandle, HAL_FreePWMPort> m_pwmHandle;
hal::Handle<HAL_AddressableLEDHandle, HAL_FreeAddressableLED> m_handle;
int m_port;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/AnalogGyro.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -100,11 +101,11 @@ class AnalogGyro : public wpi::Sendable,
*/
AnalogGyro(std::shared_ptr<AnalogInput> 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<AnalogInput> m_analog;
hal::Handle<HAL_GyroHandle> m_gyroHandle;
hal::Handle<HAL_GyroHandle, HAL_FreeAnalogGyro> m_gyroHandle;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <stdint.h>
#include <hal/AnalogInput.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<HAL_AnalogInputHandle> m_port;
hal::Handle<HAL_AnalogInputHandle, HAL_FreeAnalogInputPort> m_port;
int64_t m_accumulatorOffset;
};

View File

@@ -4,6 +4,7 @@
#pragma once
#include <hal/AnalogOutput.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<HAL_AnalogOutputHandle> m_port;
hal::Handle<HAL_AnalogOutputHandle, HAL_FreeAnalogOutputPort> m_port;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/AnalogTrigger.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<AnalogInput> m_analogInput;
std::shared_ptr<DutyCycle> m_dutyCycle;
hal::Handle<HAL_AnalogTriggerHandle> m_trigger;
hal::Handle<HAL_AnalogTriggerHandle, HAL_CleanAnalogTrigger> m_trigger;
bool m_ownsAnalog = false;
};

View File

@@ -6,7 +6,7 @@
#include <stdint.h>
#include <hal/CANAPITypes.h>
#include <hal/CANAPI.h>
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<HAL_CANHandle> m_handle;
hal::Handle<HAL_CANHandle, HAL_CleanCAN> m_handle;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Counter.h>
#include <hal/Types.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
@@ -142,11 +143,11 @@ class Counter : public CounterBase,
Counter(EncodingType encodingType, std::shared_ptr<DigitalSource> upSource,
std::shared_ptr<DigitalSource> 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<DigitalSource> m_downSource;
/// The FPGA counter object
hal::Handle<HAL_CounterHandle> m_counter;
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_counter;
private:
/// The index of this counter.

View File

@@ -4,7 +4,7 @@
#pragma once
#include <hal/Types.h>
#include <hal/DMA.h>
#include <units/time.h>
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<HAL_DMAHandle> dmaHandle;
hal::Handle<HAL_DMAHandle, HAL_FreeDMA> dmaHandle;
};
} // namespace frc

View File

@@ -4,6 +4,7 @@
#pragma once
#include <hal/DIO.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<HAL_DigitalHandle> m_handle;
hal::Handle<HAL_DigitalHandle, HAL_FreeDIOPort> m_handle;
friend class DigitalGlitchFilter;
};

View File

@@ -4,6 +4,7 @@
#pragma once
#include <hal/DIO.h>
#include <hal/Types.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
@@ -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<HAL_DigitalHandle> m_handle;
hal::Handle<HAL_DigitalHandle, HAL_FreeDIOPort> m_handle;
hal::Handle<HAL_DigitalPWMHandle> m_pwmGenerator;
};

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/DutyCycle.h>
#include <hal/Types.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
@@ -59,13 +60,13 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
*/
explicit DutyCycle(std::shared_ptr<DigitalSource> 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<DutyCycle> {
private:
void InitDutyCycle();
std::shared_ptr<DigitalSource> m_source;
hal::Handle<HAL_DutyCycleHandle> m_handle;
hal::Handle<HAL_DutyCycleHandle, HAL_FreeDutyCycle> m_handle;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Encoder.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -138,11 +139,11 @@ class Encoder : public CounterBase,
std::shared_ptr<DigitalSource> 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<DigitalSource> m_aSource; // The A phase of the quad encoder
std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
std::shared_ptr<DigitalSource> m_indexSource = nullptr;
hal::Handle<HAL_EncoderHandle> m_encoder;
hal::Handle<HAL_EncoderHandle, HAL_FreeEncoder> m_encoder;
friend class DigitalGlitchFilter;
};

View File

@@ -40,8 +40,6 @@ class I2C {
*/
I2C(Port port, int deviceAddress);
~I2C();
I2C(I2C&&) = default;
I2C& operator=(I2C&&) = default;

View File

@@ -6,6 +6,7 @@
#include <stdint.h>
#include <hal/PWM.h>
#include <hal/Types.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
@@ -59,6 +60,9 @@ class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
*/
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> {
*/
~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<PWM> {
private:
int m_channel;
hal::Handle<HAL_DigitalHandle> m_handle;
hal::Handle<HAL_DigitalHandle, HAL_FreePWMPort> m_handle;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <vector>
#include <hal/PowerDistribution.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<HAL_PowerDistributionHandle> m_handle;
hal::Handle<HAL_PowerDistributionHandle, HAL_CleanPowerDistribution> m_handle;
int m_module;
};

View File

@@ -7,6 +7,7 @@
#include <memory>
#include <string>
#include <hal/Relay.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -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<HAL_RelayHandle> m_forwardHandle;
hal::Handle<HAL_RelayHandle> m_reverseHandle;
hal::Handle<HAL_RelayHandle, HAL_FreeRelayPort> m_forwardHandle;
hal::Handle<HAL_RelayHandle, HAL_FreeRelayPort> m_reverseHandle;
};
} // namespace frc

View File

@@ -62,11 +62,11 @@ class SPI {
*/
explicit SPI(Port port);
~SPI();
SPI(SPI&&) = default;
SPI& operator=(SPI&&) = default;
~SPI();
/**
* Returns the SPI port.
*

View File

@@ -7,6 +7,7 @@
#include <string>
#include <string_view>
#include <hal/SerialPort.h>
#include <hal/Types.h>
#include <units/time.h>
@@ -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<HAL_SerialPortHandle> m_portHandle;
hal::Handle<HAL_SerialPortHandle, HAL_CloseSerial> m_portHandle;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Interrupts.h>
#include <hal/Types.h>
#include <units/time.h>
@@ -56,8 +57,6 @@ class SynchronousInterrupt {
*/
explicit SynchronousInterrupt(std::shared_ptr<DigitalSource> source);
~SynchronousInterrupt();
SynchronousInterrupt(SynchronousInterrupt&&) = default;
SynchronousInterrupt& operator=(SynchronousInterrupt&&) = default;
@@ -108,6 +107,6 @@ class SynchronousInterrupt {
private:
void InitSynchronousInterrupt();
std::shared_ptr<DigitalSource> m_source;
hal::Handle<HAL_InterruptHandle> m_handle;
hal::Handle<HAL_InterruptHandle, HAL_CleanInterrupts> m_handle;
};
} // namespace frc

View File

@@ -9,6 +9,7 @@
#include <utility>
#include <vector>
#include <hal/Notifier.h>
#include <hal/Types.h>
#include <units/math.h>
#include <units/time.h>
@@ -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<HAL_NotifierHandle> m_notifier;
hal::Handle<HAL_NotifierHandle, HAL_CleanNotifier> m_notifier;
std::chrono::microseconds m_startTime;
wpi::priority_queue<Callback, std::vector<Callback>, std::greater<Callback>>

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Counter.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -43,11 +44,11 @@ class ExternalDirectionCounter
ExternalDirectionCounter(std::shared_ptr<DigitalSource> countSource,
std::shared_ptr<DigitalSource> 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<DigitalSource> m_countSource;
std::shared_ptr<DigitalSource> m_directionSource;
hal::Handle<HAL_CounterHandle> m_handle;
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
int32_t m_index = 0;
};
} // namespace frc

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Counter.h>
#include <hal/Types.h>
#include <units/angular_velocity.h>
#include <units/frequency.h>
@@ -42,11 +43,11 @@ class Tachometer : public wpi::Sendable,
*/
explicit Tachometer(std::shared_ptr<DigitalSource> 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<DigitalSource> m_source;
hal::Handle<HAL_CounterHandle> m_handle;
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
int m_edgesPerRevolution;
int32_t m_index;
};

View File

@@ -6,6 +6,7 @@
#include <memory>
#include <hal/Counter.h>
#include <hal/Types.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -41,11 +42,11 @@ class UpDownCounter : public wpi::Sendable,
UpDownCounter(std::shared_ptr<DigitalSource> upSource,
std::shared_ptr<DigitalSource> 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<DigitalSource> m_upSource;
std::shared_ptr<DigitalSource> m_downSource;
hal::Handle<HAL_CounterHandle> m_handle;
hal::Handle<HAL_CounterHandle, HAL_FreeCounter> m_handle;
int32_t m_index = 0;
};
} // namespace frc