diff --git a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java index 635a7aabd74..a913b554e63 100644 --- a/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java +++ b/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/Command.java @@ -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. diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp index 7ea3a1449c6..d228b2679b4 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp @@ -123,6 +123,10 @@ CommandPtr Command::FinallyDo(std::function end) && { return std::move(*this).ToPtr().FinallyDo(std::move(end)); } +CommandPtr Command::FinallyDo(std::function end) && { + return std::move(*this).ToPtr().FinallyDo(std::move(end)); +} + CommandPtr Command::HandleInterrupt(std::function handler) && { return std::move(*this).ToPtr().HandleInterrupt(std::move(handler)); } diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp index 050f95218de..10e6d4e06db 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp @@ -203,6 +203,12 @@ CommandPtr CommandPtr::FinallyDo(std::function end) && { return std::move(*this); } +CommandPtr CommandPtr::FinallyDo(std::function end) && { + AssertValid(); + return std::move(*this).FinallyDo( + [endHandler = std::move(end)](bool interrupted) { endHandler(); }); +} + CommandPtr CommandPtr::HandleInterrupt(std::function handler) && { AssertValid(); return std::move(*this).FinallyDo( diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h index 414785f2056..9a48b2ecc40 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h @@ -318,6 +318,18 @@ class Command : public wpi::Sendable, public wpi::SendableHelper { [[nodiscard]] CommandPtr FinallyDo(std::function 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 end) &&; + /** * Decorates this command with a lambda to call on interrupt, following the * command's inherent Command::End(bool) method. diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h b/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h index ec21b511922..ae8c11819f4 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h @@ -223,6 +223,18 @@ class CommandPtr final { [[nodiscard]] CommandPtr FinallyDo(std::function 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 end) &&; + /** * Decorates this command with a lambda to call on interrupt, following the * command's inherent Command::End(bool) method.