[commands] Change grouping decorator impl to flatten nested group structures (#3335)

This commit is contained in:
Starlight220
2021-06-14 02:05:14 +03:00
committed by GitHub
parent b422665a3c
commit ef4ea84cb5
12 changed files with 126 additions and 17 deletions

View File

@@ -4,6 +4,8 @@
#include "frc2/command/SequentialCommandGroup.h"
#include "frc2/command/InstantCommand.h"
using namespace frc2;
SequentialCommandGroup::SequentialCommandGroup(
@@ -72,3 +74,33 @@ void SequentialCommandGroup::AddCommands(
m_commands.emplace_back(std::move(command));
}
}
SequentialCommandGroup SequentialCommandGroup::BeforeStarting(
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
// store all the commands
std::vector<std::unique_ptr<Command>> tmp;
tmp.emplace_back(
std::make_unique<InstantCommand>(std::move(toRun), requirements));
for (auto&& command : m_commands) {
command->SetGrouped(false);
tmp.emplace_back(std::move(command));
}
// reset current state
m_commands.clear();
m_requirements.clear();
m_runWhenDisabled = true;
// add the commands back
AddCommands(std::move(tmp));
return std::move(*this);
}
SequentialCommandGroup SequentialCommandGroup::AndThen(
std::function<void()> toRun, wpi::ArrayRef<Subsystem*> requirements) && {
std::vector<std::unique_ptr<Command>> tmp;
tmp.emplace_back(
std::make_unique<InstantCommand>(std::move(toRun), requirements));
AddCommands(std::move(tmp));
return std::move(*this);
}