mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[command] Add ignoringDisable decorator (#4305)
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "frc2/command/Command.h"
|
||||
|
||||
#include "frc2/command/CommandHelper.h"
|
||||
#include "frc2/command/CommandScheduler.h"
|
||||
#include "frc2/command/ConditionalCommand.h"
|
||||
#include "frc2/command/EndlessCommand.h"
|
||||
@@ -17,6 +18,7 @@
|
||||
#include "frc2/command/SequentialCommandGroup.h"
|
||||
#include "frc2/command/WaitCommand.h"
|
||||
#include "frc2/command/WaitUntilCommand.h"
|
||||
#include "frc2/command/WrapperCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
@@ -47,6 +49,24 @@ ParallelRaceGroup Command::Until(std::function<bool()> condition) && {
|
||||
return ParallelRaceGroup(std::move(temp));
|
||||
}
|
||||
|
||||
std::unique_ptr<Command> Command::IgnoringDisable(bool doesRunWhenDisabled) && {
|
||||
class RunsWhenDisabledCommand
|
||||
: public CommandHelper<WrapperCommand, RunsWhenDisabledCommand> {
|
||||
public:
|
||||
RunsWhenDisabledCommand(std::unique_ptr<Command>&& command,
|
||||
bool doesRunWhenDisabled)
|
||||
: CommandHelper(std::move(command)),
|
||||
m_runsWhenDisabled(doesRunWhenDisabled) {}
|
||||
bool RunsWhenDisabled() const override { return m_runsWhenDisabled; }
|
||||
|
||||
private:
|
||||
bool m_runsWhenDisabled;
|
||||
};
|
||||
|
||||
return std::make_unique<RunsWhenDisabledCommand>(
|
||||
std::move(*this).TransferOwnership(), doesRunWhenDisabled);
|
||||
}
|
||||
|
||||
ParallelRaceGroup Command::WithInterrupt(std::function<bool()> condition) && {
|
||||
std::vector<std::unique_ptr<Command>> temp;
|
||||
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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/WrapperCommand.h"
|
||||
|
||||
using namespace frc2;
|
||||
|
||||
WrapperCommand::WrapperCommand(std::unique_ptr<Command>&& command) {
|
||||
if (!CommandGroupBase::RequireUngrouped(*command)) {
|
||||
return;
|
||||
}
|
||||
m_command = std::move(command);
|
||||
m_command->SetGrouped(true);
|
||||
}
|
||||
|
||||
void WrapperCommand::Initialize() {
|
||||
m_command->Initialize();
|
||||
}
|
||||
|
||||
void WrapperCommand::Execute() {
|
||||
m_command->Execute();
|
||||
}
|
||||
|
||||
bool WrapperCommand::IsFinished() {
|
||||
return m_command->IsFinished();
|
||||
}
|
||||
|
||||
void WrapperCommand::End(bool interrupted) {
|
||||
m_command->End(interrupted);
|
||||
}
|
||||
|
||||
bool WrapperCommand::RunsWhenDisabled() const {
|
||||
return m_command->RunsWhenDisabled();
|
||||
}
|
||||
@@ -229,6 +229,14 @@ class Command {
|
||||
*/
|
||||
virtual ConditionalCommand Unless(std::function<bool()> condition) &&;
|
||||
|
||||
/**
|
||||
* Decorates this command to run or stop when disabled.
|
||||
*
|
||||
* @param doesRunWhenDisabled true to run when disabled.
|
||||
* @return the decorated command
|
||||
*/
|
||||
virtual std::unique_ptr<Command> IgnoringDisable(bool doesRunWhenDisabled) &&;
|
||||
|
||||
/**
|
||||
* Schedules this command.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
// 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
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4521)
|
||||
#endif
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "frc2/command/CommandBase.h"
|
||||
#include "frc2/command/CommandGroupBase.h"
|
||||
#include "frc2/command/CommandHelper.h"
|
||||
|
||||
namespace frc2 {
|
||||
/**
|
||||
* A class used internally to wrap commands while overriding a specific method;
|
||||
* all other methods will call through to the wrapped command.
|
||||
*
|
||||
* <p>Wrapped commands may only be used through the wrapper, trying to directly
|
||||
* schedule them or add them to a group will throw an exception.
|
||||
*/
|
||||
class WrapperCommand : public CommandHelper<CommandBase, WrapperCommand> {
|
||||
public:
|
||||
/**
|
||||
* Wrap a command.
|
||||
*
|
||||
* @param command the command being wrapped. Trying to directly schedule this
|
||||
* command or add it to a group will throw an exception.
|
||||
*/
|
||||
explicit WrapperCommand(std::unique_ptr<Command>&& command);
|
||||
|
||||
/**
|
||||
* Wrap a command.
|
||||
*
|
||||
* @param command the command being wrapped. Trying to directly schedule this
|
||||
* command or add it to a group will throw an exception.
|
||||
*/
|
||||
template <class T, typename = std::enable_if_t<std::is_base_of_v<
|
||||
Command, std::remove_reference_t<T>>>>
|
||||
explicit WrapperCommand(T&& command)
|
||||
: WrapperCommand(std::make_unique<std::remove_reference_t<T>>(
|
||||
std::forward<T>(command))) {}
|
||||
|
||||
WrapperCommand(WrapperCommand&& other) = default;
|
||||
|
||||
// No copy constructors for command groups
|
||||
WrapperCommand(const WrapperCommand& other) = delete;
|
||||
|
||||
// Prevent template expansion from emulating copy ctor
|
||||
WrapperCommand(WrapperCommand&) = delete;
|
||||
|
||||
void Initialize() override;
|
||||
|
||||
void Execute() override;
|
||||
|
||||
bool IsFinished() override;
|
||||
|
||||
void End(bool interrupted) override;
|
||||
|
||||
bool RunsWhenDisabled() const override;
|
||||
|
||||
protected:
|
||||
std::unique_ptr<Command> m_command;
|
||||
};
|
||||
} // namespace frc2
|
||||
|
||||
#ifdef _WIN32
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
Reference in New Issue
Block a user