9#pragma warning(disable : 4521)
16#include <unordered_map>
36template <
typename Key>
45 template <std::derived_from<Command>... Commands>
47 std::pair<Key, Commands>... commands)
48 : m_selector{
std::move(selector)} {
49 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
51 ((void)foo.emplace_back(
53 std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
56 m_defaultCommand.SetComposed(
true);
57 for (
auto&& command : foo) {
59 command.second.get());
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());
82 command.second.get()->SetComposed(
true);
85 for (
auto&& command : commands) {
86 this->AddRequirements(command.second->GetRequirements());
87 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
88 if (command.second->GetInterruptionBehavior() ==
92 m_commands.emplace(std::move(command.first), std::move(command.second));
106 void Execute()
override { m_selectedCommand->Execute(); }
108 void End(
bool interrupted)
override {
109 return m_selectedCommand->End(interrupted);
112 bool IsFinished()
override {
return m_selectedCommand->IsFinished(); }
117 return m_interruptBehavior;
126 if (m_selectedCommand) {
127 return m_selectedCommand->GetName();
129 return std::string{
"null"};
136 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
137 std::function<Key()> m_selector;
139 bool m_runsWhenDisabled =
true;
143 PrintCommand m_defaultCommand{
144 "SelectCommand selector value does not correspond to any command!"};
149 auto find = m_commands.find(m_selector());
150 if (find == m_commands.end()) {
151 m_selectedCommand = &m_defaultCommand;
153 m_selectedCommand = find->second.get();
155 m_selectedCommand->Initialize();
A state machine representing a complete action to be performed by the robot.
Definition Command.hpp:41
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition Command.hpp:173
@ kCancelSelf
This command ends, End(true) is called, and the incoming command is scheduled normally.
Definition Command.hpp:180
@ kCancelIncoming
This command continues, and the incoming command is not scheduled.
Definition Command.hpp:182
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
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.
SelectCommand(const SelectCommand &other)=delete
void Initialize() override
Definition SelectCommand.hpp:148
void Execute() override
Definition SelectCommand.hpp:106
bool IsFinished() override
Definition SelectCommand.hpp:112
void End(bool interrupted) override
Definition SelectCommand.hpp:108
void InitSendable(wpi::util::SendableBuilder &builder) override
Definition SelectCommand.hpp:120
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition SelectCommand.hpp:116
SelectCommand(SelectCommand &&other)=default
bool RunsWhenDisabled() const override
Definition SelectCommand.hpp:114
SelectCommand(std::function< Key()> selector, std::pair< Key, Commands >... commands)
Creates a new SelectCommand.
Definition SelectCommand.hpp:46
SelectCommand(SelectCommand &)=delete
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition SelectCommand.hpp:74
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp: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 StringMap.hpp:773
Definition CommandNiDsStadiaController.hpp:15