2022-11-29 00:43:10 +02:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/commands2/ProxyCommand.hpp"
|
2022-11-29 00:43:10 +02:00
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <string>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/deprecated.hpp"
|
|
|
|
|
#include "wpi/util/sendable/SendableBuilder.hpp"
|
2022-12-16 04:28:52 +02:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::cmd;
|
2022-11-29 00:43:10 +02:00
|
|
|
|
2024-04-29 23:11:29 -05:00
|
|
|
WPI_IGNORE_DEPRECATED
|
2025-11-07 20:00:05 -05:00
|
|
|
ProxyCommand::ProxyCommand(wpi::util::unique_function<Command*()> supplier)
|
2022-11-29 00:43:10 +02:00
|
|
|
: m_supplier(std::move(supplier)) {}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
ProxyCommand::ProxyCommand(wpi::util::unique_function<CommandPtr()> supplier)
|
2023-08-29 16:23:00 -04:00
|
|
|
: ProxyCommand([supplier = std::move(supplier),
|
|
|
|
|
holder = std::optional<CommandPtr>{}]() mutable {
|
|
|
|
|
holder = supplier();
|
|
|
|
|
return holder->get();
|
|
|
|
|
}) {}
|
2024-04-29 23:11:29 -05:00
|
|
|
WPI_UNIGNORE_DEPRECATED
|
2023-08-29 16:23:00 -04:00
|
|
|
|
2022-11-29 00:43:10 +02:00
|
|
|
ProxyCommand::ProxyCommand(Command* command)
|
2024-04-29 23:11:29 -05:00
|
|
|
: m_supplier([command] { return command; }) {
|
2024-04-28 00:41:04 -05:00
|
|
|
SetName(fmt::format("Proxy({})", command->GetName()));
|
2022-12-16 04:28:52 +02:00
|
|
|
}
|
2022-11-29 00:43:10 +02:00
|
|
|
|
2023-08-30 16:21:49 -04:00
|
|
|
ProxyCommand::ProxyCommand(std::unique_ptr<Command> command) {
|
2024-04-28 00:41:04 -05:00
|
|
|
SetName(fmt::format("Proxy({})", command->GetName()));
|
2023-08-30 16:21:49 -04:00
|
|
|
m_supplier = [command = std::move(command)] { return command.get(); };
|
|
|
|
|
}
|
2022-11-29 00:43:10 +02:00
|
|
|
|
|
|
|
|
void ProxyCommand::Initialize() {
|
|
|
|
|
m_command = m_supplier();
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::cmd::CommandScheduler::GetInstance().Schedule(m_command);
|
2022-11-29 00:43:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProxyCommand::End(bool interrupted) {
|
|
|
|
|
if (interrupted) {
|
|
|
|
|
m_command->Cancel();
|
|
|
|
|
}
|
|
|
|
|
m_command = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
// do whatever we want -- like return true.
|
|
|
|
|
return m_command == nullptr || !m_command->IsScheduled();
|
|
|
|
|
}
|
2022-12-16 04:28:52 +02:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void ProxyCommand::InitSendable(wpi::util::SendableBuilder& builder) {
|
2023-07-14 01:12:01 -04:00
|
|
|
Command::InitSendable(builder);
|
2022-12-16 04:28:52 +02:00
|
|
|
builder.AddStringProperty(
|
|
|
|
|
"proxied",
|
|
|
|
|
[this] {
|
|
|
|
|
if (m_command) {
|
|
|
|
|
return m_command->GetName();
|
|
|
|
|
} else {
|
|
|
|
|
return std::string{"null"};
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
nullptr);
|
|
|
|
|
}
|