9#pragma warning(disable : 4521)
16#include <unordered_map>
37template <
typename Key>
46 template <std::derived_from<Command>... Commands>
48 std::pair<Key, Commands>... commands)
49 : m_selector{
std::move(selector)} {
50 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
52 ((void)foo.emplace_back(
54 std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
57 m_defaultCommand.SetComposed(
true);
58 for (
auto&& command : foo) {
60 command.second.get()->SetComposed(
true);
63 for (
auto&& command : foo) {
64 this->AddRequirements(command.second->GetRequirements());
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 m_defaultCommand.SetComposed(
true);
79 for (
auto&& command : commands) {
81 command.second.get()->SetComposed(
true);
84 for (
auto&& command : commands) {
85 this->AddRequirements(command.second->GetRequirements());
86 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
87 if (command.second->GetInterruptionBehavior() ==
91 m_commands.emplace(std::move(command.first), std::move(command.second));
107 void End(
bool interrupted)
override {
108 return m_selectedCommand->
End(interrupted);
116 return m_interruptBehavior;
125 if (m_selectedCommand) {
126 return m_selectedCommand->
GetName();
128 return std::string{
"null"};
136 return std::make_unique<SelectCommand>(std::move(*
this));
140 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
141 std::function<Key()> m_selector;
143 bool m_runsWhenDisabled =
true;
147 PrintCommand m_defaultCommand{
148 "SelectCommand selector value does not correspond to any command!"};
153 auto find = m_commands.find(m_selector());
154 if (
find == m_commands.end()) {
155 m_selectedCommand = &m_defaultCommand;
157 m_selectedCommand =
find->second.get();
159 m_selectedCommand->Initialize();
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:25
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 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 composition that runs one of a selection of commands using a selector and a key to command ...
Definition: SelectCommand.h:38
std::unique_ptr< Command > TransferOwnership() &&override
Definition: SelectCommand.h:135
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:74
SelectCommand(std::function< Key()> selector, std::pair< Key, Commands >... commands)
Creates a new SelectCommand.
Definition: SelectCommand.h:47
void InitSendable(wpi::SendableBuilder &builder) override
Definition: SelectCommand.h:119
void End(bool interrupted) override
Definition: SelectCommand.h:107
void Initialize() override
Definition: SelectCommand.h:152
bool RunsWhenDisabled() const override
Definition: SelectCommand.h:113
bool IsFinished() override
Definition: SelectCommand.h:111
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition: SelectCommand.h:115
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition: SelectCommand.h:105
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.
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2120
Definition: TrapezoidProfileSubsystem.h:12