9#pragma warning(disable : 4521)
15#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 for (
auto&& command : foo) {
61 for (
auto&& command : foo) {
62 this->AddRequirements(command.second->GetRequirements());
63 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
64 if (command.second->GetInterruptionBehavior() ==
68 m_commands.emplace(std::move(command.first), std::move(command.second));
73 std::function<Key()> selector,
74 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
75 : m_selector{
std::move(selector)} {
76 for (
auto&& command : commands) {
80 for (
auto&& command : commands) {
81 this->AddRequirements(command.second->GetRequirements());
82 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
83 if (command.second->GetInterruptionBehavior() ==
87 m_commands.emplace(std::move(command.first), std::move(command.second));
103 void End(
bool interrupted)
override {
104 return m_selectedCommand->
End(interrupted);
112 return m_interruptBehavior;
121 if (m_selectedCommand) {
122 return m_selectedCommand->
GetName();
124 return std::string{
"null"};
132 return std::make_unique<SelectCommand>(std::move(*
this));
136 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
137 std::function<Key()> m_selector;
139 bool m_runsWhenDisabled =
true;
146 auto find = m_commands.find(m_selector());
147 if (
find == m_commands.end()) {
149 "SelectCommand selector value does not correspond to any command!");
151 m_selectedCommand =
find->second.get();
153 m_selectedCommand->Initialize();
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
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:44
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:101
@ 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:80
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
Definition: SelectCommand.h:131
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:72
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:115
void End(bool interrupted) override
Definition: SelectCommand.h:103
void Initialize() override
Definition: SelectCommand.h:145
bool RunsWhenDisabled() const override
Definition: SelectCommand.h:109
bool IsFinished() override
Definition: SelectCommand.h:107
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition: SelectCommand.h:111
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition: SelectCommand.h:101
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:2325
Definition: ProfiledPIDCommand.h:18
Definition: BFloat16.h:88