WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
ParallelCommandGroup.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 when
23 * the last command ends.
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<Command, ParallelCommandGroup> {
34 public:
35 /**
36 * Creates a new ParallelCommandGroup. The given commands will be executed
37 * simultaneously. The command group will finish when the last command
38 * finishes. If the composition is interrupted, only the commands that are
39 * still running will be interrupted.
40 *
41 * @param commands the commands to include in this composition.
42 */
44 std::vector<std::unique_ptr<Command>>&& commands);
45
46 /**
47 * Creates a new ParallelCommandGroup. The given commands will be executed
48 * simultaneously. The command group will finish when the last command
49 * finishes. If the composition is interrupted, only the commands that are
50 * still running will be interrupted.
51 *
52 * @param commands the commands to include in this composition.
53 */
54 template <wpi::util::DecayedDerivedFrom<Command>... Commands>
55 explicit ParallelCommandGroup(Commands&&... commands) {
56 AddCommands(std::forward<Commands>(commands)...);
57 }
58
60
61 // No copy constructors for command groups
63
64 // Prevent template expansion from emulating copy ctor
66
67 /**
68 * Adds the given commands to the group.
69 *
70 * @param commands Commands to add to the group.
71 */
72 template <wpi::util::DecayedDerivedFrom<Command>... Commands>
73 void AddCommands(Commands&&... commands) {
74 std::vector<std::unique_ptr<Command>> foo;
75 ((void)foo.emplace_back(std::make_unique<std::decay_t<Commands>>(
76 std::forward<Commands>(commands))),
77 ...);
78 AddCommands(std::move(foo));
79 }
80
81 void Initialize() final;
82
83 void Execute() final;
84
85 void End(bool interrupted) final;
86
87 bool IsFinished() final;
88
89 bool RunsWhenDisabled() const override;
90
91 Command::InterruptionBehavior GetInterruptionBehavior() const override;
92
93 private:
94 void AddCommands(std::vector<std::unique_ptr<Command>>&& commands);
95
96 std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
97 bool m_runWhenDisabled{true};
98 Command::InterruptionBehavior m_interruptBehavior{
100 bool isRunning = false;
101};
102} // namespace wpi::cmd
103
104#ifdef _WIN32
105#pragma warning(pop)
106#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
void AddCommands(Commands &&... commands)
Adds the given commands to the group.
Definition ParallelCommandGroup.hpp:73
ParallelCommandGroup(ParallelCommandGroup &)=delete
ParallelCommandGroup(std::vector< std::unique_ptr< Command > > &&commands)
Creates a new ParallelCommandGroup.
ParallelCommandGroup(const ParallelCommandGroup &)=delete
bool RunsWhenDisabled() const override
Command::InterruptionBehavior GetInterruptionBehavior() const override
ParallelCommandGroup(Commands &&... commands)
Creates a new ParallelCommandGroup.
Definition ParallelCommandGroup.hpp:55
void End(bool interrupted) final
ParallelCommandGroup(ParallelCommandGroup &&other)=default
Definition StringMap.hpp:773
Definition CommandNiDsStadiaController.hpp:15