WPILibC++ 2024.1.1-beta-4
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 <functional>
14#include <memory>
15#include <string>
16#include <unordered_map>
17#include <utility>
18#include <vector>
19
21
24
25namespace frc2 {
26/**
27 * A command composition that runs one of a selection of commands using a
28 * selector and a key to command mapping.
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<Command, 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 m_defaultCommand.SetComposed(true);
58 for (auto&& command : foo) {
59 CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
60 command.second.get()->SetComposed(true);
61 }
62
63 for (auto&& command : foo) {
64 this->AddRequirements(command.second->GetRequirements());
65 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
66 if (command.second->GetInterruptionBehavior() ==
69 }
70 m_commands.emplace(std::move(command.first), std::move(command.second));
71 }
72 }
73
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) {
80 CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
81 command.second.get()->SetComposed(true);
82 }
83
84 for (auto&& command : commands) {
85 this->AddRequirements(command.second->GetRequirements());
86 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
87 if (command.second->GetInterruptionBehavior() ==
90 }
91 m_commands.emplace(std::move(command.first), std::move(command.second));
92 }
93 }
94
95 // No copy constructors for command groups
96 SelectCommand(const SelectCommand& other) = delete;
97
98 // Prevent template expansion from emulating copy ctor
100
101 SelectCommand(SelectCommand&& other) = default;
102
103 void Initialize() override;
104
105 void Execute() override { m_selectedCommand->Execute(); }
106
107 void End(bool interrupted) override {
108 return m_selectedCommand->End(interrupted);
109 }
110
111 bool IsFinished() override { return m_selectedCommand->IsFinished(); }
112
113 bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
114
116 return m_interruptBehavior;
117 }
118
119 void InitSendable(wpi::SendableBuilder& builder) override {
120 Command::InitSendable(builder);
121
122 builder.AddStringProperty(
123 "selected",
124 [this] {
125 if (m_selectedCommand) {
126 return m_selectedCommand->GetName();
127 } else {
128 return std::string{"null"};
129 }
130 },
131 nullptr);
132 }
133
134 protected:
135 std::unique_ptr<Command> TransferOwnership() && override {
136 return std::make_unique<SelectCommand>(std::move(*this));
137 }
138
139 private:
140 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
141 std::function<Key()> m_selector;
142 Command* m_selectedCommand;
143 bool m_runsWhenDisabled = true;
144 Command::InterruptionBehavior m_interruptBehavior{
146
147 PrintCommand m_defaultCommand{
148 "SelectCommand selector value does not correspond to any command!"};
149};
150
151template <typename T>
153 auto find = m_commands.find(m_selector());
154 if (find == m_commands.end()) {
155 m_selectedCommand = &m_defaultCommand;
156 } else {
157 m_selectedCommand = find->second.get();
158 }
159 m_selectedCommand->Initialize();
160}
161
162} // namespace frc2
163
164#ifdef _WIN32
165#pragma warning(pop)
166#endif
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 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 composition that runs one of a selection of commands using a selector and a key to command ...
Definition: SelectCommand.h:38
std::unique_ptr< Command > TransferOwnership() &&override
Definition: SelectCommand.h:135
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:74
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:119
void End(bool interrupted) override
Definition: SelectCommand.h:107
void Initialize() override
Definition: SelectCommand.h:152
bool RunsWhenDisabled() const override
Definition: SelectCommand.h:113
bool IsFinished() override
Definition: SelectCommand.h:111
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition: SelectCommand.h:115
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition: SelectCommand.h:105
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:2120
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89