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