WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
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
10#include <wpi/deprecated.h>
11
14
15namespace frc2 {
16/**
17 * Schedules a given command when this command is initialized and ends when it
18 * ends, but does not directly run it. Use this for including a command in a
19 * composition without adding its requirements, <strong>but only if you know
20 * what you are doing. If you are unsure, see <a
21 * href="https://docs.wpilib.org/en/stable/docs/software/commandbased/command-compositions.html#scheduling-other-commands">the
22 * WPILib docs</a> for a complete explanation of proxy semantics.</strong> Do
23 * not proxy a command from a subsystem already required by the composition, or
24 * else the composition will cancel itself when the proxy is reached. If this
25 * command is interrupted, it will cancel the command.
26 *
27 * <p>This class is provided by the NewCommands VendorDep
28 */
29class ProxyCommand : public CommandHelper<Command, ProxyCommand> {
30 public:
31 /**
32 * Creates a new ProxyCommand that schedules the supplied command when
33 * initialized, and ends when it is no longer scheduled. Use this for lazily
34 * creating <strong>proxied</strong> commands at runtime. Proxying should only
35 * be done to escape from composition requirement semantics, so if only
36 * initialization time command construction is needed, use {@link
37 * DeferredCommand} instead.
38 *
39 * @param supplier the command supplier
40 * @deprecated This constructor's similarity to {@link DeferredCommand} is
41 * confusing and opens potential footguns for users who do not fully
42 * understand the semantics and implications of proxying, but who simply want
43 * runtime construction. Users who do know what they are doing and need a
44 * supplier-constructed proxied command should instead defer a proxy command.
45 * @see DeferredCommand
46 */
48 [[deprecated("Defer a proxy command instead.")]]
50
51 /**
52 * Creates a new ProxyCommand that schedules the supplied command when
53 * initialized, and ends when it is no longer scheduled. Use this for lazily
54 * creating <strong>proxied</strong> commands at runtime. Proxying should only
55 * be done to escape from composition requirement semantics, so if only
56 * initialization time command construction is needed, use {@link
57 * DeferredCommand} instead.
58 *
59 * @param supplier the command supplier
60 * @deprecated This constructor's similarity to {@link DeferredCommand} is
61 * confusing and opens potential footguns for users who do not fully
62 * understand the semantics and implications of proxying, but who simply want
63 * runtime construction. Users who do know what they are doing and need a
64 * supplier-constructed proxied command should instead defer a proxy command.
65 * @see DeferredCommand
66 */
67 [[deprecated("Defer a proxy command instead.")]]
70
71 /**
72 * Creates a new ProxyCommand that schedules the given command when
73 * initialized, and ends when it is no longer scheduled.
74 *
75 * @param command the command to run by proxy
76 */
77 explicit ProxyCommand(Command* command);
78
79 /**
80 * Creates a new ProxyCommand that schedules the given command when
81 * initialized, and ends when it is no longer scheduled.
82 *
83 * <p>Note that this constructor passes ownership of the given command to the
84 * returned ProxyCommand.
85 *
86 * @param command the command to schedule
87 */
88 explicit ProxyCommand(std::unique_ptr<Command> command);
89
90 ProxyCommand(ProxyCommand&& other) = default;
91
92 void Initialize() override;
93
94 void End(bool interrupted) override;
95
96 bool IsFinished() override;
97
98 void InitSendable(wpi::SendableBuilder& builder) override;
99
100 private:
101 wpi::unique_function<Command*()> m_supplier;
102 Command* m_command = nullptr;
103};
104} // 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 state machine representing a complete action to be performed by the robot.
Definition Command.h:41
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition CommandPtr.h:28
Schedules a given command when this command is initialized and ends when it ends, but does not direct...
Definition ProxyCommand.h:29
ProxyCommand(std::unique_ptr< Command > command)
Creates a new ProxyCommand that schedules the given command when initialized, and ends when it is no ...
WPI_IGNORE_DEPRECATED ProxyCommand(wpi::unique_function< Command *()> supplier)
Creates a new ProxyCommand that schedules the supplied command when initialized, and ends when it is ...
WPI_UNIGNORE_DEPRECATED ProxyCommand(Command *command)
Creates a new ProxyCommand that schedules the given command when initialized, and ends when it is no ...
void Initialize() override
void InitSendable(wpi::SendableBuilder &builder) override
bool IsFinished() override
ProxyCommand(ProxyCommand &&other)=default
void End(bool interrupted) override
ProxyCommand(wpi::unique_function< CommandPtr()> supplier)
Creates a new ProxyCommand that schedules the supplied command when initialized, and ends when it is ...
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
#define WPI_IGNORE_DEPRECATED
Definition deprecated.h:16
#define WPI_UNIGNORE_DEPRECATED
Definition deprecated.h:27
Definition FunctionalCommand.h:13