WPILibC++ 2023.4.3
SequentialCommandGroup.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#ifdef _WIN32
8#pragma warning(push)
9#pragma warning(disable : 4521)
10#endif
11
12#include <limits>
13#include <memory>
14#include <span>
15#include <type_traits>
16#include <utility>
17#include <vector>
18
21
22namespace frc2 {
23
25
26/**
27 * A command composition that runs a list of commands in sequence.
28 *
29 * <p>The rules for command compositions apply: command instances that are
30 * passed to it are owned by the composition and cannot be added to any other
31 * composition or scheduled individually, and the composition requires all
32 * subsystems its components require.
33 *
34 * This class is provided by the NewCommands VendorDep
35 */
37 : public CommandHelper<CommandGroupBase, SequentialCommandGroup> {
38 public:
39 /**
40 * Creates a new SequentialCommandGroup. The given commands will be run
41 * sequentially, with the composition finishing when the last command
42 * finishes.
43 *
44 * @param commands the commands to include in this composition.
45 */
47 std::vector<std::unique_ptr<Command>>&& commands);
48
49 /**
50 * Creates a new SequentialCommandGroup. The given commands will be run
51 * sequentially, with the composition finishing when the last command
52 * finishes.
53 *
54 * @param commands the commands to include in this composition.
55 */
56 template <class... Types,
57 typename = std::enable_if_t<std::conjunction_v<
58 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
59 explicit SequentialCommandGroup(Types&&... commands) {
60 AddCommands(std::forward<Types>(commands)...);
61 }
62
64
65 // No copy constructors for command groups
67
68 // Prevent template expansion from emulating copy ctor
70
71 template <class... Types,
72 typename = std::enable_if_t<std::conjunction_v<
73 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
74 void AddCommands(Types&&... commands) {
75 std::vector<std::unique_ptr<Command>> foo;
76 ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
77 std::forward<Types>(commands))),
78 ...);
79 AddCommands(std::move(foo));
80 }
81
82 void Initialize() final;
83
84 void Execute() final;
85
86 void End(bool interrupted) final;
87
88 bool IsFinished() final;
89
90 bool RunsWhenDisabled() const override;
91
93
94 void InitSendable(wpi::SendableBuilder& builder) override;
95
96 private:
97 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
98
99 wpi::SmallVector<std::unique_ptr<Command>, 4> m_commands;
100 size_t m_currentCommandIndex{invalid_index};
101 bool m_runWhenDisabled{true};
102 Command::InterruptionBehavior m_interruptBehavior{
104};
105} // namespace frc2
106
107#ifdef _WIN32
108#pragma warning(pop)
109#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:47
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:104
@ kCancelIncoming
This command continues, and the incoming command is not scheduled.
A command composition that runs a list of commands in sequence.
Definition: SequentialCommandGroup.h:37
void Initialize() final
The initial subroutine of a command.
void Execute() final
The main body of a command.
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
SequentialCommandGroup(SequentialCommandGroup &&other)=default
SequentialCommandGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new SequentialCommandGroup.
void End(bool interrupted) final
The action to take when the command ends.
SequentialCommandGroup(Types &&... commands)
Creates a new SequentialCommandGroup.
Definition: SequentialCommandGroup.h:59
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
void AddCommands(Types &&... commands)
Definition: SequentialCommandGroup.h:74
SequentialCommandGroup(SequentialCommandGroup &)=delete
bool IsFinished() final
Whether the command has finished.
SequentialCommandGroup(const SequentialCommandGroup &)=delete
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
Definition: InstantCommand.h:14
const size_t invalid_index
Definition: SequentialCommandGroup.h:24
Definition: StdDeque.h:50
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Definition: SmallVector.h:1112