9#pragma warning(disable : 4521)
15#include <unordered_map>
37template <
typename Key>
46 template <
class... Types,
48 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
50 std::pair<Key, Types>... commands)
51 : m_selector{
std::move(selector)} {
52 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
54 ((void)foo.emplace_back(commands.first,
55 std::make_unique<std::remove_reference_t<Types>>(
56 std::move(commands.second))),
59 for (
auto&& command : foo) {
63 for (
auto&& command : foo) {
65 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
66 if (command.second->GetInterruptionBehavior() ==
70 m_commands.emplace(std::move(command.first), std::move(command.second));
75 std::function<Key()> selector,
76 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
77 : m_selector{
std::move(selector)} {
78 for (
auto&& command : commands) {
82 for (
auto&& command : commands) {
84 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
85 if (command.second->GetInterruptionBehavior() ==
89 m_commands.emplace(std::move(command.first), std::move(command.second));
106 WPI_DEPRECATED(
"Replace with ProxyCommand")
108 : m_toRun{std::move(toRun)} {}
116 void End(
bool interrupted)
override {
117 return m_selectedCommand->
End(interrupted);
125 return m_interruptBehavior;
134 if (m_selectedCommand) {
135 return m_selectedCommand->
GetName();
137 return std::string{
"null"};
145 return std::make_unique<SelectCommand>(std::move(*
this));
149 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
150 std::function<Key()> m_selector;
151 std::function<
Command*()> m_toRun;
153 bool m_runsWhenDisabled =
true;
161 auto find = m_commands.find(m_selector());
162 if (
find == m_commands.end()) {
164 "SelectCommand selector value does not correspond to any command!");
167 m_selectedCommand =
find->second.get();
169 m_selectedCommand = m_toRun();
171 m_selectedCommand->Initialize();
void AddRequirements(std::initializer_list< Subsystem * > requirements)
Adds the specified Subsystem requirements to the command.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:47
virtual void End(bool interrupted)
The action to take when the command ends.
virtual std::string GetName() const
Gets the name of this Command.
virtual void Execute()
The main body of a command.
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:104
@ kCancelSelf
This command ends, End(true) is called, and the incoming command is scheduled normally.
@ kCancelIncoming
This command continues, and the incoming command is not scheduled.
virtual bool IsFinished()
Whether the command has finished.
Definition: Command.h:83
void RequireUngrouped(const Command *command)
Requires that the specified command hasn't been already added to a composition.
static CommandScheduler & GetInstance()
Returns the Scheduler instance.
A command that prints a string when initialized.
Definition: PrintCommand.h:18
A command composition that runs one of a selection of commands, either using a selector and a key to ...
Definition: SelectCommand.h:38
std::unique_ptr< Command > TransferOwnership() &&override
Transfers ownership of this command to a unique pointer.
Definition: SelectCommand.h:144
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:74
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
Definition: SelectCommand.h:128
void End(bool interrupted) override
The action to take when the command ends.
Definition: SelectCommand.h:116
SelectCommand(std::function< Key()> selector, std::pair< Key, Types >... commands)
Creates a new SelectCommand.
Definition: SelectCommand.h:49
void Initialize() override
The initial subroutine of a command.
Definition: SelectCommand.h:159
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
Definition: SelectCommand.h:122
bool IsFinished() override
Whether the command has finished.
Definition: SelectCommand.h:120
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
Definition: SelectCommand.h:124
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
The main body of a command.
Definition: SelectCommand.h:114
Definition: SendableBuilder.h:18
virtual void AddStringProperty(std::string_view key, std::function< std::string()> getter, std::function< void(std::string_view)> setter)=0
Add a string property.
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2318
Definition: InstantCommand.h:14
Definition: StdDeque.h:50