From dc18f71004a6d8a53eab84f7c27038cb4e694fa7 Mon Sep 17 00:00:00 2001 From: Ryan Blue Date: Thu, 29 Aug 2024 00:34:28 -0400 Subject: [PATCH] [wpilibc] Refactor AnalogTrigger to use shared_ptr (#7010) --- wpilibc/src/main/native/cpp/AnalogTrigger.cpp | 32 +++++++----- .../main/native/include/frc/AnalogTrigger.h | 51 +++++++++++++++---- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp index ea33c73f8d..e3f117b8e7 100644 --- a/wpilibc/src/main/native/cpp/AnalogTrigger.cpp +++ b/wpilibc/src/main/native/cpp/AnalogTrigger.cpp @@ -18,24 +18,36 @@ using namespace frc; AnalogTrigger::AnalogTrigger(int channel) - : AnalogTrigger(new AnalogInput(channel)) { + : AnalogTrigger(std::make_shared(channel)) { m_ownsAnalog = true; - wpi::SendableRegistry::AddChild(this, m_analogInput); + wpi::SendableRegistry::AddChild(this, m_analogInput.get()); } -AnalogTrigger::AnalogTrigger(AnalogInput* input) { - m_analogInput = input; +AnalogTrigger::AnalogTrigger(AnalogInput& input) + : AnalogTrigger{{&input, wpi::NullDeleter{}}} {} + +AnalogTrigger::AnalogTrigger(AnalogInput* input) + : AnalogTrigger{{input, wpi::NullDeleter{}}} {} + +AnalogTrigger::AnalogTrigger(std::shared_ptr input) + : m_analogInput{std::move(input)} { int32_t status = 0; - m_trigger = HAL_InitializeAnalogTrigger(input->m_port, &status); - FRC_CheckErrorStatus(status, "Channel {}", input->GetChannel()); + m_trigger = HAL_InitializeAnalogTrigger(m_analogInput->m_port, &status); + FRC_CheckErrorStatus(status, "Channel {}", m_analogInput->GetChannel()); int index = GetIndex(); HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, index + 1); wpi::SendableRegistry::AddLW(this, "AnalogTrigger", index); } -AnalogTrigger::AnalogTrigger(DutyCycle* input) { - m_dutyCycle = input; +AnalogTrigger::AnalogTrigger(DutyCycle& input) + : AnalogTrigger{{&input, wpi::NullDeleter{}}} {} + +AnalogTrigger::AnalogTrigger(DutyCycle* input) + : AnalogTrigger{{input, wpi::NullDeleter{}}} {} + +AnalogTrigger::AnalogTrigger(std::shared_ptr input) + : m_dutyCycle{input} { int32_t status = 0; m_trigger = HAL_InitializeAnalogTriggerDutyCycle(input->m_handle, &status); FRC_CheckErrorStatus(status, "Channel {}", m_dutyCycle->GetSourceChannel()); @@ -49,10 +61,6 @@ AnalogTrigger::~AnalogTrigger() { int32_t status = 0; HAL_CleanAnalogTrigger(m_trigger, &status); FRC_ReportError(status, "Channel {}", GetSourceChannel()); - - if (m_ownsAnalog) { - delete m_analogInput; - } } void AnalogTrigger::SetLimitsVoltage(double lower, double upper) { diff --git a/wpilibc/src/main/native/include/frc/AnalogTrigger.h b/wpilibc/src/main/native/include/frc/AnalogTrigger.h index f9c5602666..d987ec037d 100644 --- a/wpilibc/src/main/native/include/frc/AnalogTrigger.h +++ b/wpilibc/src/main/native/include/frc/AnalogTrigger.h @@ -31,27 +31,61 @@ class AnalogTrigger : public wpi::Sendable, explicit AnalogTrigger(int channel); /** - * Construct an analog trigger given an analog input. + * Construct an analog trigger using an existing analog input. * * This should be used in the case of sharing an analog channel between the * trigger and an analog input object. * - * @param input The pointer to the existing AnalogInput object + * @param input A reference to the existing AnalogInput object + */ + explicit AnalogTrigger(AnalogInput& input); + + /** + * Construct an analog trigger using an existing analog input. + * + * This should be used in the case of sharing an analog channel between the + * trigger and an analog input object. + * + * @param input A pointer to the existing AnalogInput object */ explicit AnalogTrigger(AnalogInput* input); /** - * Construct an analog trigger given a duty cycle input. + * Construct an analog trigger using an existing analog input. * - * @param dutyCycle The pointer to the existing DutyCycle object + * This should be used in the case of sharing an analog channel between the + * trigger and an analog input object. + * + * @param input A shared_ptr to the existing AnalogInput object + */ + explicit AnalogTrigger(std::shared_ptr input); + + /** + * Construct an analog trigger using an existing duty cycle input. + * + * @param dutyCycle A reference to the existing DutyCycle object + */ + explicit AnalogTrigger(DutyCycle& dutyCycle); + + /** + * Construct an analog trigger using an existing duty cycle input. + * + * @param dutyCycle A pointer to the existing DutyCycle object */ explicit AnalogTrigger(DutyCycle* dutyCycle); - ~AnalogTrigger() override; + /** + * Construct an analog trigger using an existing duty cycle input. + * + * @param dutyCycle A shared_ptr to the existing DutyCycle object + */ + explicit AnalogTrigger(std::shared_ptr dutyCycle); AnalogTrigger(AnalogTrigger&&) = default; AnalogTrigger& operator=(AnalogTrigger&&) = default; + ~AnalogTrigger() override; + /** * Set the upper and lower limits of the analog trigger. * @@ -139,9 +173,6 @@ class AnalogTrigger : public wpi::Sendable, /** * Creates an AnalogTriggerOutput object. * - * Gets an output object that can be used for routing. Caller is responsible - * for deleting the AnalogTriggerOutput object. - * * @param type An enum of the type of output object to create. * @return A pointer to a new AnalogTriggerOutput object. */ @@ -153,9 +184,9 @@ class AnalogTrigger : public wpi::Sendable, private: int GetSourceChannel() const; + std::shared_ptr m_analogInput; + std::shared_ptr m_dutyCycle; hal::Handle m_trigger; - AnalogInput* m_analogInput = nullptr; - DutyCycle* m_dutyCycle = nullptr; bool m_ownsAnalog = false; };