[commands] Fix C++ Select() factory (#5024)

Update example to use it.
This commit is contained in:
Starlight220
2023-01-29 17:23:12 +02:00
committed by GitHub
parent f75acd11ce
commit 4054893669
3 changed files with 17 additions and 14 deletions

View File

@@ -6,8 +6,10 @@
#include <functional>
#include <initializer_list>
#include <memory>
#include <span>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
@@ -156,13 +158,15 @@ namespace cmd {
* @param selector the selector function
* @param commands map of commands to select from
*/
template <typename Key>
[[nodiscard]] CommandPtr Select(
std::function<Key()> selector,
std::vector<std::pair<Key, CommandPtr>> commands) {
return SelectCommand(std::move(selector),
CommandPtr::UnwrapVector(std::move(commands)))
.ToPtr();
template <typename Key, class... Types>
[[nodiscard]] CommandPtr Select(std::function<Key()> selector,
std::pair<Key, Types>&&... commands) {
std::vector<std::pair<Key, std::unique_ptr<Command>>> vec;
((void)vec.emplace_back(commands.first, std::move(commands.second).Unwrap()),
...);
return SelectCommand(std::move(selector), std::move(vec)).ToPtr();
}
// Command Groups

View File

@@ -17,5 +17,5 @@ void RobotContainer::ConfigureButtonBindings() {
frc2::Command* RobotContainer::GetAutonomousCommand() {
// Run the select command in autonomous
return &m_exampleSelectCommand;
return m_exampleSelectCommand.get();
}

View File

@@ -5,8 +5,7 @@
#pragma once
#include <frc2/command/Command.h>
#include <frc2/command/PrintCommand.h>
#include <frc2/command/SelectCommand.h>
#include <frc2/command/Commands.h>
/**
* This class is where the bulk of the robot should be declared. Since
@@ -36,12 +35,12 @@ class RobotContainer {
// value returned by the selector method at runtime. Note that selectcommand
// takes a generic type, so the selector does not have to be an enum; it could
// be any desired type (string, integer, boolean, double...)
frc2::SelectCommand<CommandSelector> m_exampleSelectCommand{
frc2::CommandPtr m_exampleSelectCommand = frc2::cmd::Select<CommandSelector>(
[this] { return Select(); },
// Maps selector values to commands
std::pair{ONE, frc2::PrintCommand{"Command one was selected!"}},
std::pair{TWO, frc2::PrintCommand{"Command two was selected!"}},
std::pair{THREE, frc2::PrintCommand{"Command three was selected!"}}};
std::pair{ONE, frc2::cmd::Print("Command one was selected!")},
std::pair{TWO, frc2::cmd::Print("Command two was selected!")},
std::pair{THREE, frc2::cmd::Print("Command three was selected!")});
void ConfigureButtonBindings();
};