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