12 #pragma warning(disable : 4521)
16 #include <type_traits>
17 #include <unordered_map>
21 #include "frc2/command/CommandBase.h"
22 #include "frc2/command/CommandGroupBase.h"
23 #include "frc2/command/PrintCommand.h"
42 template <
typename Key>
51 template <
class... Types,
52 typename = std::enable_if_t<std::conjunction_v<
53 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
55 std::pair<Key, Types>... commands)
56 : m_selector{std::move(selector)} {
57 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
59 ((void)foo.emplace_back(commands.first,
60 std::make_unique<std::remove_reference_t<Types>>(
61 std::move(commands.second))),
64 for (
auto&& command : foo) {
70 for (
auto&& command : foo) {
72 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
73 m_commands.emplace(std::move(command.first), std::move(command.second));
78 std::function<Key()> selector,
79 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
80 : m_selector{std::move(selector)} {
81 for (
auto&& command : commands) {
87 for (
auto&& command : commands) {
89 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
90 m_commands.emplace(std::move(command.first), std::move(command.second));
113 void End(
bool interrupted)
override {
114 return m_selectedCommand->
End(interrupted);
123 return std::make_unique<SelectCommand>(std::move(*
this));
127 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
128 std::function<Key()> m_selector;
129 std::function<
Command*()> m_toRun;
131 bool m_runsWhenDisabled =
true;
134 template <
typename T>
137 auto find = m_commands.find(m_selector());
138 if (find == m_commands.end()) {
140 "SelectCommand selector value does not correspond to any command!");
143 m_selectedCommand = find->second.get();
145 m_selectedCommand = m_toRun();
147 m_selectedCommand->Initialize();
std::unique_ptr< Command > TransferOwnership() &&override
Transfers ownership of this command to a unique pointer.
Definition: SelectCommand.h:122
virtual bool IsFinished()
Whether the command has finished.
Definition: Command.h:88
SelectCommand(std::function< Key()> selector, std::pair< Key, Types >... commands)
Creates a new selectcommand.
Definition: SelectCommand.h:54
SelectCommand(std::function< Command *()> toRun)
Creates a new selectcommand.
Definition: SelectCommand.h:105
virtual void Execute()
The main body of a command.
virtual void End(bool interrupted)
The action to take when the command ends.
bool IsFinished() override
Whether the command has finished.
Definition: SelectCommand.h:117
static bool RequireUngrouped(Command &command)
Requires that the specified command not have been already allocated to a CommandGroup.
void Execute() override
The main body of a command.
Definition: SelectCommand.h:111
Runs one of a selection of commands, either using a selector and a key to command mapping,...
Definition: SelectCommand.h:43
void End(bool interrupted) override
The action to take when the command ends.
Definition: SelectCommand.h:113
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
Definition: SelectCommand.h:119
void AddRequirements(std::initializer_list< Subsystem * > requirements)
Adds the specified requirements to the command.
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
void Initialize() override
The initial subroutine of a command.
Definition: SelectCommand.h:135
A command that prints a string when initialized.
Definition: PrintCommand.h:19