mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[commands] C++ unique_ptr migration (#4319)
Add a CommandPtr with an internal unique_ptr to enable not needing to move the underlying classes, which is error-prone due to the potential for lambda captures.
This commit is contained in:
@@ -18,6 +18,11 @@ Trigger Trigger::WhenActive(Command* command) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhenActive(CommandPtr&& command) {
|
||||
this->Rising().IfHigh([command = std::move(command)] { command.Schedule(); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhenActive(std::function<void()> toRun,
|
||||
std::initializer_list<Subsystem*> requirements) {
|
||||
return WhenActive(std::move(toRun),
|
||||
@@ -35,6 +40,13 @@ Trigger Trigger::WhileActiveContinous(Command* command) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhileActiveContinous(CommandPtr&& command) {
|
||||
auto ptr = std::make_shared<CommandPtr>(std::move(command));
|
||||
this->IfHigh([ptr] { ptr->Schedule(); });
|
||||
this->Falling().IfHigh([ptr] { ptr->Cancel(); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhileActiveContinous(
|
||||
std::function<void()> toRun,
|
||||
std::initializer_list<Subsystem*> requirements) {
|
||||
@@ -53,11 +65,24 @@ Trigger Trigger::WhileActiveOnce(Command* command) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhileActiveOnce(CommandPtr&& command) {
|
||||
auto ptr = std::make_shared<CommandPtr>(std::move(command));
|
||||
this->Rising().IfHigh([ptr] { ptr->Schedule(); });
|
||||
this->Falling().IfHigh([ptr] { ptr->Cancel(); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhenInactive(Command* command) {
|
||||
this->Falling().IfHigh([command] { command->Schedule(); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhenInactive(CommandPtr&& command) {
|
||||
this->Falling().IfHigh(
|
||||
[command = std::move(command)] { command.Schedule(); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::WhenInactive(std::function<void()> toRun,
|
||||
std::initializer_list<Subsystem*> requirements) {
|
||||
return WhenInactive(std::move(toRun),
|
||||
@@ -80,6 +105,17 @@ Trigger Trigger::ToggleWhenActive(Command* command) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::ToggleWhenActive(CommandPtr&& command) {
|
||||
this->Rising().IfHigh([command = std::move(command)] {
|
||||
if (command.IsScheduled()) {
|
||||
command.Cancel();
|
||||
} else {
|
||||
command.Schedule();
|
||||
}
|
||||
});
|
||||
return *this;
|
||||
}
|
||||
|
||||
Trigger Trigger::CancelWhenActive(Command* command) {
|
||||
this->Rising().IfHigh([command] { command->Cancel(); });
|
||||
return *this;
|
||||
|
||||
Reference in New Issue
Block a user