WPILibC++ 2024.3.2
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 the
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 runs the supplied command when
33 * initialized, and ends when it ends. Useful for lazily
34 * creating commands at runtime. The supplier will be called each time this
35 * command is initialized. The supplier <i>must</i> create a new Command each
36 * call.
37 *
38 * @param supplier The command supplier
39 * @param requirements The command requirements.
40 *
41 */
43 Requirements requirements);
44
45 DeferredCommand(DeferredCommand&& other) = default;
46
47 void Initialize() override;
48
49 void Execute() override;
50
51 void End(bool interrupted) override;
52
53 bool IsFinished() override;
54
55 void InitSendable(wpi::SendableBuilder& builder) override;
56
57 private:
58 wpi::unique_function<CommandPtr()> m_supplier;
59 std::unique_ptr<Command> m_command;
60};
61} // 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:27
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition: CommandPtr.h:29
Defers Command construction to runtime.
Definition: DeferredCommand.h:29
DeferredCommand(wpi::unique_function< CommandPtr()> supplier, Requirements requirements)
Creates a new DeferredCommand that runs the supplied command when initialized, and ends when it ends.
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: TrapezoidProfileSubsystem.h:12