WPILibC++  2020.3.2
ParallelCommandGroup.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #ifdef _WIN32
11 #pragma warning(push)
12 #pragma warning(disable : 4521)
13 #endif
14 
15 #include <memory>
16 #include <utility>
17 #include <vector>
18 
19 #include "frc2/command/CommandGroupBase.h"
20 #include "frc2/command/CommandHelper.h"
21 
22 namespace frc2 {
31  : public CommandHelper<CommandGroupBase, ParallelCommandGroup> {
32  public:
41  explicit ParallelCommandGroup(
42  std::vector<std::unique_ptr<Command>>&& commands);
43 
52  template <class... Types,
53  typename = std::enable_if_t<std::conjunction_v<
54  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
55  explicit ParallelCommandGroup(Types&&... commands) {
56  AddCommands(std::forward<Types>(commands)...);
57  }
58 
59  ParallelCommandGroup(ParallelCommandGroup&& other) = default;
60 
61  // No copy constructors for commandgroups
63 
64  // Prevent template expansion from emulating copy ctor
66 
67  template <class... Types,
68  typename = std::enable_if_t<std::conjunction_v<
69  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
70  void AddCommands(Types&&... commands) {
71  std::vector<std::unique_ptr<Command>> foo;
72  ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
73  std::forward<Types>(commands))),
74  ...);
75  AddCommands(std::move(foo));
76  }
77 
78  void Initialize() override;
79 
80  void Execute() override;
81 
82  void End(bool interrupted) override;
83 
84  bool IsFinished() override;
85 
86  bool RunsWhenDisabled() const override;
87 
88  private:
89  void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) override;
90 
91  std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
92  bool m_runWhenDisabled{true};
93  bool isRunning = false;
94 };
95 } // namespace frc2
96 
97 #ifdef _WIN32
98 #pragma warning(pop)
99 #endif
frc2::ParallelCommandGroup::Initialize
void Initialize() override
The initial subroutine of a command.
frc2::ParallelCommandGroup::IsFinished
bool IsFinished() override
Whether the command has finished.
frc2::ParallelCommandGroup::Execute
void Execute() override
The main body of a command.
frc2::ParallelCommandGroup::ParallelCommandGroup
ParallelCommandGroup(std::vector< std::unique_ptr< Command >> &&commands)
Creates a new ParallelCommandGroup.
frc2::ParallelCommandGroup::ParallelCommandGroup
ParallelCommandGroup(Types &&... commands)
Creates a new ParallelCommandGroup.
Definition: ParallelCommandGroup.h:55
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::ParallelCommandGroup
A CommandGroup that runs a set of commands in parallel, ending when the last command ends.
Definition: ParallelCommandGroup.h:30
frc2::ParallelCommandGroup::RunsWhenDisabled
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
frc2::ParallelCommandGroup::End
void End(bool interrupted) override
The action to take when the command ends.