WPILibC++ 2023.4.3
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 <utility>
14#include <vector>
15
18
19namespace frc2 {
20/**
21 * A composition that runs a set of commands in parallel, ending when any one of
22 * the commands ends and interrupting all the others.
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, ParallelRaceGroup> {
33 public:
34 /**
35 * Creates a new ParallelCommandRace. The given commands will be executed
36 * simultaneously, and will "race to the finish" - the first command to finish
37 * ends the entire command, with all other commands being interrupted.
38 *
39 * @param commands the commands to include in this composition.
40 */
41 explicit ParallelRaceGroup(std::vector<std::unique_ptr<Command>>&& commands);
42
43 template <class... Types,
44 typename = std::enable_if_t<std::conjunction_v<
45 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
46 explicit ParallelRaceGroup(Types&&... commands) {
47 AddCommands(std::forward<Types>(commands)...);
48 }
49
51
52 // No copy constructors for command groups
54
55 // Prevent template expansion from emulating copy ctor
57
58 template <class... Types>
59 void AddCommands(Types&&... commands) {
60 std::vector<std::unique_ptr<Command>> foo;
61 ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
62 std::forward<Types>(commands))),
63 ...);
64 AddCommands(std::move(foo));
65 }
66
67 void Initialize() final;
68
69 void Execute() final;
70
71 void End(bool interrupted) final;
72
73 bool IsFinished() final;
74
75 bool RunsWhenDisabled() const override;
76
78
79 private:
80 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
81
82 std::vector<std::unique_ptr<Command>> m_commands;
83 bool m_runWhenDisabled{true};
84 Command::InterruptionBehavior m_interruptBehavior{
86 bool m_finished{false};
87 bool isRunning = false;
88};
89} // namespace frc2
90
91#ifdef _WIN32
92#pragma warning(pop)
93#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 composition that runs a set of commands in parallel, ending when any one of the commands ends and i...
Definition: ParallelRaceGroup.h:32
ParallelRaceGroup(const ParallelRaceGroup &)=delete
bool IsFinished() final
Whether the command has finished.
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
ParallelRaceGroup(ParallelRaceGroup &&other)=default
void Execute() final
The main body of a command.
void Initialize() final
The initial subroutine of a command.
ParallelRaceGroup(ParallelRaceGroup &)=delete
void End(bool interrupted) final
The action to take when the command ends.
ParallelRaceGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelCommandRace.
ParallelRaceGroup(Types &&... commands)
Definition: ParallelRaceGroup.h:46
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
void AddCommands(Types &&... commands)
Definition: ParallelRaceGroup.h:59
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
Definition: InstantCommand.h:14
Definition: StdDeque.h:50