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