[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

@@ -13,6 +13,11 @@ Button Button::WhenPressed(Command* command) {
return *this;
}
Button Button::WhenPressed(CommandPtr&& command) {
WhenActive(std::move(command));
return *this;
}
Button Button::WhenPressed(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) {
WhenActive(std::move(toRun), requirements);
@@ -30,6 +35,11 @@ Button Button::WhileHeld(Command* command) {
return *this;
}
Button Button::WhileHeld(CommandPtr&& command) {
WhileActiveContinous(std::move(command));
return *this;
}
Button Button::WhileHeld(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) {
WhileActiveContinous(std::move(toRun), requirements);
@@ -47,11 +57,21 @@ Button Button::WhenHeld(Command* command) {
return *this;
}
Button Button::WhenHeld(CommandPtr&& command) {
WhileActiveOnce(std::move(command));
return *this;
}
Button Button::WhenReleased(Command* command) {
WhenInactive(command);
return *this;
}
Button Button::WhenReleased(CommandPtr&& command) {
WhenInactive(std::move(command));
return *this;
}
Button Button::WhenReleased(std::function<void()> toRun,
std::initializer_list<Subsystem*> requirements) {
WhenInactive(std::move(toRun), requirements);
@@ -69,6 +89,11 @@ Button Button::ToggleWhenPressed(Command* command) {
return *this;
}
Button Button::ToggleWhenPressed(CommandPtr&& command) {
ToggleWhenActive(std::move(command));
return *this;
}
Button Button::CancelWhenPressed(Command* command) {
CancelWhenActive(command);
return *this;

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;