WPILibC++ 2023.4.3
ProxyCommand.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
15
16namespace frc2 {
17/**
18 * Schedules the given command when this command is initialized, and ends when
19 * it ends. Useful for forking off from CommandGroups. If this command is
20 * interrupted, it will cancel the command.
21 *
22 * <p>This class is provided by the NewCommands VendorDep
23 */
24class ProxyCommand : public CommandHelper<CommandBase, ProxyCommand> {
25 public:
26 /**
27 * Creates a new ProxyCommand that schedules the supplied command when
28 * initialized, and ends when it is no longer scheduled. Useful for lazily
29 * creating commands at runtime.
30 *
31 * @param supplier the command supplier
32 */
34
35 /**
36 * Creates a new ProxyCommand that schedules the given command when
37 * initialized, and ends when it is no longer scheduled.
38 *
39 * @param command the command to run by proxy
40 */
41 explicit ProxyCommand(Command* command);
42
43 /**
44 * Creates a new ProxyCommand that schedules the given command when
45 * initialized, and ends when it is no longer scheduled.
46 *
47 * <p>Note that this constructor passes ownership of the given command to the
48 * returned ProxyCommand.
49 *
50 * @param command the command to schedule
51 */
52 explicit ProxyCommand(std::unique_ptr<Command> command);
53
54 ProxyCommand(ProxyCommand&& other) = default;
55
56 void Initialize() override;
57
58 void End(bool interrupted) override;
59
60 void Execute() override;
61
62 bool IsFinished() override;
63
64 void InitSendable(wpi::SendableBuilder& builder) override;
65
66 private:
67 wpi::unique_function<Command*()> m_supplier;
68 Command* m_command = nullptr;
69};
70} // 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:26
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:47
Schedules the given command when this command is initialized, and ends when it ends.
Definition: ProxyCommand.h:24
ProxyCommand(Command *command)
Creates a new ProxyCommand that schedules the given command when initialized, and ends when it is no ...
ProxyCommand(std::unique_ptr< Command > command)
Creates a new ProxyCommand that schedules the given command when initialized, and ends when it is no ...
ProxyCommand(wpi::unique_function< Command *()> supplier)
Creates a new ProxyCommand that schedules the supplied command when initialized, and ends when it is ...
void Initialize() override
The initial subroutine of a command.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
void Execute() override
The main body of a command.
bool IsFinished() override
Whether the command has finished.
ProxyCommand(ProxyCommand &&other)=default
void End(bool interrupted) override
The action to take when the command ends.
Definition: SendableBuilder.h:18
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:56
Definition: InstantCommand.h:14