[commands] Add convenience factories (#4460)

Co-authored-by: Starlight220 <53231611+Starlight220@users.noreply.github.com>
This commit is contained in:
Eli Barnett
2022-11-28 10:41:25 -05:00
committed by GitHub
parent 42b6d4e3f7
commit 1a59737f40
31 changed files with 1240 additions and 1 deletions

View File

@@ -105,6 +105,11 @@ CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
}
CommandPtr Command::WithName(std::string_view name) && {
SetName(name);
return std::move(*this).ToPtr();
}
void Command::Schedule() {
CommandScheduler::GetInstance().Schedule(this);
}
@@ -129,6 +134,8 @@ std::string Command::GetName() const {
return GetTypeName(*this);
}
void Command::SetName(std::string_view name) {}
bool Command::IsGrouped() const {
return m_isGrouped;
}

View File

@@ -219,6 +219,12 @@ CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
});
}
CommandPtr CommandPtr::WithName(std::string_view name) && {
AssertValid();
m_ptr->SetName(name);
return std::move(*this);
}
CommandBase* CommandPtr::get() const {
AssertValid();
return m_ptr.get();

View File

@@ -5,6 +5,7 @@
#include "frc2/command/Subsystem.h"
#include "frc2/command/CommandPtr.h"
#include "frc2/command/Commands.h"
using namespace frc2;
Subsystem::~Subsystem() {
@@ -31,3 +32,21 @@ Command* Subsystem::GetCurrentCommand() const {
void Subsystem::Register() {
return CommandScheduler::GetInstance().RegisterSubsystem(this);
}
CommandPtr Subsystem::RunOnce(std::function<void()> action) {
return cmd::RunOnce(std::move(action), {this});
}
CommandPtr Subsystem::Run(std::function<void()> action) {
return cmd::Run(std::move(action), {this});
}
CommandPtr Subsystem::StartEnd(std::function<void()> start,
std::function<void()> end) {
return cmd::StartEnd(std::move(start), std::move(end), {this});
}
CommandPtr Subsystem::RunEnd(std::function<void()> run,
std::function<void()> end) {
return cmd::RunEnd(std::move(run), std::move(end), {this});
}