WPILibC++ 2024.3.2-109-g8c420fa
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 proxy a DeferredCommand
45 * using the <code>AsProxy</code> decorator.
46 * @see DeferredCommand
47 */
49 [[deprecated("Proxy a DeferredCommand instead")]]
51
52 /**
53 * Creates a new ProxyCommand that schedules the supplied command when
54 * initialized, and ends when it is no longer scheduled. Use this for lazily
55 * creating <strong>proxied</strong> commands at runtime. Proxying should only
56 * be done to escape from composition requirement semantics, so if only
57 * initialization time command construction is needed, use {@link
58 * DeferredCommand} instead.
59 *
60 * @param supplier the command supplier
61 * @deprecated This constructor's similarity to {@link DeferredCommand} is
62 * confusing and opens potential footguns for users who do not fully
63 * understand the semantics and implications of proxying, but who simply want
64 * runtime construction. Users who do know what they are doing and need a
65 * supplier-constructed proxied command should instead proxy a DeferredCommand
66 * using the <code>AsProxy</code> decorator.
67 * @see DeferredCommand
68 */
69 [[deprecated("Proxy a DeferredCommand instead")]]
72
73 /**
74 * Creates a new ProxyCommand that schedules the given command when
75 * initialized, and ends when it is no longer scheduled.
76 *
77 * @param command the command to run by proxy
78 */
79 explicit ProxyCommand(Command* command);
80
81 /**
82 * Creates a new ProxyCommand that schedules the given command when
83 * initialized, and ends when it is no longer scheduled.
84 *
85 * <p>Note that this constructor passes ownership of the given command to the
86 * returned ProxyCommand.
87 *
88 * @param command the command to schedule
89 */
90 explicit ProxyCommand(std::unique_ptr<Command> command);
91
92 ProxyCommand(ProxyCommand&& other) = default;
93
94 void Initialize() override;
95
96 void End(bool interrupted) override;
97
98 bool IsFinished() override;
99
100 void InitSendable(wpi::SendableBuilder& builder) override;
101
102 private:
103 wpi::unique_function<Command*()> m_supplier;
104 Command* m_command = nullptr;
105};
106} // 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:58
#define WPI_IGNORE_DEPRECATED
Definition: deprecated.h:16
#define WPI_UNIGNORE_DEPRECATED
Definition: deprecated.h:27
Definition: StartEndCommand.h:13