WPILibC++ 2024.3.2-102-g637647b
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 <type_traits>
15#include <utility>
16#include <vector>
17
19
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<Command, 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 <wpi::DecayedDerivedFrom<Command>... Commands>
57 explicit SequentialCommandGroup(Commands&&... commands) {
58 AddCommands(std::forward<Commands>(commands)...);
59 }
60
62
63 // No copy constructors for command groups
65
66 // Prevent template expansion from emulating copy ctor
68
69 /**
70 * Adds the given commands to the group.
71 *
72 * @param commands Commands to add, in order of execution.
73 */
74 template <wpi::DecayedDerivedFrom<Command>... Commands>
75 void AddCommands(Commands&&... commands) {
76 std::vector<std::unique_ptr<Command>> foo;
77 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
78 std::forward<Commands>(commands))),
79 ...);
80 AddCommands(std::move(foo));
81 }
82
83 void Initialize() final;
84
85 void Execute() final;
86
87 void End(bool interrupted) final;
88
89 bool IsFinished() final;
90
91 bool RunsWhenDisabled() const override;
92
93 Command::InterruptionBehavior GetInterruptionBehavior() const override;
94
95 void InitSendable(wpi::SendableBuilder& builder) override;
96
97 private:
98 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
99
100 wpi::SmallVector<std::unique_ptr<Command>, 4> m_commands;
101 size_t m_currentCommandIndex{invalid_index};
102 bool m_runWhenDisabled{true};
103 Command::InterruptionBehavior m_interruptBehavior{
105};
106} // namespace frc2
107
108#ifdef _WIN32
109#pragma warning(pop)
110#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:25
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:41
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:173
@ 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
bool RunsWhenDisabled() const override
SequentialCommandGroup(SequentialCommandGroup &&other)=default
SequentialCommandGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new SequentialCommandGroup.
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition: SequentialCommandGroup.h:75
void End(bool interrupted) final
Command::InterruptionBehavior GetInterruptionBehavior() const override
void InitSendable(wpi::SendableBuilder &builder) override
SequentialCommandGroup(SequentialCommandGroup &)=delete
SequentialCommandGroup(Commands &&... commands)
Creates a new SequentialCommandGroup.
Definition: SequentialCommandGroup.h:57
SequentialCommandGroup(const SequentialCommandGroup &)=delete
Definition: NotifierCommand.h:16
const size_t invalid_index
Definition: SequentialCommandGroup.h:24
Implement std::hash so that hash_code can be used in STL containers.
Definition: array.h:89
UnitTypeLhs() max(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3419
Definition: ntcore_cpp.h:26