WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
DeferredCommand.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 <memory>
8
10
14
15namespace frc2 {
16/**
17 * Defers Command construction to runtime. Runs the command returned by a
18 * supplier when this command is initialized, and ends when it ends. Useful for
19 * performing runtime tasks before creating a new command. If this command is
20 * interrupted, it will cancel the command.
21 *
22 * Note that the supplier <i>must</i> create a new Command each call. For
23 * selecting one of a preallocated set of commands, use SelectCommand.
24 *
25 * <p>This class is provided by the NewCommands VendorDep
26 */
27class DeferredCommand : public CommandHelper<Command, DeferredCommand> {
28 public:
29 /**
30 * Creates a new DeferredCommand that directly runs the supplied command when
31 * initialized, and ends when it ends. Useful for lazily creating commands
32 * when the DeferredCommand is initialized, such as if the supplied command
33 * depends on runtime state. The supplier will be called each time this
34 * command is initialized. The supplier <i>must</i> create a new Command each
35 * call.
36 *
37 * @param supplier The command supplier
38 * @param requirements The command requirements.
39 *
40 */
42 Requirements requirements);
43
44 DeferredCommand(DeferredCommand&& other) = default;
45
46 void Initialize() override;
47
48 void Execute() override;
49
50 void End(bool interrupted) override;
51
52 bool IsFinished() override;
53
54 void InitSendable(wpi::SendableBuilder& builder) override;
55
56 private:
57 wpi::unique_function<CommandPtr()> m_supplier;
58 std::unique_ptr<Command> m_command;
59};
60} // namespace frc2
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
CRTP implementation to allow polymorphic decorator functions in Command.
Definition CommandHelper.h:25
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition CommandPtr.h:28
Defers Command construction to runtime.
Definition DeferredCommand.h:27
DeferredCommand(wpi::unique_function< CommandPtr()> supplier, Requirements requirements)
Creates a new DeferredCommand that directly runs the supplied command when initialized,...
void Execute() override
DeferredCommand(DeferredCommand &&other)=default
void InitSendable(wpi::SendableBuilder &builder) override
void Initialize() override
bool IsFinished() override
void End(bool interrupted) override
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition Requirements.h:20
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.h:21
unique_function is a type-erasing functor similar to std::function.
Definition FunctionExtras.h:57
Definition FunctionalCommand.h:13