WPILibC++ 2023.4.3-108-ge5452e3
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 <concepts>
13#include <memory>
14#include <type_traits>
15#include <utility>
16#include <vector>
17
19
22
23namespace frc2 {
24/**
25 * A command composition that runs a set of commands in parallel, ending when
26 * the last command ends.
27 *
28 * <p>The rules for command compositions apply: command instances that are
29 * passed to it are owned by the composition and cannot be added to any other
30 * composition or scheduled individually, and the composition requires all
31 * subsystems its components require.
32 *
33 * This class is provided by the NewCommands VendorDep
34 */
36 : public CommandHelper<CommandBase, ParallelCommandGroup> {
37 public:
38 /**
39 * Creates a new ParallelCommandGroup. The given commands will be executed
40 * simultaneously. The command group will finish when the last command
41 * finishes. If the composition is interrupted, only the commands that are
42 * still running will be interrupted.
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 ParallelCommandGroup. The given commands will be executed
51 * simultaneously. The command group will finish when the last command
52 * finishes. If the composition is interrupted, only the commands that are
53 * still running will be interrupted.
54 *
55 * @param commands the commands to include in this composition.
56 */
57 template <wpi::DecayedDerivedFrom<Command>... Commands>
58 explicit ParallelCommandGroup(Commands&&... commands) {
59 AddCommands(std::forward<Commands>(commands)...);
60 }
61
63
64 // No copy constructors for command groups
66
67 // Prevent template expansion from emulating copy ctor
69
70 /**
71 * Adds the given commands to the group.
72 *
73 * @param commands Commands to add to the group.
74 */
75 template <wpi::DecayedDerivedFrom<Command>... Commands>
76 void AddCommands(Commands&&... commands) {
77 std::vector<std::unique_ptr<Command>> foo;
78 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
79 std::forward<Commands>(commands))),
80 ...);
81 AddCommands(std::move(foo));
82 }
83
84 void Initialize() final;
85
86 void Execute() final;
87
88 void End(bool interrupted) final;
89
90 bool IsFinished() final;
91
92 bool RunsWhenDisabled() const override;
93
94 Command::InterruptionBehavior GetInterruptionBehavior() const override;
95
96 private:
97 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
98
99 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
100 bool m_runWhenDisabled{true};
101 Command::InterruptionBehavior m_interruptBehavior{
103 bool isRunning = false;
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:25
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:44
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:101
@ 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:36
ParallelCommandGroup(ParallelCommandGroup &)=delete
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition: ParallelCommandGroup.h:76
ParallelCommandGroup(Commands &&... commands)
Creates a new ParallelCommandGroup.
Definition: ParallelCommandGroup.h:58
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: ProfiledPIDCommand.h:18
Definition: BFloat16.h:88