2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-18 10:57:43 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4521)
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
#include <limits>
|
|
|
|
|
#include <memory>
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <span>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <type_traits>
|
2019-08-25 23:55:59 -04:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include "frc2/command/CommandGroupBase.h"
|
|
|
|
|
#include "frc2/command/CommandHelper.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
|
|
|
|
|
const size_t invalid_index = std::numeric_limits<size_t>::max();
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A command composition that runs a list of commands in sequence.
|
2019-08-25 23:55:59 -04:00
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* <p>The rules for command compositions apply: command instances that are
|
|
|
|
|
* passed to it are owned by the composition and cannot be added to any other
|
|
|
|
|
* composition or scheduled individually, and the composition requires all
|
|
|
|
|
* subsystems its components require.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* This class is provided by the NewCommands VendorDep
|
2019-08-25 23:55:59 -04:00
|
|
|
*/
|
|
|
|
|
class SequentialCommandGroup
|
|
|
|
|
: public CommandHelper<CommandGroupBase, SequentialCommandGroup> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* Creates a new SequentialCommandGroup. The given commands will be run
|
|
|
|
|
* sequentially, with the composition finishing when the last command
|
2019-08-25 23:55:59 -04:00
|
|
|
* finishes.
|
|
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* @param commands the commands to include in this composition.
|
2019-08-25 23:55:59 -04:00
|
|
|
*/
|
|
|
|
|
explicit SequentialCommandGroup(
|
|
|
|
|
std::vector<std::unique_ptr<Command>>&& commands);
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* Creates a new SequentialCommandGroup. The given commands will be run
|
|
|
|
|
* sequentially, with the composition finishing when the last command
|
2019-08-25 23:55:59 -04:00
|
|
|
* finishes.
|
|
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* @param commands the commands to include in this composition.
|
2019-08-25 23:55:59 -04:00
|
|
|
*/
|
|
|
|
|
template <class... Types,
|
|
|
|
|
typename = std::enable_if_t<std::conjunction_v<
|
|
|
|
|
std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
|
|
|
|
|
explicit SequentialCommandGroup(Types&&... commands) {
|
|
|
|
|
AddCommands(std::forward<Types>(commands)...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SequentialCommandGroup(SequentialCommandGroup&& other) = default;
|
|
|
|
|
|
|
|
|
|
// No copy constructors for command groups
|
|
|
|
|
SequentialCommandGroup(const SequentialCommandGroup&) = delete;
|
|
|
|
|
|
2019-10-18 10:57:43 -04:00
|
|
|
// Prevent template expansion from emulating copy ctor
|
|
|
|
|
SequentialCommandGroup(SequentialCommandGroup&) = delete;
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
template <class... Types,
|
|
|
|
|
typename = std::enable_if_t<std::conjunction_v<
|
|
|
|
|
std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
|
|
|
|
|
void AddCommands(Types&&... commands) {
|
|
|
|
|
std::vector<std::unique_ptr<Command>> foo;
|
|
|
|
|
((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
|
|
|
|
|
std::forward<Types>(commands))),
|
|
|
|
|
...);
|
|
|
|
|
AddCommands(std::move(foo));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 19:15:28 +03:00
|
|
|
void Initialize() final;
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-08-31 19:15:28 +03:00
|
|
|
void Execute() final;
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-08-31 19:15:28 +03:00
|
|
|
void End(bool interrupted) final;
|
2019-08-25 23:55:59 -04:00
|
|
|
|
2022-08-31 19:15:28 +03:00
|
|
|
bool IsFinished() final;
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
bool RunsWhenDisabled() const override;
|
|
|
|
|
|
2022-11-28 02:23:56 +02:00
|
|
|
Command::InterruptionBehavior GetInterruptionBehavior() const override;
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
private:
|
|
|
|
|
void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
|
|
|
|
|
|
|
|
|
|
wpi::SmallVector<std::unique_ptr<Command>, 4> m_commands;
|
|
|
|
|
size_t m_currentCommandIndex{invalid_index};
|
|
|
|
|
bool m_runWhenDisabled{true};
|
2022-11-28 02:23:56 +02:00
|
|
|
Command::InterruptionBehavior m_interruptBehavior{
|
|
|
|
|
Command::InterruptionBehavior::kCancelIncoming};
|
2019-08-25 23:55:59 -04:00
|
|
|
};
|
|
|
|
|
} // namespace frc2
|
2019-10-18 10:57:43 -04:00
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif
|