WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ParallelDeadlineGroup.hpp
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
19
20namespace wpi::cmd {
21/**
22 * A command composition that runs a set of commands in parallel, ending only
23 * when a specific command (the "deadline") ends, interrupting all other
24 * commands that are still running at that point.
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 */
34 : public CommandHelper<Command, ParallelDeadlineGroup> {
35 public:
36 /**
37 * Creates a new ParallelDeadlineGroup. The given commands (including the
38 * deadline) will be executed simultaneously. The composition will finish when
39 * the deadline finishes, interrupting all other still-running commands. If
40 * the composition is interrupted, only the commands still running will be
41 * interrupted.
42 *
43 * @param deadline the command that determines when the composition ends
44 * @param commands the commands to be executed
45 */
46 ParallelDeadlineGroup(std::unique_ptr<Command>&& deadline,
47 std::vector<std::unique_ptr<Command>>&& commands);
48 /**
49 * Creates a new ParallelDeadlineGroup. The given commands (including the
50 * deadline) will be executed simultaneously. The composition will finish when
51 * the deadline finishes, interrupting all other still-running commands. If
52 * the composition is interrupted, only the commands still running will be
53 * interrupted.
54 *
55 * @param deadline the command that determines when the composition ends
56 * @param commands the commands to be executed
57 */
58 template <wpi::util::DecayedDerivedFrom<Command> T,
59 wpi::util::DecayedDerivedFrom<Command>... Commands>
60 explicit ParallelDeadlineGroup(T&& deadline, Commands&&... commands) {
61 SetDeadline(std::make_unique<std::decay_t<T>>(std::forward<T>(deadline)));
62 AddCommands(std::forward<Commands>(commands)...);
63 }
64
66
67 // No copy constructors for command groups
69
70 // Prevent template expansion from emulating copy ctor
72
73 /**
74 * Adds the given commands to the group.
75 *
76 * @param commands Commands to add to the group.
77 */
78 template <wpi::util::DecayedDerivedFrom<Command>... Commands>
79 void AddCommands(Commands&&... commands) {
80 std::vector<std::unique_ptr<Command>> foo;
81 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
82 std::forward<Commands>(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
97 Command::InterruptionBehavior GetInterruptionBehavior() const override;
98
99 void InitSendable(wpi::util::SendableBuilder& builder) override;
100
101 private:
102 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
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 wpi::cmd
114
115#ifdef _WIN32
116#pragma warning(pop)
117#endif
A state machine representing a complete action to be performed by the robot.
Definition Command.hpp:41
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition Command.hpp:173
@ kCancelIncoming
This command continues, and the incoming command is not scheduled.
Definition Command.hpp:182
ParallelDeadlineGroup(ParallelDeadlineGroup &&other)=default
void End(bool interrupted) final
ParallelDeadlineGroup(const ParallelDeadlineGroup &)=delete
ParallelDeadlineGroup(std::unique_ptr< Command > &&deadline, std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelDeadlineGroup.
Command::InterruptionBehavior GetInterruptionBehavior() const override
ParallelDeadlineGroup(T &&deadline, Commands &&... commands)
Creates a new ParallelDeadlineGroup.
Definition ParallelDeadlineGroup.hpp:60
ParallelDeadlineGroup(ParallelDeadlineGroup &)=delete
bool RunsWhenDisabled() const override
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition ParallelDeadlineGroup.hpp:79
void InitSendable(wpi::util::SendableBuilder &builder) override
Definition StringMap.hpp:773
Definition CommandNiDsStadiaController.hpp:15
Definition raw_os_ostream.hpp:19
Definition CvSource.hpp:15