Skip to content

Commit

Permalink
refactor AnalogTrigger to use shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
rzblue committed Aug 28, 2024
1 parent ec85f99 commit a09c3f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
19 changes: 9 additions & 10 deletions wpilibc/src/main/native/cpp/AnalogTrigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
using namespace frc;

AnalogTrigger::AnalogTrigger(int channel)
: AnalogTrigger(new AnalogInput(channel)) {
: AnalogTrigger(std::make_shared<AnalogInput>(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<AnalogInput>{}}} {}

AnalogTrigger::AnalogTrigger(std::shared_ptr<AnalogInput> 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);
Expand All @@ -47,10 +50,6 @@ AnalogTrigger::AnalogTrigger(DutyCycle* input) {

AnalogTrigger::~AnalogTrigger() {
HAL_CleanAnalogTrigger(m_trigger);

if (m_ownsAnalog) {
delete m_analogInput;
}
}

void AnalogTrigger::SetLimitsVoltage(double lower, double upper) {
Expand Down
23 changes: 15 additions & 8 deletions wpilibc/src/main/native/include/frc/AnalogTrigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ 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.
Expand All @@ -40,18 +40,28 @@ class AnalogTrigger : public wpi::Sendable,
*/
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 The shared_ptr to the existing AnalogInput object
*/
explicit AnalogTrigger(std::shared_ptr<AnalogInput> input);

/**
* Construct an analog trigger given a duty cycle input.
*
* @param dutyCycle The pointer to the existing DutyCycle object
*/
explicit AnalogTrigger(DutyCycle* dutyCycle);

~AnalogTrigger() override;

AnalogTrigger(AnalogTrigger&&) = default;
AnalogTrigger& operator=(AnalogTrigger&&) = default;

~AnalogTrigger() override;

/**
* Set the upper and lower limits of the analog trigger.
*
Expand Down Expand Up @@ -139,9 +149,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.
*/
Expand All @@ -153,9 +160,9 @@ class AnalogTrigger : public wpi::Sendable,
private:
int GetSourceChannel() const;

hal::Handle<HAL_AnalogTriggerHandle> m_trigger;
AnalogInput* m_analogInput = nullptr;
std::shared_ptr<AnalogInput> m_analogInput = nullptr;
DutyCycle* m_dutyCycle = nullptr;
hal::Handle<HAL_AnalogTriggerHandle> m_trigger;
bool m_ownsAnalog = false;
};

Expand Down

0 comments on commit a09c3f2

Please sign in to comment.