WPILibC++ 2024.3.2
ParallelCommandGroup.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 <memory>
13#include <type_traits>
14#include <utility>
15#include <vector>
16
18
20
21namespace frc2 {
22/**
23 * A command composition that runs a set of commands in parallel, ending when
24 * the last command ends.
25 *
26 * <p>The rules for command compositions apply: command instances that are
27 * passed to it are owned by the composition and cannot be added to any other
28 * composition or scheduled individually, and the composition requires all
29 * subsystems its components require.
30 *
31 * This class is provided by the NewCommands VendorDep
32 */
34 : public CommandHelper<Command, ParallelCommandGroup> {
35 public:
36 /**
37 * Creates a new ParallelCommandGroup. The given commands will be executed
38 * simultaneously. The command group will finish when the last command
39 * finishes. If the composition is interrupted, only the commands that are
40 * still running will be interrupted.
41 *
42 * @param commands the commands to include in this composition.
43 */
45 std::vector<std::unique_ptr<Command>>&& commands);
46
47 /**
48 * Creates a new ParallelCommandGroup. The given commands will be executed
49 * simultaneously. The command group will finish when the last command
50 * finishes. If the composition is interrupted, only the commands that are
51 * still running will be interrupted.
52 *
53 * @param commands the commands to include in this composition.
54 */
55 template <wpi::DecayedDerivedFrom<Command>... Commands>
56 explicit ParallelCommandGroup(Commands&&... commands) {
57 AddCommands(std::forward<Commands>(commands)...);
58 }
59
61
62 // No copy constructors for command groups
64
65 // Prevent template expansion from emulating copy ctor
67
68 /**
69 * Adds the given commands to the group.
70 *
71 * @param commands Commands to add to the group.
72 */
73 template <wpi::DecayedDerivedFrom<Command>... Commands>
74 void AddCommands(Commands&&... commands) {
75 std::vector<std::unique_ptr<Command>> foo;
76 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
77 std::forward<Commands>(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
92 Command::InterruptionBehavior GetInterruptionBehavior() const override;
93
94 private:
95 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
96
97 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
98 bool m_runWhenDisabled{true};
99 Command::InterruptionBehavior m_interruptBehavior{
101 bool isRunning = false;
102};
103} // namespace frc2
104
105#ifdef _WIN32
106#pragma warning(pop)
107#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
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 set of commands in parallel, ending when the last command ends.
Definition: ParallelCommandGroup.h:34
ParallelCommandGroup(ParallelCommandGroup &)=delete
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition: ParallelCommandGroup.h:74
ParallelCommandGroup(Commands &&... commands)
Creates a new ParallelCommandGroup.
Definition: ParallelCommandGroup.h:56
ParallelCommandGroup(ParallelCommandGroup &&other)=default
ParallelCommandGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelCommandGroup.
Command::InterruptionBehavior GetInterruptionBehavior() const override
void End(bool interrupted) final
ParallelCommandGroup(const ParallelCommandGroup &)=delete
bool RunsWhenDisabled() const override
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89