diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/ProxyCommand.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/ProxyCommand.cpp index 49bff658ce..9fe462ad2e 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/ProxyCommand.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/ProxyCommand.cpp @@ -11,13 +11,20 @@ using namespace frc2; ProxyCommand::ProxyCommand(wpi::unique_function supplier) : m_supplier(std::move(supplier)) {} +ProxyCommand::ProxyCommand(wpi::unique_function supplier) + : ProxyCommand([supplier = std::move(supplier), + holder = std::optional{}]() mutable { + holder = supplier(); + return holder->get(); + }) {} + ProxyCommand::ProxyCommand(Command* command) - : m_supplier([command] { return command; }) { + : ProxyCommand([command] { return command; }) { SetName(std::string{"Proxy("}.append(command->GetName()).append(")")); } ProxyCommand::ProxyCommand(std::unique_ptr command) - : m_supplier([command = std::move(command)] { return command.get(); }) {} + : ProxyCommand([command = std::move(command)] { return command.get(); }) {} void ProxyCommand::Initialize() { m_command = m_supplier(); @@ -31,8 +38,6 @@ void ProxyCommand::End(bool interrupted) { m_command = nullptr; } -void ProxyCommand::Execute() {} - bool ProxyCommand::IsFinished() { // because we're between `initialize` and `end`, `m_command` is necessarily // not null but if called otherwise and m_command is null, it's UB, so we can diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/ProxyCommand.h b/wpilibNewCommands/src/main/native/include/frc2/command/ProxyCommand.h index a3e0ab323e..d39ef054b0 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/ProxyCommand.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/ProxyCommand.h @@ -31,6 +31,15 @@ class ProxyCommand : public CommandHelper { */ explicit ProxyCommand(wpi::unique_function supplier); + /** + * Creates a new ProxyCommand that schedules the supplied command when + * initialized, and ends when it is no longer scheduled. Useful for lazily + * creating commands at runtime. + * + * @param supplier the command supplier + */ + explicit ProxyCommand(wpi::unique_function supplier); + /** * Creates a new ProxyCommand that schedules the given command when * initialized, and ends when it is no longer scheduled. @@ -56,8 +65,6 @@ class ProxyCommand : public CommandHelper { void End(bool interrupted) override; - void Execute() override; - bool IsFinished() override; void InitSendable(wpi::SendableBuilder& builder) override;