2022-10-06 01:19:28 +03: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.
|
|
|
|
|
|
|
|
|
|
#include "frc2/command/CommandPtr.h"
|
|
|
|
|
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2022-11-15 00:20:52 +02:00
|
|
|
#include <frc/Errors.h>
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
#include "frc2/command/CommandScheduler.h"
|
|
|
|
|
#include "frc2/command/ConditionalCommand.h"
|
|
|
|
|
#include "frc2/command/InstantCommand.h"
|
|
|
|
|
#include "frc2/command/ParallelCommandGroup.h"
|
|
|
|
|
#include "frc2/command/ParallelDeadlineGroup.h"
|
|
|
|
|
#include "frc2/command/ParallelRaceGroup.h"
|
2022-11-29 00:43:10 +02:00
|
|
|
#include "frc2/command/ProxyCommand.h"
|
2022-10-06 01:19:28 +03:00
|
|
|
#include "frc2/command/RepeatCommand.h"
|
|
|
|
|
#include "frc2/command/SequentialCommandGroup.h"
|
|
|
|
|
#include "frc2/command/WaitCommand.h"
|
|
|
|
|
#include "frc2/command/WaitUntilCommand.h"
|
|
|
|
|
#include "frc2/command/WrapperCommand.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc2;
|
|
|
|
|
|
2023-12-03 02:45:04 +02:00
|
|
|
CommandPtr::CommandPtr(std::unique_ptr<Command>&& command)
|
|
|
|
|
: m_ptr(std::move(command)) {
|
|
|
|
|
AssertValid();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-09 19:45:02 +02:00
|
|
|
CommandPtr::CommandPtr(CommandPtr&& rhs) {
|
|
|
|
|
m_ptr = std::move(rhs.m_ptr);
|
|
|
|
|
AssertValid();
|
|
|
|
|
rhs.m_moveOutSite = wpi::GetStackTrace(1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 00:20:52 +02:00
|
|
|
void CommandPtr::AssertValid() const {
|
|
|
|
|
if (!m_ptr) {
|
|
|
|
|
throw FRC_MakeError(frc::err::CommandIllegalUse,
|
2023-12-09 19:45:02 +02:00
|
|
|
"Moved-from CommandPtr object used!\nMoved out at:\n{}",
|
|
|
|
|
m_moveOutSite);
|
2022-11-15 00:20:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr CommandPtr::Repeatedly() && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<RepeatCommand>(std::move(m_ptr));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::AsProxy() && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-11-29 00:43:10 +02:00
|
|
|
m_ptr = std::make_unique<ProxyCommand>(std::move(m_ptr));
|
2022-10-06 01:19:28 +03:00
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RunsWhenDisabledCommand : public WrapperCommand {
|
|
|
|
|
public:
|
|
|
|
|
RunsWhenDisabledCommand(std::unique_ptr<Command>&& command,
|
|
|
|
|
bool doesRunWhenDisabled)
|
|
|
|
|
: WrapperCommand(std::move(command)),
|
|
|
|
|
m_runsWhenDisabled(doesRunWhenDisabled) {}
|
|
|
|
|
|
|
|
|
|
bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_runsWhenDisabled;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::IgnoringDisable(bool doesRunWhenDisabled) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<RunsWhenDisabledCommand>(std::move(m_ptr),
|
|
|
|
|
doesRunWhenDisabled);
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using InterruptionBehavior = Command::InterruptionBehavior;
|
|
|
|
|
class InterruptBehaviorCommand : public WrapperCommand {
|
|
|
|
|
public:
|
|
|
|
|
InterruptBehaviorCommand(std::unique_ptr<Command>&& command,
|
|
|
|
|
InterruptionBehavior interruptBehavior)
|
|
|
|
|
: WrapperCommand(std::move(command)),
|
|
|
|
|
m_interruptBehavior(interruptBehavior) {}
|
|
|
|
|
|
|
|
|
|
InterruptionBehavior GetInterruptionBehavior() const override {
|
|
|
|
|
return m_interruptBehavior;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
InterruptionBehavior m_interruptBehavior;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::WithInterruptBehavior(
|
|
|
|
|
InterruptionBehavior interruptBehavior) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<InterruptBehaviorCommand>(std::move(m_ptr),
|
|
|
|
|
interruptBehavior);
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::AndThen(std::function<void()> toRun,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
return std::move(*this).AndThen(CommandPtr(
|
|
|
|
|
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
|
2022-10-06 01:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 19:00:11 +03:00
|
|
|
CommandPtr CommandPtr::AndThen(CommandPtr&& next) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::move(m_ptr));
|
2022-10-10 19:00:11 +03:00
|
|
|
temp.emplace_back(std::move(next).Unwrap());
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<SequentialCommandGroup>(std::move(temp));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 20:48:39 -07:00
|
|
|
CommandPtr CommandPtr::BeforeStarting(std::function<void()> toRun,
|
|
|
|
|
Requirements requirements) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
return std::move(*this).BeforeStarting(CommandPtr(
|
|
|
|
|
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::BeforeStarting(CommandPtr&& before) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::move(before).Unwrap());
|
|
|
|
|
temp.emplace_back(std::move(m_ptr));
|
|
|
|
|
m_ptr = std::make_unique<SequentialCommandGroup>(std::move(temp));
|
|
|
|
|
return std::move(*this);
|
2022-10-06 01:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::WithTimeout(units::second_t duration) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::move(m_ptr));
|
2024-06-01 12:01:15 -07:00
|
|
|
temp.emplace_back(std::make_unique<WaitCommand>(duration));
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<ParallelRaceGroup>(std::move(temp));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::Until(std::function<bool()> condition) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> temp;
|
|
|
|
|
temp.emplace_back(std::move(m_ptr));
|
2024-06-01 12:01:15 -07:00
|
|
|
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<ParallelRaceGroup>(std::move(temp));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 14:09:02 -07:00
|
|
|
CommandPtr CommandPtr::OnlyWhile(std::function<bool()> condition) && {
|
|
|
|
|
AssertValid();
|
|
|
|
|
return std::move(*this).Until(std::not_fn(std::move(condition)));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandPtr CommandPtr::Unless(std::function<bool()> condition) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
m_ptr = std::make_unique<ConditionalCommand>(
|
|
|
|
|
std::make_unique<InstantCommand>(), std::move(m_ptr),
|
|
|
|
|
std::move(condition));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 14:09:02 -07:00
|
|
|
CommandPtr CommandPtr::OnlyIf(std::function<bool()> condition) && {
|
|
|
|
|
AssertValid();
|
|
|
|
|
return std::move(*this).Unless(std::not_fn(std::move(condition)));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-13 20:30:02 -05:00
|
|
|
CommandPtr CommandPtr::WithDeadline(CommandPtr&& deadline) && {
|
|
|
|
|
AssertValid();
|
|
|
|
|
std::vector<std::unique_ptr<Command>> vec;
|
|
|
|
|
vec.emplace_back(std::move(m_ptr));
|
|
|
|
|
m_ptr = std::make_unique<ParallelDeadlineGroup>(std::move(deadline).Unwrap(),
|
|
|
|
|
std::move(vec));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 19:00:11 +03:00
|
|
|
CommandPtr CommandPtr::DeadlineWith(CommandPtr&& parallel) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> vec;
|
|
|
|
|
vec.emplace_back(std::move(parallel).Unwrap());
|
|
|
|
|
m_ptr =
|
|
|
|
|
std::make_unique<ParallelDeadlineGroup>(std::move(m_ptr), std::move(vec));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 03:02:29 +08:00
|
|
|
CommandPtr CommandPtr::DeadlineFor(CommandPtr&& parallel) && {
|
|
|
|
|
AssertValid();
|
|
|
|
|
std::vector<std::unique_ptr<Command>> vec;
|
|
|
|
|
vec.emplace_back(std::move(parallel).Unwrap());
|
|
|
|
|
m_ptr =
|
|
|
|
|
std::make_unique<ParallelDeadlineGroup>(std::move(m_ptr), std::move(vec));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 19:00:11 +03:00
|
|
|
CommandPtr CommandPtr::AlongWith(CommandPtr&& parallel) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> vec;
|
|
|
|
|
vec.emplace_back(std::move(m_ptr));
|
|
|
|
|
vec.emplace_back(std::move(parallel).Unwrap());
|
|
|
|
|
m_ptr = std::make_unique<ParallelCommandGroup>(std::move(vec));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::RaceWith(CommandPtr&& parallel) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> vec;
|
|
|
|
|
vec.emplace_back(std::move(m_ptr));
|
|
|
|
|
vec.emplace_back(std::move(parallel).Unwrap());
|
|
|
|
|
m_ptr = std::make_unique<ParallelRaceGroup>(std::move(vec));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 19:53:27 +03:00
|
|
|
namespace {
|
|
|
|
|
class FinallyCommand : public WrapperCommand {
|
|
|
|
|
public:
|
|
|
|
|
FinallyCommand(std::unique_ptr<Command>&& command,
|
|
|
|
|
std::function<void(bool)> end)
|
|
|
|
|
: WrapperCommand(std::move(command)), m_end(std::move(end)) {}
|
|
|
|
|
|
|
|
|
|
void End(bool interrupted) override {
|
|
|
|
|
WrapperCommand::End(interrupted);
|
|
|
|
|
m_end(interrupted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::function<void(bool)> m_end;
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
CommandPtr CommandPtr::FinallyDo(std::function<void(bool)> end) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-11 19:53:27 +03:00
|
|
|
m_ptr = std::make_unique<FinallyCommand>(std::move(m_ptr), std::move(end));
|
|
|
|
|
return std::move(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 15:21:21 -07:00
|
|
|
CommandPtr CommandPtr::FinallyDo(std::function<void()> end) && {
|
|
|
|
|
AssertValid();
|
|
|
|
|
return std::move(*this).FinallyDo(
|
|
|
|
|
[endHandler = std::move(end)](bool interrupted) { endHandler(); });
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-03 21:22:47 -04:00
|
|
|
CommandPtr CommandPtr::HandleInterrupt(std::function<void()> handler) && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-11 19:53:27 +03:00
|
|
|
return std::move(*this).FinallyDo(
|
|
|
|
|
[handler = std::move(handler)](bool interrupted) {
|
|
|
|
|
if (interrupted) {
|
|
|
|
|
handler();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 10:41:25 -05:00
|
|
|
CommandPtr CommandPtr::WithName(std::string_view name) && {
|
|
|
|
|
AssertValid();
|
2022-12-16 04:28:52 +02:00
|
|
|
WrapperCommand wrapper{std::move(m_ptr)};
|
|
|
|
|
wrapper.SetName(name);
|
|
|
|
|
return std::move(wrapper).ToPtr();
|
2022-11-28 10:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
Command* CommandPtr::get() const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
return m_ptr.get();
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
std::unique_ptr<Command> CommandPtr::Unwrap() && {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-10 19:00:11 +03:00
|
|
|
return std::move(m_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 08:53:04 +02:00
|
|
|
void CommandPtr::Schedule() const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandScheduler::GetInstance().Schedule(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 08:53:04 +02:00
|
|
|
void CommandPtr::Cancel() const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
CommandScheduler::GetInstance().Cancel(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 08:53:04 +02:00
|
|
|
bool CommandPtr::IsScheduled() const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
return CommandScheduler::GetInstance().IsScheduled(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 08:53:04 +02:00
|
|
|
bool CommandPtr::HasRequirement(Subsystem* requirement) const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
AssertValid();
|
2022-10-06 01:19:28 +03:00
|
|
|
return m_ptr->HasRequirement(requirement);
|
|
|
|
|
}
|
2022-10-10 19:00:11 +03:00
|
|
|
|
2023-01-12 08:53:04 +02:00
|
|
|
CommandPtr::operator bool() const& {
|
2022-11-15 00:20:52 +02:00
|
|
|
return m_ptr.operator bool();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 19:00:11 +03:00
|
|
|
std::vector<std::unique_ptr<Command>> CommandPtr::UnwrapVector(
|
|
|
|
|
std::vector<CommandPtr>&& vec) {
|
|
|
|
|
std::vector<std::unique_ptr<Command>> ptrs;
|
|
|
|
|
for (auto&& ptr : vec) {
|
|
|
|
|
ptrs.emplace_back(std::move(ptr).Unwrap());
|
|
|
|
|
}
|
|
|
|
|
return ptrs;
|
|
|
|
|
}
|