WPILibC++ 2024.3.1
ParallelRaceGroup.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 composition that runs a set of commands in parallel, ending when any one of
26 * the commands ends and interrupting all the others.
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 */
35class ParallelRaceGroup : public CommandHelper<Command, ParallelRaceGroup> {
36 public:
37 /**
38 * Creates a new ParallelCommandRace. The given commands will be executed
39 * simultaneously, and will "race to the finish" - the first command to finish
40 * ends the entire command, with all other commands being interrupted.
41 *
42 * @param commands the commands to include in this composition.
43 */
44 explicit ParallelRaceGroup(std::vector<std::unique_ptr<Command>>&& commands);
45
46 template <wpi::DecayedDerivedFrom<Command>... Commands>
47 explicit ParallelRaceGroup(Commands&&... commands) {
48 AddCommands(std::forward<Commands>(commands)...);
49 }
50
52
53 // No copy constructors for command groups
55
56 // Prevent template expansion from emulating copy ctor
58
59 /**
60 * Adds the given commands to the group.
61 *
62 * @param commands Commands to add to the group.
63 */
64 template <wpi::DecayedDerivedFrom<Command>... Commands>
65 void AddCommands(Commands&&... commands) {
66 std::vector<std::unique_ptr<Command>> foo;
67 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
68 std::forward<Commands>(commands))),
69 ...);
70 AddCommands(std::move(foo));
71 }
72
73 void Initialize() final;
74
75 void Execute() final;
76
77 void End(bool interrupted) final;
78
79 bool IsFinished() final;
80
81 bool RunsWhenDisabled() const override;
82
83 Command::InterruptionBehavior GetInterruptionBehavior() const override;
84
85 private:
86 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
87
88 std::vector<std::unique_ptr<Command>> m_commands;
89 bool m_runWhenDisabled{true};
90 Command::InterruptionBehavior m_interruptBehavior{
92 bool m_finished{false};
93 bool isRunning = false;
94};
95} // namespace frc2
96
97#ifdef _WIN32
98#pragma warning(pop)
99#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 composition that runs a set of commands in parallel, ending when any one of the commands ends and i...
Definition: ParallelRaceGroup.h:35
ParallelRaceGroup(const ParallelRaceGroup &)=delete
Command::InterruptionBehavior GetInterruptionBehavior() const override
ParallelRaceGroup(ParallelRaceGroup &&other)=default
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition: ParallelRaceGroup.h:65
ParallelRaceGroup(ParallelRaceGroup &)=delete
void End(bool interrupted) final
ParallelRaceGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelCommandRace.
ParallelRaceGroup(Commands &&... commands)
Definition: ParallelRaceGroup.h:47
bool RunsWhenDisabled() const override
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89