WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
CommandPtr.hpp
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#include <concepts>
8#include <functional>
9#include <memory>
10#include <string>
11#include <utility>
12#include <vector>
13
16
17namespace wpi::cmd {
18/**
19 * A wrapper around std::unique_ptr<Command> so commands have move-only
20 * semantics. Commands should only be stored and passed around when held in a
21 * CommandPtr instance. For more info, see
22 * https://github.com/wpilibsuite/allwpilib/issues/4303.
23 *
24 * Various classes in the command-based library accept a
25 * std::unique_ptr<Command>, use CommandPtr::Unwrap to convert.
26 * CommandPtr::UnwrapVector does the same for vectors.
27 */
28class [[nodiscard]] CommandPtr final {
29 public:
30 explicit CommandPtr(std::unique_ptr<Command>&& command);
31
32 template <std::derived_from<Command> T>
33 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
34 explicit CommandPtr(T&& command)
35 : CommandPtr(
36 std::make_unique<std::decay_t<T>>(std::forward<T>(command))) {}
37
40
41 explicit CommandPtr(std::nullptr_t) = delete;
42
43 /**
44 * Decorates this command to run repeatedly, restarting it when it ends, until
45 * this command is interrupted. The decorated command can still be canceled.
46 *
47 * @return the decorated command
48 */
50
51 /**
52 * Decorates this command to run "by proxy" by wrapping it in a ProxyCommand.
53 * Use this for "forking off" from command compositions when the user does not
54 * wish to extend the command's requirements to the entire command
55 * composition. ProxyCommand has unique implications and semantics, see <a
56 * href="https://docs.wpilib.org/en/stable/docs/software/commandbased/command-compositions.html#scheduling-other-commands">the
57 * WPILib docs</a> for a full explanation.
58 *
59 * @return the decorated command
60 * @see ProxyCommand
61 */
63
64 /**
65 * Decorates this command to run or stop when disabled.
66 *
67 * @param doesRunWhenDisabled true to run when disabled
68 * @return the decorated command
69 */
70 CommandPtr IgnoringDisable(bool doesRunWhenDisabled) &&;
71
72 /**
73 * Decorates this command to have a different interrupt behavior.
74 *
75 * @param interruptBehavior the desired interrupt behavior
76 * @return the decorated command
77 */
79 Command::InterruptionBehavior interruptBehavior) &&;
80
81 /**
82 * Decorates this command with a runnable to run after the command finishes.
83 *
84 * @param toRun the Runnable to run
85 * @param requirements the required subsystems
86 * @return the decorated command
87 */
88 CommandPtr AndThen(std::function<void()> toRun,
89 Requirements requirements = {}) &&;
90
91 /**
92 * Decorates this command with a set of commands to run after it in sequence.
93 * Often more convenient/less-verbose than constructing a new {@link
94 * SequentialCommandGroup} explicitly.
95 *
96 * @param next the commands to run next
97 * @return the decorated command
98 */
100
101 /**
102 * Decorates this command with a runnable to run before this command starts.
103 *
104 * @param toRun the Runnable to run
105 * @param requirements the required subsystems
106 * @return the decorated command
107 */
108 CommandPtr BeforeStarting(std::function<void()> toRun,
109 Requirements requirements = {}) &&;
110
111 /**
112 * Decorates this command with another command to run before this command
113 * starts.
114 *
115 * @param before the command to run before this one
116 * @return the decorated command
117 */
119
120 /**
121 * Decorates this command with a timeout. If the specified timeout is
122 * exceeded before the command finishes normally, the command will be
123 * interrupted and un-scheduled.
124 *
125 * @param duration the timeout duration
126 * @return the command with the timeout added
127 */
128 CommandPtr WithTimeout(wpi::units::second_t duration) &&;
129
130 /**
131 * Decorates this command with an interrupt condition. If the specified
132 * condition becomes true before the command finishes normally, the command
133 * will be interrupted and un-scheduled.
134 *
135 * @param condition the interrupt condition
136 * @return the command with the interrupt condition added
137 */
138 CommandPtr Until(std::function<bool()> condition) &&;
139
140 /**
141 * Decorates this command with a run condition. If the specified condition
142 * becomes false before the command finishes normally, the command will be
143 * interrupted and un-scheduled.
144 *
145 * @param condition the run condition
146 * @return the command with the run condition added
147 */
148 CommandPtr OnlyWhile(std::function<bool()> condition) &&;
149
150 /**
151 * Decorates this command to only run if this condition is not met. If the
152 * command is already running and the condition changes to true, the command
153 * will not stop running. The requirements of this command will be kept for
154 * the new conditional command.
155 *
156 * @param condition the condition that will prevent the command from running
157 * @return the decorated command
158 */
159 CommandPtr Unless(std::function<bool()> condition) &&;
160
161 /**
162 * Decorates this command to only run if this condition is met. If the command
163 * is already running and the condition changes to false, the command will not
164 * stop running. The requirements of this command will be kept for the new
165 * conditional command.
166 *
167 * @param condition the condition that will allow the command to run
168 * @return the decorated command
169 */
170 CommandPtr OnlyIf(std::function<bool()> condition) &&;
171
172 /**
173 * Creates a new command that runs this command and the deadline in parallel,
174 * finishing (and interrupting this command) when the deadline finishes.
175 *
176 * @param deadline the deadline of the command group
177 * @return the decorated command
178 * @see DeadlineFor
179 */
181
182 /**
183 * Decorates this command with a set of commands to run parallel to it, ending
184 * when the calling command ends and interrupting all the others. Often more
185 * convenient/less-verbose than constructing a new {@link
186 * ParallelDeadlineGroup} explicitly.
187 *
188 * @param parallel the commands to run in parallel. Note the parallel commands
189 * will be interupted when the deadline command ends
190 * @return the decorated command
191 */
193 /**
194 * Decorates this command with a set of commands to run parallel to it, ending
195 * when the last command ends. Often more convenient/less-verbose than
196 * constructing a new {@link ParallelCommandGroup} explicitly.
197 *
198 * @param parallel the commands to run in parallel
199 * @return the decorated command
200 */
202
203 /**
204 * Decorates this command with a set of commands to run parallel to it, ending
205 * when the first command ends. Often more convenient/less-verbose than
206 * constructing a new {@link ParallelRaceGroup} explicitly.
207 *
208 * @param parallel the commands to run in parallel
209 * @return the decorated command
210 */
212
213 /**
214 * Decorates this command with a lambda to call on interrupt or end, following
215 * the command's inherent Command::End(bool) method.
216 *
217 * @param end a lambda accepting a boolean parameter specifying whether the
218 * command was interrupted
219 * @return the decorated command
220 */
221 CommandPtr FinallyDo(std::function<void(bool)> end) &&;
222
223 /**
224 * Decorates this command with a lambda to call on interrupt or end, following
225 * the command's inherent Command::End(bool) method. The provided lambda will
226 * run identically in both interrupt and end cases.
227 *
228 * @param end a lambda to run when the command ends, whether or not it was
229 * interrupted.
230 * @return the decorated command
231 */
232 CommandPtr FinallyDo(std::function<void()> end) &&;
233
234 /**
235 * Decorates this command with a lambda to call on interrupt, following the
236 * command's inherent Command::End(bool) method.
237 *
238 * @param handler a lambda to run when the command is interrupted
239 * @return the decorated command
240 */
241 CommandPtr HandleInterrupt(std::function<void()> handler) &&;
242
243 /**
244 * Decorates this Command with a name. Is an inline function for
245 * Command::SetName(std::string_view);
246 *
247 * @param name name
248 * @return the decorated Command
249 */
250 CommandPtr WithName(std::string_view name) &&;
251
252 /**
253 * Get a raw pointer to the held command.
254 */
255 Command* get() const&;
256
257 // Prevent calls on a temporary, as the returned pointer would be invalid
258 Command* get() && = delete;
259
260 /**
261 * Convert to the underlying unique_ptr.
262 */
263 std::unique_ptr<Command> Unwrap() &&;
264
265 /**
266 * Cancels this command. Will call End(true). Commands will be canceled
267 * regardless of interruption behavior.
268 */
269 void Cancel() const&;
270
271 // Prevent calls on a temporary, as the returned pointer would be invalid
272 void Cancel() && = delete;
273
274 /**
275 * Whether or not the command is currently scheduled. Note that this does not
276 * detect whether the command is in a composition, only whether it is directly
277 * being run by the scheduler.
278 *
279 * @return Whether the command is scheduled.
280 */
281 bool IsScheduled() const&;
282
283 // Prevent calls on a temporary, as the returned pointer would be invalid
284 void IsScheduled() && = delete;
285
286 /**
287 * Whether the command requires a given subsystem. Named "HasRequirement"
288 * rather than "requires" to avoid confusion with Command::Requires(Subsystem)
289 * -- this may be able to be changed in a few years.
290 *
291 * @param requirement the subsystem to inquire about
292 * @return whether the subsystem is required
293 */
294 bool HasRequirement(Subsystem* requirement) const&;
295
296 // Prevent calls on a temporary, as the returned pointer would be invalid
297 void HasRequirement(Subsystem* requirement) && = delete;
298
299 /**
300 * Check if this CommandPtr object is valid and wasn't moved-from.
301 */
302 explicit operator bool() const&;
303
304 // Prevent calls on a temporary, as the returned pointer would be invalid
305 explicit operator bool() && = delete;
306
307 /**
308 * Convert a vector of CommandPtr objects to their underlying unique_ptrs.
309 */
310 static std::vector<std::unique_ptr<Command>> UnwrapVector(
311 std::vector<CommandPtr>&& vec);
312
313 private:
314 std::unique_ptr<Command> m_ptr;
315 std::string m_moveOutSite{""};
316 void AssertValid() const;
317};
318
319} // namespace wpi::cmd
@ name
Definition base.h:690
typename std::decay< T >::type decay_t
Definition base.h:321
A state machine representing a complete action to be performed by the robot.
Definition Command.hpp:38
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition Command.hpp:170
CommandPtr OnlyWhile(std::function< bool()> condition) &&
Decorates this command with a run condition.
CommandPtr(CommandPtr &&)
CommandPtr FinallyDo(std::function< void()> end) &&
Decorates this command with a lambda to call on interrupt or end, following the command's inherent Co...
CommandPtr AlongWith(CommandPtr &&parallel) &&
Decorates this command with a set of commands to run parallel to it, ending when the last command end...
CommandPtr & operator=(CommandPtr &&)=default
CommandPtr Repeatedly() &&
Decorates this command to run repeatedly, restarting it when it ends, until this command is interrupt...
std::unique_ptr< Command > Unwrap() &&
Convert to the underlying unique_ptr.
CommandPtr AsProxy() &&
Decorates this command to run "by proxy" by wrapping it in a ProxyCommand.
CommandPtr(std::unique_ptr< Command > &&command)
CommandPtr DeadlineFor(CommandPtr &&parallel) &&
Decorates this command with a set of commands to run parallel to it, ending when the calling command ...
CommandPtr AndThen(std::function< void()> toRun, Requirements requirements={}) &&
Decorates this command with a runnable to run after the command finishes.
CommandPtr WithTimeout(wpi::units::second_t duration) &&
Decorates this command with a timeout.
bool HasRequirement(Subsystem *requirement) const &
Whether the command requires a given subsystem.
void Cancel() const &
Cancels this command.
CommandPtr AndThen(CommandPtr &&next) &&
Decorates this command with a set of commands to run after it in sequence.
CommandPtr FinallyDo(std::function< void(bool)> end) &&
Decorates this command with a lambda to call on interrupt or end, following the command's inherent Co...
CommandPtr WithDeadline(CommandPtr &&deadline) &&
Creates a new command that runs this command and the deadline in parallel, finishing (and interruptin...
CommandPtr Until(std::function< bool()> condition) &&
Decorates this command with an interrupt condition.
CommandPtr HandleInterrupt(std::function< void()> handler) &&
Decorates this command with a lambda to call on interrupt, following the command's inherent Command::...
CommandPtr OnlyIf(std::function< bool()> condition) &&
Decorates this command to only run if this condition is met.
CommandPtr(T &&command)
Definition CommandPtr.hpp:34
bool IsScheduled() const &
Whether or not the command is currently scheduled.
CommandPtr(std::nullptr_t)=delete
CommandPtr RaceWith(CommandPtr &&parallel) &&
Decorates this command with a set of commands to run parallel to it, ending when the first command en...
CommandPtr BeforeStarting(std::function< void()> toRun, Requirements requirements={}) &&
Decorates this command with a runnable to run before this command starts.
CommandPtr WithInterruptBehavior(Command::InterruptionBehavior interruptBehavior) &&
Decorates this command to have a different interrupt behavior.
CommandPtr IgnoringDisable(bool doesRunWhenDisabled) &&
Decorates this command to run or stop when disabled.
static std::vector< std::unique_ptr< Command > > UnwrapVector(std::vector< CommandPtr > &&vec)
Convert a vector of CommandPtr objects to their underlying unique_ptrs.
Command * get() const &
Get a raw pointer to the held command.
CommandPtr WithName(std::string_view name) &&
Decorates this Command with a name.
CommandPtr Unless(std::function< bool()> condition) &&
Decorates this command to only run if this condition is not met.
CommandPtr BeforeStarting(CommandPtr &&before) &&
Decorates this command with another command to run before this command starts.
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition Requirements.hpp:20
A robot subsystem.
Definition Subsystem.hpp:42
Definition StringMap.hpp:773
Definition CommandNiDsStadiaController.hpp:15