mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-02 02:51:42 +00:00
[commands] Refactor ProxyScheduleCommand, SelectCommand into ProxyCommand (#4534)
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
#include "frc2/command/ParallelDeadlineGroup.h"
|
||||
#include "frc2/command/ParallelRaceGroup.h"
|
||||
#include "frc2/command/PerpetualCommand.h"
|
||||
#include "frc2/command/ProxyScheduleCommand.h"
|
||||
#include "frc2/command/RepeatCommand.h"
|
||||
#include "frc2/command/SequentialCommandGroup.h"
|
||||
#include "frc2/command/WaitCommand.h"
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "frc2/command/ParallelDeadlineGroup.h"
|
||||
#include "frc2/command/ParallelRaceGroup.h"
|
||||
#include "frc2/command/PrintCommand.h"
|
||||
#include "frc2/command/ProxyScheduleCommand.h"
|
||||
#include "frc2/command/ProxyCommand.h"
|
||||
#include "frc2/command/RepeatCommand.h"
|
||||
#include "frc2/command/SequentialCommandGroup.h"
|
||||
#include "frc2/command/WaitCommand.h"
|
||||
@@ -37,7 +37,7 @@ CommandPtr CommandPtr::Repeatedly() && {
|
||||
|
||||
CommandPtr CommandPtr::AsProxy() && {
|
||||
AssertValid();
|
||||
m_ptr = std::make_unique<ProxyScheduleCommand>(std::move(m_ptr));
|
||||
m_ptr = std::make_unique<ProxyCommand>(std::move(m_ptr));
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,9 +10,7 @@
|
||||
#include "frc2/command/ParallelCommandGroup.h"
|
||||
#include "frc2/command/ParallelDeadlineGroup.h"
|
||||
#include "frc2/command/ParallelRaceGroup.h"
|
||||
#include "frc2/command/PerpetualCommand.h"
|
||||
#include "frc2/command/PrintCommand.h"
|
||||
#include "frc2/command/ProxyScheduleCommand.h"
|
||||
#include "frc2/command/RunCommand.h"
|
||||
#include "frc2/command/SequentialCommandGroup.h"
|
||||
#include "frc2/command/WaitCommand.h"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#include "frc2/command/ProxyCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
ProxyCommand::ProxyCommand(wpi::unique_function<Command*()> supplier)
|
||||
: m_supplier(std::move(supplier)) {}
|
||||
|
||||
ProxyCommand::ProxyCommand(Command* command)
|
||||
: m_supplier([command] { return command; }) {}
|
||||
|
||||
ProxyCommand::ProxyCommand(std::unique_ptr<Command> command)
|
||||
: m_supplier([command = std::move(command)] { return command.get(); }) {}
|
||||
|
||||
void ProxyCommand::Initialize() {
|
||||
m_command = m_supplier();
|
||||
m_command->Schedule();
|
||||
}
|
||||
|
||||
void ProxyCommand::End(bool interrupted) {
|
||||
if (interrupted) {
|
||||
m_command->Cancel();
|
||||
}
|
||||
m_command = nullptr;
|
||||
}
|
||||
|
||||
void ProxyCommand::Execute() {}
|
||||
|
||||
bool ProxyCommand::IsFinished() {
|
||||
// because we're between `initialize` and `end`, `m_command` is necessarily
|
||||
// not null but if called otherwise and m_command is null, it's UB, so we can
|
||||
// do whatever we want -- like return true.
|
||||
return m_command == nullptr || !m_command->IsScheduled();
|
||||
}
|
||||
@@ -15,13 +15,6 @@ ProxyScheduleCommand::ProxyScheduleCommand(Command* toSchedule) {
|
||||
SetInsert(m_toSchedule, {&toSchedule, 1});
|
||||
}
|
||||
|
||||
ProxyScheduleCommand::ProxyScheduleCommand(
|
||||
std::unique_ptr<Command>&& toSchedule)
|
||||
: m_owning(std::move(toSchedule)) {
|
||||
Command* ptr = m_owning.get();
|
||||
SetInsert(m_toSchedule, {&ptr, 1});
|
||||
}
|
||||
|
||||
void ProxyScheduleCommand::Initialize() {
|
||||
for (auto* command : m_toSchedule) {
|
||||
command->Schedule();
|
||||
|
||||
@@ -25,7 +25,6 @@ std::string GetTypeName(const T& type) {
|
||||
}
|
||||
|
||||
class PerpetualCommand;
|
||||
class ProxyScheduleCommand;
|
||||
|
||||
/**
|
||||
* A state machine representing a complete action to be performed by the robot.
|
||||
@@ -225,7 +224,7 @@ safe) semantics.
|
||||
|
||||
/**
|
||||
* Decorates this command to run "by proxy" by wrapping it in a
|
||||
* ProxyScheduleCommand. This is useful for "forking off" from command groups
|
||||
* ProxyCommand. This is useful for "forking off" from command groups
|
||||
* when the user does not wish to extend the command's requirements to the
|
||||
* entire command group.
|
||||
*
|
||||
|
||||
@@ -49,7 +49,7 @@ class CommandPtr final {
|
||||
|
||||
/**
|
||||
* Decorates this command to run "by proxy" by wrapping it in a
|
||||
* ProxyScheduleCommand. This is useful for "forking off" from command groups
|
||||
* ProxyCommand. This is useful for "forking off" from command groups
|
||||
* when the user does not wish to extend the command's requirements to the
|
||||
* entire command group.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
// 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 <span>
|
||||
|
||||
#include <wpi/FunctionExtras.h>
|
||||
|
||||
#include "frc2/command/CommandBase.h"
|
||||
#include "frc2/command/CommandHelper.h"
|
||||
#include "frc2/command/SetUtilities.h"
|
||||
|
||||
namespace frc2 {
|
||||
/**
|
||||
* Schedules the given command when this command is initialized, and ends when
|
||||
* it ends. Useful for forking off from CommandGroups. If this command is
|
||||
* interrupted, it will cancel the command.
|
||||
*
|
||||
* <p>This class is provided by the NewCommands VendorDep
|
||||
*/
|
||||
class ProxyCommand : public CommandHelper<CommandBase, ProxyCommand> {
|
||||
public:
|
||||
/**
|
||||
* Creates a new ProxyCommand that schedules the supplied command when
|
||||
* initialized, and ends when it is no longer scheduled. Useful for lazily
|
||||
* creating commands at runtime.
|
||||
*
|
||||
* @param supplier the command supplier
|
||||
*/
|
||||
explicit ProxyCommand(wpi::unique_function<Command*()> supplier);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
void Execute() override;
|
||||
|
||||
bool IsFinished() override;
|
||||
|
||||
private:
|
||||
wpi::unique_function<Command*()> m_supplier;
|
||||
Command* m_command = nullptr;
|
||||
};
|
||||
} // namespace frc2
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <span>
|
||||
|
||||
#include <wpi/SmallVector.h>
|
||||
#include <wpi/deprecated.h>
|
||||
|
||||
#include "frc2/command/CommandBase.h"
|
||||
#include "frc2/command/CommandHelper.h"
|
||||
@@ -30,22 +31,14 @@ class ProxyScheduleCommand
|
||||
* initialized, and ends when they are all no longer scheduled.
|
||||
*
|
||||
* @param toSchedule the commands to schedule
|
||||
* @deprecated Replace with {@link ProxyCommand},
|
||||
* composing multiple of them in a {@link ParallelRaceGroup} if needed.
|
||||
*/
|
||||
WPI_DEPRECATED("Replace with ProxyCommand")
|
||||
explicit ProxyScheduleCommand(std::span<Command* const> toSchedule);
|
||||
|
||||
explicit ProxyScheduleCommand(Command* toSchedule);
|
||||
|
||||
/**
|
||||
* Creates a new ProxyScheduleCommand 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 ProxyScheduleCommand.
|
||||
*
|
||||
* @param toSchedule the command to schedule
|
||||
*/
|
||||
explicit ProxyScheduleCommand(std::unique_ptr<Command>&& toSchedule);
|
||||
|
||||
ProxyScheduleCommand(ProxyScheduleCommand&& other) = default;
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
@@ -108,7 +108,10 @@ class SelectCommand : public CommandHelper<CommandBase, SelectCommand<Key>> {
|
||||
* Creates a new selectcommand.
|
||||
*
|
||||
* @param toRun a supplier providing the command to run
|
||||
* @deprecated Replace with {@link ProxyCommand},
|
||||
* composing multiple of them in a {@link ParallelRaceGroup} if needed.
|
||||
*/
|
||||
WPI_DEPRECATED("Replace with ProxyCommand")
|
||||
explicit SelectCommand(std::function<Command*()> toRun)
|
||||
: m_toRun{std::move(toRun)} {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user