[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:
Starlight220
2022-10-06 01:19:28 +03:00
committed by GitHub
parent 3b81cf6c35
commit 60e29627c0
18 changed files with 644 additions and 133 deletions

View File

@@ -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;