WPILibC++ 2024.3.2
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
20#include <wpi/deprecated.h>
22
25
26namespace frc2 {
27/**
28 * A command composition that runs one of a selection of commands using a
29 * selector and a key to command mapping.
30 *
31 * <p>The rules for command compositions apply: command instances that are
32 * passed to it are owned by the composition and cannot be added to any other
33 * composition or scheduled individually, and the composition requires all
34 * subsystems its components require.
35 *
36 * This class is provided by the NewCommands VendorDep
37 */
38template <typename Key>
39class SelectCommand : public CommandHelper<Command, SelectCommand<Key>> {
40 public:
41 /**
42 * Creates a new SelectCommand.
43 *
44 * @param commands the map of commands to choose from
45 * @param selector the selector to determine which command to run
46 */
47 template <std::derived_from<Command>... Commands>
48 explicit SelectCommand(std::function<Key()> selector,
49 std::pair<Key, Commands>... commands)
50 : m_selector{std::move(selector)} {
51 std::vector<std::pair<Key, std::unique_ptr<Command>>> foo;
52
53 ((void)foo.emplace_back(
54 commands.first,
55 std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
56 ...);
57
58 m_defaultCommand.SetComposed(true);
59 for (auto&& command : foo) {
61 command.second.get());
62 command.second.get()->SetComposed(true);
63 }
64
65 for (auto&& command : foo) {
66 this->AddRequirements(command.second->GetRequirements());
67 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
68 if (command.second->GetInterruptionBehavior() ==
71 }
72 m_commands.emplace(std::move(command.first), std::move(command.second));
73 }
74 }
75
77 std::function<Key()> selector,
78 std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
79 : m_selector{std::move(selector)} {
80 m_defaultCommand.SetComposed(true);
81 for (auto&& command : commands) {
83 command.second.get());
84 command.second.get()->SetComposed(true);
85 }
86
87 for (auto&& command : commands) {
88 this->AddRequirements(command.second->GetRequirements());
89 m_runsWhenDisabled &= command.second->RunsWhenDisabled();
90 if (command.second->GetInterruptionBehavior() ==
93 }
94 m_commands.emplace(std::move(command.first), std::move(command.second));
95 }
96 }
97
98 // No copy constructors for command groups
99 SelectCommand(const SelectCommand& other) = delete;
100
101 // Prevent template expansion from emulating copy ctor
103
104 SelectCommand(SelectCommand&& other) = default;
105
106 void Initialize() override;
107
108 void Execute() override { m_selectedCommand->Execute(); }
109
110 void End(bool interrupted) override {
111 return m_selectedCommand->End(interrupted);
112 }
113
114 bool IsFinished() override { return m_selectedCommand->IsFinished(); }
115
116 bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
117
119 return m_interruptBehavior;
120 }
121
122 void InitSendable(wpi::SendableBuilder& builder) override {
123 Command::InitSendable(builder);
124
125 builder.AddStringProperty(
126 "selected",
127 [this] {
128 if (m_selectedCommand) {
129 return m_selectedCommand->GetName();
130 } else {
131 return std::string{"null"};
132 }
133 },
134 nullptr);
135 }
136
137 protected:
138 WPI_DEPRECATED("Use ToPtr() instead")
139 std::unique_ptr<Command> TransferOwnership() && override {
140 return std::make_unique<SelectCommand>(std::move(*this));
141 }
142
143 private:
144 std::unordered_map<Key, std::unique_ptr<Command>> m_commands;
145 std::function<Key()> m_selector;
146 Command* m_selectedCommand;
147 bool m_runsWhenDisabled = true;
148 Command::InterruptionBehavior m_interruptBehavior{
150
151 PrintCommand m_defaultCommand{
152 "SelectCommand selector value does not correspond to any command!"};
153};
154
155template <typename T>
157 auto find = m_commands.find(m_selector());
158 if (find == m_commands.end()) {
159 m_selectedCommand = &m_defaultCommand;
160 } else {
161 m_selectedCommand = find->second.get();
162 }
163 m_selectedCommand->Initialize();
164}
165
166} // namespace frc2
167
168#ifdef _WIN32
169#pragma warning(pop)
170#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
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:39
std::unique_ptr< Command > TransferOwnership() &&override
Definition: SelectCommand.h:139
SelectCommand(SelectCommand &&other)=default
SelectCommand(std::function< Key()> selector, std::vector< std::pair< Key, std::unique_ptr< Command > > > &&commands)
Definition: SelectCommand.h:76
SelectCommand(std::function< Key()> selector, std::pair< Key, Commands >... commands)
Creates a new SelectCommand.
Definition: SelectCommand.h:48
void InitSendable(wpi::SendableBuilder &builder) override
Definition: SelectCommand.h:122
void End(bool interrupted) override
Definition: SelectCommand.h:110
void Initialize() override
Definition: SelectCommand.h:156
bool RunsWhenDisabled() const override
Definition: SelectCommand.h:116
bool IsFinished() override
Definition: SelectCommand.h:114
Command::InterruptionBehavior GetInterruptionBehavior() const override
Definition: SelectCommand.h:118
SelectCommand(SelectCommand &)=delete
SelectCommand(const SelectCommand &other)=delete
void Execute() override
Definition: SelectCommand.h:108
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.
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