// 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 #include #include #include #include #include "frc2/command/CommandBase.h" #include "frc2/command/CommandGroupBase.h" #include "frc2/command/PrintCommand.h" namespace frc2 { /** * Runs one of a selection of commands, either using a selector and a key to * command mapping, or a supplier that returns the command directly at runtime. * Does not actually schedule the selected command - rather, the command is run * through this command; this ensures that the command will behave as expected * if used as part of a CommandGroup. Requires the requirements of all included * commands, again to ensure proper functioning when used in a CommandGroup. If * this is undesired, consider using ScheduleCommand. * *

As this command contains multiple component commands within it, it is * technically a command group; the command instances that are passed to it * cannot be added to any other groups, or scheduled individually. * *

As a rule, CommandGroups require the union of the requirements of their * component commands. */ template class SelectCommand : public CommandHelper> { public: /** * Creates a new selectcommand. * * @param commands the map of commands to choose from * @param selector the selector to determine which command to run */ template >...>>> SelectCommand(std::function selector, std::pair... commands) : m_selector{std::move(selector)} { std::vector>> foo; ((void)foo.emplace_back(commands.first, std::make_unique>( std::move(commands.second))), ...); for (auto&& command : foo) { if (!CommandGroupBase::RequireUngrouped(command.second)) { return; } } for (auto&& command : foo) { this->AddRequirements(command.second->GetRequirements()); m_runsWhenDisabled &= command.second->RunsWhenDisabled(); m_commands.emplace(std::move(command.first), std::move(command.second)); } } SelectCommand( std::function selector, std::vector>>&& commands) : m_selector{std::move(selector)} { for (auto&& command : commands) { if (!CommandGroupBase::RequireUngrouped(command.second)) { return; } } for (auto&& command : commands) { this->AddRequirements(command.second->GetRequirements()); m_runsWhenDisabled &= command.second->RunsWhenDisabled(); m_commands.emplace(std::move(command.first), std::move(command.second)); } } // No copy constructors for command groups SelectCommand(const SelectCommand& other) = delete; // Prevent template expansion from emulating copy ctor SelectCommand(SelectCommand&) = delete; /** * Creates a new selectcommand. * * @param toRun a supplier providing the command to run */ explicit SelectCommand(std::function toRun) : m_toRun{std::move(toRun)} {} SelectCommand(SelectCommand&& other) = default; void Initialize() override; void Execute() override { m_selectedCommand->Execute(); } void End(bool interrupted) override { return m_selectedCommand->End(interrupted); } bool IsFinished() override { return m_selectedCommand->IsFinished(); } bool RunsWhenDisabled() const override { return m_runsWhenDisabled; } protected: std::unique_ptr TransferOwnership() && override { return std::make_unique(std::move(*this)); } private: std::unordered_map> m_commands; std::function m_selector; std::function m_toRun; Command* m_selectedCommand; bool m_runsWhenDisabled = true; }; template void SelectCommand::Initialize() { if (m_selector) { auto find = m_commands.find(m_selector()); if (find == m_commands.end()) { m_selectedCommand = new PrintCommand( "SelectCommand selector value does not correspond to any command!"); return; } m_selectedCommand = find->second.get(); } else { m_selectedCommand = m_toRun(); } m_selectedCommand->Initialize(); } } // namespace frc2 #ifdef _WIN32 #pragma warning(pop) #endif