2022-11-29 00:43:10 +02:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include <wpi/FunctionExtras.h>
|
|
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
#include "frc2/command/Command.h"
|
2022-11-29 00:43:10 +02:00
|
|
|
#include "frc2/command/CommandHelper.h"
|
|
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
2024-04-28 00:41:04 -05:00
|
|
|
* Schedules a given command when this command is initialized and ends when it
|
|
|
|
|
* ends, but does not directly run it. Use this for including a command in a
|
|
|
|
|
* composition without adding its requirements, <strong>but only if you know
|
|
|
|
|
* what you are doing. If you are unsure, see <a
|
|
|
|
|
* href="https://docs.wpilib.org/en/stable/docs/software/commandbased/command-compositions.html#scheduling-other-commands">the
|
|
|
|
|
* WPILib docs</a> for a complete explanation of proxy semantics.</strong> Do
|
|
|
|
|
* not proxy a command from a subsystem already required by the composition, or
|
|
|
|
|
* else the composition will cancel itself when the proxy is reached. If this
|
|
|
|
|
* command is interrupted, it will cancel the command.
|
2022-11-29 00:43:10 +02:00
|
|
|
*
|
|
|
|
|
* <p>This class is provided by the NewCommands VendorDep
|
|
|
|
|
*/
|
2023-07-14 01:12:01 -04:00
|
|
|
class ProxyCommand : public CommandHelper<Command, ProxyCommand> {
|
2022-11-29 00:43:10 +02:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new ProxyCommand that schedules the supplied command when
|
2024-04-28 00:41:04 -05:00
|
|
|
* initialized, and ends when it is no longer scheduled. Use this for lazily
|
|
|
|
|
* creating <strong>proxied</strong> commands at runtime. Proxying should only
|
|
|
|
|
* be done to escape from composition requirement semantics, so if only
|
|
|
|
|
* initialization time command construction is needed, use {@link
|
|
|
|
|
* DeferredCommand} instead.
|
2022-11-29 00:43:10 +02:00
|
|
|
*
|
|
|
|
|
* @param supplier the command supplier
|
2024-04-28 00:41:04 -05:00
|
|
|
* @see DeferredCommand
|
2022-11-29 00:43:10 +02:00
|
|
|
*/
|
|
|
|
|
explicit ProxyCommand(wpi::unique_function<Command*()> supplier);
|
|
|
|
|
|
2023-08-29 16:23:00 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a new ProxyCommand that schedules the supplied command when
|
2024-04-28 00:41:04 -05:00
|
|
|
* initialized, and ends when it is no longer scheduled. Use this for lazily
|
|
|
|
|
* creating <strong>proxied</strong> commands at runtime. Proxying should only
|
|
|
|
|
* be done to escape from composition requirement semantics, so if only
|
|
|
|
|
* initialization time command construction is needed, use {@link
|
|
|
|
|
* DeferredCommand} instead.
|
2023-08-29 16:23:00 -04:00
|
|
|
*
|
|
|
|
|
* @param supplier the command supplier
|
2024-04-28 00:41:04 -05:00
|
|
|
* @see DeferredCommand
|
2023-08-29 16:23:00 -04:00
|
|
|
*/
|
|
|
|
|
explicit ProxyCommand(wpi::unique_function<CommandPtr()> supplier);
|
|
|
|
|
|
2022-11-29 00:43:10 +02:00
|
|
|
/**
|
|
|
|
|
* Creates a new ProxyCommand that schedules the given command when
|
|
|
|
|
* initialized, and ends when it is no longer scheduled.
|
|
|
|
|
*
|
|
|
|
|
* @param command the command to run by proxy
|
|
|
|
|
*/
|
|
|
|
|
explicit ProxyCommand(Command* command);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new ProxyCommand that schedules the given command when
|
|
|
|
|
* initialized, and ends when it is no longer scheduled.
|
|
|
|
|
*
|
|
|
|
|
* <p>Note that this constructor passes ownership of the given command to the
|
|
|
|
|
* returned ProxyCommand.
|
|
|
|
|
*
|
|
|
|
|
* @param command the command to schedule
|
|
|
|
|
*/
|
|
|
|
|
explicit ProxyCommand(std::unique_ptr<Command> command);
|
|
|
|
|
|
|
|
|
|
ProxyCommand(ProxyCommand&& other) = default;
|
|
|
|
|
|
|
|
|
|
void Initialize() override;
|
|
|
|
|
|
|
|
|
|
void End(bool interrupted) override;
|
|
|
|
|
|
|
|
|
|
bool IsFinished() override;
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
void InitSendable(wpi::SendableBuilder& builder) override;
|
|
|
|
|
|
2022-11-29 00:43:10 +02:00
|
|
|
private:
|
|
|
|
|
wpi::unique_function<Command*()> m_supplier;
|
|
|
|
|
Command* m_command = nullptr;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc2
|