9#pragma warning(disable : 4521)
16#include <unordered_map>
38template <
typename Key>
47 template <std::derived_from<Command>... Commands>
49 std::pair<Key, Commands>... commands)
50 : m_selector{
std::move(selector)} {
51 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
53 ((void)foo.emplace_back(
55 std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
58 m_defaultCommand.SetComposed(
true);
59 for (
auto&& command : foo) {
61 command.second.get());
62 command.second.get()->SetComposed(
true);
65 for (
auto&& command : foo) {
66 this->AddRequirements(command.second->GetRequirements());
67 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
68 if (command.second->GetInterruptionBehavior() ==
72 m_commands.emplace(std::move(command.first), std::move(command.second));
77 std::function<Key()> selector,
78 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
79 : m_selector{
std::move(selector)} {
80 m_defaultCommand.SetComposed(
true);
81 for (
auto&& command : commands) {
83 command.second.get());
84 command.second.get()->SetComposed(
true);
87 for (
auto&& command : commands) {
88 this->AddRequirements(command.second->GetRequirements());
89 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
90 if (command.second->GetInterruptionBehavior() ==
94 m_commands.emplace(std::move(command.first), std::move(command.second));
110 void End(
bool interrupted)
override {
111 return m_selectedCommand->
End(interrupted);
119 return m_interruptBehavior;
128 if (m_selectedCommand) {
129 return m_selectedCommand->
GetName();
131 return std::string{
"null"};
138 WPI_DEPRECATED(
"Use ToPtr() instead")
140 return std::make_unique<SelectCommand>(std::move(*
this));
144 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
145 std::function<Key()> m_selector;
147 bool m_runsWhenDisabled =
true;
151 PrintCommand m_defaultCommand{
152 "SelectCommand selector value does not correspond to any command!"};
157 auto find = m_commands.find(m_selector());
158 if (
find == m_commands.end()) {
159 m_selectedCommand = &m_defaultCommand;
161 m_selectedCommand =
find->second.get();
163 m_selectedCommand->Initialize();
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:41
virtual void End(bool interrupted)
The action to take when the command ends.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
virtual void Execute()
The main body of a command.
std::string GetName() const
Gets the name of this Command.
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:173
@ 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:76
void RequireUngroupedAndUnscheduled(const Command *command)
Requires that the specified command has not already been added to a composition and is not currently ...
static CommandScheduler & GetInstance()
Returns the Scheduler instance.
A command composition that runs one of a selection of commands using a selector and a key to command ...
Definition: SelectCommand.h:39
std::unique_ptr< Command > TransferOwnership() &&override
Definition: SelectCommand.h:139
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:76
SelectCommand(std::function< Key()> selector, std::pair< Key, Commands >... commands)
Creates a new SelectCommand.
Definition: SelectCommand.h:48
void InitSendable(wpi::SendableBuilder &builder) override
Definition: SelectCommand.h:122
void End(bool interrupted) override
Definition: SelectCommand.h:110
void Initialize() override
Definition: SelectCommand.h:156
bool RunsWhenDisabled() const override
Definition: SelectCommand.h:116
bool IsFinished() override
Definition: SelectCommand.h:114
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition: SelectCommand.h:118
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition: SelectCommand.h:108
Helper class for building Sendable dashboard representations.
Definition: SendableBuilder.h:21
virtual void AddStringProperty(std::string_view key, std::function< std::string()> getter, std::function< void(std::string_view)> setter)=0
Add a string property.
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2120
Definition: TrapezoidProfileSubsystem.h:12