WPILibC++ 2024.3.2-107-g0f45fe9
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#include <span>
9
10#include <wpi/FunctionExtras.h>
11
16
17namespace frc2 {
18/**
19 * Defers Command construction to runtime. Runs the command returned by a
20 * supplier when this command is initialized, and ends when it ends. Useful for
21 * performing runtime tasks before creating a new command. If this command is
22 * interrupted, it will cancel the command.
23 *
24 * Note that the supplier <i>must</i> create a new Command each call. For
25 * selecting one of a preallocated set of commands, use SelectCommand.
26 *
27 * <p>This class is provided by the NewCommands VendorDep
28 */
29class DeferredCommand : public CommandHelper<Command, DeferredCommand> {
30 public:
31 /**
32 * Creates a new DeferredCommand that directly runs the supplied command when
33 * initialized, and ends when it ends. Useful for lazily creating commands
34 * when the DeferredCommand is initialized, such as if the supplied command
35 * depends on runtime state. The supplier will be called each time this
36 * command is initialized. The supplier <i>must</i> create a new Command each
37 * call.
38 *
39 * @param supplier The command supplier
40 * @param requirements The command requirements.
41 *
42 */
44 Requirements requirements);
45
46 DeferredCommand(DeferredCommand&& other) = default;
47
48 void Initialize() override;
49
50 void Execute() override;
51
52 void End(bool interrupted) override;
53
54 bool IsFinished() override;
55
56 void InitSendable(wpi::SendableBuilder& builder) override;
57
58 private:
59 wpi::unique_function<CommandPtr()> m_supplier;
60 std::unique_ptr<Command> m_command;
61};
62} // 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:29
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:58
Definition: NotifierCommand.h:16