WPILibC++ 2023.4.3
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 <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 only
22 * when a specific command (the "deadline") ends, interrupting all other
23 * commands that are still running at that point.
24 *
25 * <p>The rules for command compositions apply: command instances that are
26 * passed to it are owned by the composition and cannot be added to any other
27 * composition or scheduled individually, and the composition requires all
28 * subsystems its components require.
29 *
30 * This class is provided by the NewCommands VendorDep
31 */
33 : public CommandHelper<CommandGroupBase, ParallelDeadlineGroup> {
34 public:
35 /**
36 * Creates a new ParallelDeadlineGroup. The given commands (including the
37 * deadline) will be executed simultaneously. The composition will finish when
38 * the deadline finishes, interrupting all other still-running commands. If
39 * the composition is interrupted, only the commands still running will be
40 * interrupted.
41 *
42 * @param deadline the command that determines when the composition ends
43 * @param commands the commands to be executed
44 */
45 ParallelDeadlineGroup(std::unique_ptr<Command>&& deadline,
46 std::vector<std::unique_ptr<Command>>&& commands);
47 /**
48 * Creates a new ParallelDeadlineGroup. The given commands (including the
49 * deadline) will be executed simultaneously. The composition will finish when
50 * the deadline finishes, interrupting all other still-running commands. If
51 * the composition is interrupted, only the commands still running will be
52 * interrupted.
53 *
54 * @param deadline the command that determines when the composition ends
55 * @param commands the commands to be executed
56 */
57 template <class T, class... Types,
58 typename = std::enable_if_t<
59 std::is_base_of_v<Command, std::remove_reference_t<T>>>,
60 typename = std::enable_if_t<std::conjunction_v<
61 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
62 explicit ParallelDeadlineGroup(T&& deadline, Types&&... commands) {
63 SetDeadline(std::make_unique<std::remove_reference_t<T>>(
64 std::forward<T>(deadline)));
65 AddCommands(std::forward<Types>(commands)...);
66 }
67
69
70 // No copy constructors for command groups
72
73 // Prevent template expansion from emulating copy ctor
75
76 template <class... Types,
77 typename = std::enable_if_t<std::conjunction_v<
78 std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
79 void AddCommands(Types&&... commands) {
80 std::vector<std::unique_ptr<Command>> foo;
81 ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
82 std::forward<Types>(commands))),
83 ...);
84 AddCommands(std::move(foo));
85 }
86
87 void Initialize() final;
88
89 void Execute() final;
90
91 void End(bool interrupted) final;
92
93 bool IsFinished() final;
94
95 bool RunsWhenDisabled() const override;
96
98
99 void InitSendable(wpi::SendableBuilder& builder) override;
100
101 private:
102 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
103
104 void SetDeadline(std::unique_ptr<Command>&& deadline);
105
106 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
107 Command* m_deadline;
108 bool m_runWhenDisabled{true};
109 Command::InterruptionBehavior m_interruptBehavior{
111 bool m_finished{true};
112};
113} // namespace frc2
114
115#ifdef _WIN32
116#pragma warning(pop)
117#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 only when a specific command (t...
Definition: ParallelDeadlineGroup.h:33
ParallelDeadlineGroup(T &&deadline, Types &&... commands)
Creates a new ParallelDeadlineGroup.
Definition: ParallelDeadlineGroup.h:62
ParallelDeadlineGroup(ParallelDeadlineGroup &)=delete
void Initialize() final
The initial subroutine of a command.
void AddCommands(Types &&... commands)
Definition: ParallelDeadlineGroup.h:79
bool IsFinished() final
Whether the command has finished.
void Execute() final
The main body of a command.
ParallelDeadlineGroup(ParallelDeadlineGroup &&other)=default
ParallelDeadlineGroup(std::unique_ptr< Command > &&deadline, std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelDeadlineGroup.
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
ParallelDeadlineGroup(const ParallelDeadlineGroup &)=delete
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
void End(bool interrupted) final
The action to take when the command ends.
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
Definition: InstantCommand.h:14
Definition: StdDeque.h:50
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18