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());
61 command.second.get()->SetComposed(
true);
64 for (
auto&& command : foo) {
65 this->AddRequirements(command.second->GetRequirements());
66 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
67 if (command.second->GetInterruptionBehavior() ==
71 m_commands.emplace(std::move(command.first), std::move(command.second));
76 std::function<Key()> selector,
77 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
78 : m_selector{
std::move(selector)} {
79 m_defaultCommand.SetComposed(
true);
80 for (
auto&& command : commands) {
82 command.second.get());
83 command.second.get()->SetComposed(
true);
86 for (
auto&& command : commands) {
87 this->AddRequirements(command.second->GetRequirements());
88 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
89 if (command.second->GetInterruptionBehavior() ==
93 m_commands.emplace(std::move(command.first), std::move(command.second));
109 void End(
bool interrupted)
override {
110 return m_selectedCommand->
End(interrupted);
118 return m_interruptBehavior;
127 if (m_selectedCommand) {
128 return m_selectedCommand->
GetName();
130 return std::string{
"null"};
137 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
138 std::function<Key()> m_selector;
140 bool m_runsWhenDisabled =
true;
144 PrintCommand m_defaultCommand{
145 "SelectCommand selector value does not correspond to any command!"};
150 auto find = m_commands.find(m_selector());
151 if (find == m_commands.end()) {
152 m_selectedCommand = &m_defaultCommand;
154 m_selectedCommand = find->second.get();
156 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 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:38
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition SelectCommand.h:75
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:121
void End(bool interrupted) override
Definition SelectCommand.h:109
void Initialize() override
Definition SelectCommand.h:149
bool RunsWhenDisabled() const override
Definition SelectCommand.h:115
bool IsFinished() override
Definition SelectCommand.h:113
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition SelectCommand.h:117
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition SelectCommand.h:107
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.
Definition FunctionalCommand.h:13
Implement std::hash so that hash_code can be used in STL containers.
Definition PointerIntPair.h:280