WPILibC++ 2023.4.3
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 <utility>
14#include <vector>
15
18
19namespace frc2 {
20/**
21 * A command composition that runs a set of commands in parallel, ending when
22 * the last command ends.
23 *
24 * <p>The rules for command compositions apply: command instances that are
25 * passed to it are owned by the composition and cannot be added to any other
26 * composition or scheduled individually, and the composition requires all
27 * subsystems its components require.
28 *
29 * This class is provided by the NewCommands VendorDep
30 */
32 : public CommandHelper<CommandGroupBase, ParallelCommandGroup> {
33 public:
34 /**
35 * Creates a new ParallelCommandGroup. The given commands will be executed
36 * simultaneously. The command group will finish when the last command
37 * finishes. If the composition is interrupted, only the commands that are
38 * still running will be interrupted.
39 *
40 * @param commands the commands to include in this composition.
41 */
43 std::vector<std::unique_ptr<Command>>&& commands);
44
45 /**
46 * Creates a new ParallelCommandGroup. The given commands will be executed
47 * simultaneously. The command group will finish when the last command
48 * finishes. If the composition is interrupted, only the commands that are
49 * still running will be interrupted.
50 *
51 * @param commands the commands to include in this composition.
52 */
53 template <class... Types,
54 typename = std::enable_if_t<std::conjunction_v<
55 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
56 explicit ParallelCommandGroup(Types&&... commands) {
57 AddCommands(std::forward<Types>(commands)...);
58 }
59
61
62 // No copy constructors for command groups
64
65 // Prevent template expansion from emulating copy ctor
67
68 template <class... Types,
69 typename = std::enable_if_t<std::conjunction_v<
70 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
71 void AddCommands(Types&&... commands) {
72 std::vector<std::unique_ptr<Command>> foo;
73 ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
74 std::forward<Types>(commands))),
75 ...);
76 AddCommands(std::move(foo));
77 }
78
79 void Initialize() final;
80
81 void Execute() final;
82
83 void End(bool interrupted) final;
84
85 bool IsFinished() final;
86
87 bool RunsWhenDisabled() const override;
88
90
91 private:
92 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
93
94 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
95 bool m_runWhenDisabled{true};
96 Command::InterruptionBehavior m_interruptBehavior{
98 bool isRunning = false;
99};
100} // namespace frc2
101
102#ifdef _WIN32
103#pragma warning(pop)
104#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 set of commands in parallel, ending when the last command ends.
Definition: ParallelCommandGroup.h:32
ParallelCommandGroup(ParallelCommandGroup &)=delete
bool IsFinished() final
Whether the command has finished.
void Execute() final
The main body of a command.
ParallelCommandGroup(ParallelCommandGroup &&other)=default
ParallelCommandGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelCommandGroup.
ParallelCommandGroup(Types &&... commands)
Creates a new ParallelCommandGroup.
Definition: ParallelCommandGroup.h:56
void AddCommands(Types &&... commands)
Definition: ParallelCommandGroup.h:71
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
void End(bool interrupted) final
The action to take when the command ends.
void Initialize() final
The initial subroutine of a command.
ParallelCommandGroup(const ParallelCommandGroup &)=delete
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
Definition: InstantCommand.h:14
Definition: StdDeque.h:50