WPILibC++ 2023.4.3-108-ge5452e3
SelectCommand.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#ifdef _WIN32
8#pragma warning(push)
9#pragma warning(disable : 4521)
10#endif
11
12#include <concepts>
13#include <memory>
14#include <string>
15#include <unordered_map>
16#include <utility>
17#include <vector>
18
20
23
24namespace frc2 {
25/**
26 * A command composition that runs one of a selection of commands, either using
27 * a selector and a key to command mapping, or a supplier that returns the
28 * command directly at runtime.
29 *
30 * <p>The rules for command compositions apply: command instances that are
31 * passed to it are owned by the composition and cannot be added to any other
32 * composition or scheduled individually, and the composition requires all
33 * subsystems its components require.
34 *
35 * This class is provided by the NewCommands VendorDep
36 */
37template <typename Key>
38class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
39 public:
40 /**
41 * Creates a new SelectCommand.
42 *
43 * @param commands the map of commands to choose from
44 * @param selector the selector to determine which command to run
45 */
46 template <std::derived_from<Command>... Commands>
47 explicit SelectCommand(std::function<Key()> selector,
48 std::pair<Key, Commands>... commands)
49 : m_selector{std::move(selector)} {
50 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
51
52 ((void)foo.emplace_back(
53 commands.first,
54 std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
55 ...);
56
57 for (auto&& command : foo) {
58 CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
59 }
60
61 for (auto&& command : foo) {
62 this->AddRequirements(command.second->GetRequirements());
63 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
64 if (command.second->GetInterruptionBehavior() ==
67 }
68 m_commands.emplace(std::move(command.first), std::move(command.second));
69 }
70 }
71
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) {
77 CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
78 }
79
80 for (auto&& command : commands) {
81 this->AddRequirements(command.second->GetRequirements());
82 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
83 if (command.second->GetInterruptionBehavior() ==
86 }
87 m_commands.emplace(std::move(command.first), std::move(command.second));
88 }
89 }
90
91 // No copy constructors for command groups
92 SelectCommand(const SelectCommand& other) = delete;
93
94 // Prevent template expansion from emulating copy ctor
96
97 SelectCommand(SelectCommand&& other) = default;
98
99 void Initialize() override;
100
101 void Execute() override { m_selectedCommand->Execute(); }
102
103 void End(bool interrupted) override {
104 return m_selectedCommand->End(interrupted);
105 }
106
107 bool IsFinished() override { return m_selectedCommand->IsFinished(); }
108
109 bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
110
112 return m_interruptBehavior;
113 }
114
115 void InitSendable(wpi::SendableBuilder& builder) override {
117
118 builder.AddStringProperty(
119 "selected",
120 [this] {
121 if (m_selectedCommand) {
122 return m_selectedCommand->GetName();
123 } else {
124 return std::string{"null"};
125 }
126 },
127 nullptr);
128 }
129
130 protected:
131 std::unique_ptr<Command> TransferOwnership() && override {
132 return std::make_unique<SelectCommand>(std::move(*this));
133 }
134
135 private:
136 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
137 std::function<Key()> m_selector;
138 Command* m_selectedCommand;
139 bool m_runsWhenDisabled = true;
140 Command::InterruptionBehavior m_interruptBehavior{
142};
143
144template <typename T>
146 auto find = m_commands.find(m_selector());
147 if (find == m_commands.end()) {
148 m_selectedCommand = new PrintCommand(
149 "SelectCommand selector value does not correspond to any command!");
150 } else {
151 m_selectedCommand = find->second.get();
152 }
153 m_selectedCommand->Initialize();
154}
155
156} // namespace frc2
157
158#ifdef _WIN32
159#pragma warning(pop)
160#endif
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