Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd] Add Java and c++ finallyDo with zero-arg lambda #5862

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,18 @@ public void end(boolean interrupted) {
};
}

/**
* Decorates this command with a lambda to call on interrupt or end, following the command's
* inherent {@link #end(boolean)} method. The provided lambda will run identically in both
* interrupt and end cases.
*
* @param end a lambda to run when the command ends, whether or not it was interrupted.
* @return the decorated command
*/
public WrapperCommand finallyDo(Runnable end) {
return finallyDo(interrupted -> end.run());
}

/**
* Decorates this command with a lambda to call on interrupt, following the command's inherent
* {@link #end(boolean)} method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ CommandPtr Command::FinallyDo(std::function<void(bool)> end) && {
return std::move(*this).ToPtr().FinallyDo(std::move(end));
}

CommandPtr Command::FinallyDo(std::function<void()> end) && {
return std::move(*this).ToPtr().FinallyDo(std::move(end));
}

CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ CommandPtr CommandPtr::FinallyDo(std::function<void(bool)> end) && {
return std::move(*this);
}

CommandPtr CommandPtr::FinallyDo(std::function<void()> end) && {
AssertValid();
return std::move(*this).FinallyDo(
[endHandler = std::move(end)](bool interrupted) { endHandler(); });
}

CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
AssertValid();
return std::move(*this).FinallyDo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ class Command : public wpi::Sendable, public wpi::SendableHelper<Command> {
[[nodiscard]]
CommandPtr FinallyDo(std::function<void(bool)> end) &&;

/**
* Decorates this command with a lambda to call on interrupt or end, following
* the command's inherent Command::End(bool) method. The provided lambda will
* run identically in both interrupt and end cases.
*
* @param end a lambda to run when the command ends, whether or not it was
* interrupted.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void()> end) &&;

/**
* Decorates this command with a lambda to call on interrupt, following the
* command's inherent Command::End(bool) method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ class CommandPtr final {
[[nodiscard]]
CommandPtr FinallyDo(std::function<void(bool)> end) &&;

/**
* Decorates this command with a lambda to call on interrupt or end, following
* the command's inherent Command::End(bool) method. The provided lambda will
* run identically in both interrupt and end cases.
*
* @param end a lambda to run when the command ends, whether or not it was
* interrupted.
* @return the decorated command
*/
[[nodiscard]]
CommandPtr FinallyDo(std::function<void()> end) &&;

/**
* Decorates this command with a lambda to call on interrupt, following the
* command's inherent Command::End(bool) method.
Expand Down