mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Add Lambda support to InstantCommand (#1262)
This commit is contained in:
committed by
Peter Johnson
parent
59700882f1
commit
8b5dc53cc7
@@ -16,4 +16,30 @@ InstantCommand::InstantCommand(Subsystem& subsystem) : Command(subsystem) {}
|
||||
InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem)
|
||||
: Command(name, subsystem) {}
|
||||
|
||||
InstantCommand::InstantCommand(std::function<void()> func) : m_func(func) {}
|
||||
|
||||
InstantCommand::InstantCommand(Subsystem& subsystem, std::function<void()> func)
|
||||
: InstantCommand(subsystem) {
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
InstantCommand::InstantCommand(const wpi::Twine& name,
|
||||
std::function<void()> func)
|
||||
: InstantCommand(name) {
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
InstantCommand::InstantCommand(const wpi::Twine& name, Subsystem& subsystem,
|
||||
std::function<void()> func)
|
||||
: InstantCommand(name, subsystem) {
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
void InstantCommand::_Initialize() {
|
||||
Command::_Initialize();
|
||||
if (m_func) {
|
||||
m_func();
|
||||
}
|
||||
}
|
||||
|
||||
bool InstantCommand::IsFinished() { return true; }
|
||||
|
||||
@@ -7,9 +7,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <wpi/Twine.h>
|
||||
|
||||
#include "frc/commands/Command.h"
|
||||
#include "frc/commands/Subsystem.h"
|
||||
|
||||
namespace frc {
|
||||
|
||||
@@ -42,10 +45,45 @@ class InstantCommand : public Command {
|
||||
*/
|
||||
InstantCommand(const wpi::Twine& name, Subsystem& subsystem);
|
||||
|
||||
/**
|
||||
* Create a command that calls the given function when run.
|
||||
*
|
||||
* @param func The function to run when Initialize() is run.
|
||||
*/
|
||||
explicit InstantCommand(std::function<void()> func);
|
||||
|
||||
/**
|
||||
* Create a command that calls the given function when run.
|
||||
*
|
||||
* @param subsystem The subsystems that this command runs on.
|
||||
* @param func The function to run when Initialize() is run.
|
||||
*/
|
||||
InstantCommand(Subsystem& subsystem, std::function<void()> func);
|
||||
|
||||
/**
|
||||
* Create a command that calls the given function when run.
|
||||
*
|
||||
* @param name The name of the command.
|
||||
* @param func The function to run when Initialize() is run.
|
||||
*/
|
||||
InstantCommand(const wpi::Twine& name, std::function<void()> func);
|
||||
|
||||
/**
|
||||
* Create a command that calls the given function when run.
|
||||
*
|
||||
* @param name The name of the command.
|
||||
* @param subsystem The subsystems that this command runs on.
|
||||
* @param func The function to run when Initialize() is run.
|
||||
*/
|
||||
InstantCommand(const wpi::Twine& name, Subsystem& subsystem,
|
||||
std::function<void()> func);
|
||||
|
||||
InstantCommand() = default;
|
||||
virtual ~InstantCommand() = default;
|
||||
|
||||
protected:
|
||||
std::function<void()> m_func = nullptr;
|
||||
void _Initialize() override;
|
||||
bool IsFinished() override;
|
||||
};
|
||||
|
||||
|
||||
@@ -14,11 +14,14 @@ package edu.wpi.first.wpilibj.command;
|
||||
* {@link Command isFinished}.
|
||||
*/
|
||||
public class InstantCommand extends Command {
|
||||
private Runnable m_func;
|
||||
|
||||
public InstantCommand() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand} with the given name.
|
||||
*
|
||||
* @param name the name for this command
|
||||
*/
|
||||
public InstantCommand(String name) {
|
||||
@@ -27,6 +30,7 @@ public class InstantCommand extends Command {
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand} with the given requirement.
|
||||
*
|
||||
* @param subsystem the subsystem this command requires
|
||||
*/
|
||||
public InstantCommand(Subsystem subsystem) {
|
||||
@@ -35,6 +39,7 @@ public class InstantCommand extends Command {
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand} with the given name and requirement.
|
||||
*
|
||||
* @param name the name for this command
|
||||
* @param subsystem the subsystem this command requires
|
||||
*/
|
||||
@@ -42,8 +47,64 @@ public class InstantCommand extends Command {
|
||||
super(name, subsystem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand}.
|
||||
*
|
||||
* @param func the function to run when {@link Command#initialize() initialize()} is run
|
||||
*/
|
||||
public InstantCommand(Runnable func) {
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand}.
|
||||
*
|
||||
* @param name the name for this command
|
||||
* @param func the function to run when {@link Command#initialize() initialize()} is run
|
||||
*/
|
||||
public InstantCommand(String name, Runnable func) {
|
||||
super(name);
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand}.
|
||||
*
|
||||
* @param requirement the subsystem this command requires
|
||||
* @param func the function to run when {@link Command#initialize() initialize()} is run
|
||||
*/
|
||||
public InstantCommand(Subsystem requirement, Runnable func) {
|
||||
super(requirement);
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link InstantCommand InstantCommand}.
|
||||
*
|
||||
* @param name the name for this command
|
||||
* @param requirement the subsystem this command requires
|
||||
* @param func the function to run when {@link Command#initialize() initialize()} is run
|
||||
*/
|
||||
public InstantCommand(String name, Subsystem requirement, Runnable func) {
|
||||
super(name, requirement);
|
||||
m_func = func;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isFinished() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger the stored function.
|
||||
*
|
||||
* <p>Called just before this Command runs the first time.
|
||||
*/
|
||||
@Override
|
||||
protected void _initialize() {
|
||||
super._initialize();
|
||||
if (m_func != null) {
|
||||
m_func.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user