WPILibC++ 2024.3.2
ParallelDeadlineGroup.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 only
26 * when a specific command (the "deadline") ends, interrupting all other
27 * commands that are still running at that point.
28 *
29 * <p>The rules for command compositions apply: command instances that are
30 * passed to it are owned by the composition and cannot be added to any other
31 * composition or scheduled individually, and the composition requires all
32 * subsystems its components require.
33 *
34 * This class is provided by the NewCommands VendorDep
35 */
37 : public CommandHelper<Command, ParallelDeadlineGroup> {
38 public:
39 /**
40 * Creates a new ParallelDeadlineGroup. The given commands (including the
41 * deadline) will be executed simultaneously. The composition will finish when
42 * the deadline finishes, interrupting all other still-running commands. If
43 * the composition is interrupted, only the commands still running will be
44 * interrupted.
45 *
46 * @param deadline the command that determines when the composition ends
47 * @param commands the commands to be executed
48 */
49 ParallelDeadlineGroup(std::unique_ptr<Command>&& deadline,
50 std::vector<std::unique_ptr<Command>>&& commands);
51 /**
52 * Creates a new ParallelDeadlineGroup. The given commands (including the
53 * deadline) will be executed simultaneously. The composition will finish when
54 * the deadline finishes, interrupting all other still-running commands. If
55 * the composition is interrupted, only the commands still running will be
56 * interrupted.
57 *
58 * @param deadline the command that determines when the composition ends
59 * @param commands the commands to be executed
60 */
61 template <wpi::DecayedDerivedFrom<Command> T,
62 wpi::DecayedDerivedFrom<Command>... Commands>
63 explicit ParallelDeadlineGroup(T&& deadline, Commands&&... commands) {
64 SetDeadline(std::make_unique<std::decay_t<T>>(std::forward<T>(deadline)));
65 AddCommands(std::forward<Commands>(commands)...);
66 }
67
69
70 // No copy constructors for command groups
72
73 // Prevent template expansion from emulating copy ctor
75
76 /**
77 * Adds the given commands to the group.
78 *
79 * @param commands Commands to add to the group.
80 */
81 template <wpi::DecayedDerivedFrom<Command>... Commands>
82 void AddCommands(Commands&&... commands) {
83 std::vector<std::unique_ptr<Command>> foo;
84 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
85 std::forward<Commands>(commands))),
86 ...);
87 AddCommands(std::move(foo));
88 }
89
90 void Initialize() final;
91
92 void Execute() final;
93
94 void End(bool interrupted) final;
95
96 bool IsFinished() final;
97
98 bool RunsWhenDisabled() const override;
99
100 Command::InterruptionBehavior GetInterruptionBehavior() const override;
101
102 void InitSendable(wpi::SendableBuilder& builder) override;
103
104 private:
105 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
106
107 void SetDeadline(std::unique_ptr<Command>&& deadline);
108
109 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
110 Command* m_deadline;
111 bool m_runWhenDisabled{true};
112 Command::InterruptionBehavior m_interruptBehavior{
114 bool m_finished{true};
115};
116} // namespace frc2
117
118#ifdef _WIN32
119#pragma warning(pop)
120#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 command composition that runs a set of commands in parallel, ending only when a specific command (t...
Definition: ParallelDeadlineGroup.h:37
ParallelDeadlineGroup(ParallelDeadlineGroup &)=delete
ParallelDeadlineGroup(ParallelDeadlineGroup &&other)=default
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition: ParallelDeadlineGroup.h:82
ParallelDeadlineGroup(std::unique_ptr< Command > &&deadline, std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelDeadlineGroup.
Command::InterruptionBehavior GetInterruptionBehavior() const override
void InitSendable(wpi::SendableBuilder &builder) override
ParallelDeadlineGroup(const ParallelDeadlineGroup &)=delete
bool RunsWhenDisabled() const override
void End(bool interrupted) final
ParallelDeadlineGroup(T &&deadline, Commands &&... commands)
Creates a new ParallelDeadlineGroup.
Definition: ParallelDeadlineGroup.h:63
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89
Definition: ntcore_cpp.h:26