WPILibC++  2020.3.2
ParallelDeadlineGroup.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 {
32  : public CommandHelper<CommandGroupBase, ParallelDeadlineGroup> {
33  public:
44  ParallelDeadlineGroup(std::unique_ptr<Command>&& deadline,
45  std::vector<std::unique_ptr<Command>>&& commands);
56  template <class T, class... Types,
57  typename = std::enable_if_t<
58  std::is_base_of_v<Command, std::remove_reference_t<T>>>,
59  typename = std::enable_if_t<std::conjunction_v<
60  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
61  explicit ParallelDeadlineGroup(T&& deadline, Types&&... commands) {
62  SetDeadline(std::make_unique<std::remove_reference_t<T>>(
63  std::forward<T>(deadline)));
64  AddCommands(std::forward<Types>(commands)...);
65  }
66 
68 
69  // No copy constructors for command groups
71 
72  // Prevent template expansion from emulating copy ctor
74 
75  template <class... Types,
76  typename = std::enable_if_t<std::conjunction_v<
77  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
78  void AddCommands(Types&&... commands) {
79  std::vector<std::unique_ptr<Command>> foo;
80  ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
81  std::forward<Types>(commands))),
82  ...);
83  AddCommands(std::move(foo));
84  }
85 
86  void Initialize() override;
87 
88  void Execute() override;
89 
90  void End(bool interrupted) override;
91 
92  bool IsFinished() override;
93 
94  bool RunsWhenDisabled() const override;
95 
96  private:
97  void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) override;
98 
99  void SetDeadline(std::unique_ptr<Command>&& deadline);
100 
101  std::vector<std::pair<std::unique_ptr<Command>, bool>> m_commands;
102  Command* m_deadline;
103  bool m_runWhenDisabled{true};
104  bool m_finished{true};
105 };
106 } // namespace frc2
107 
108 #ifdef _WIN32
109 #pragma warning(pop)
110 #endif
frc2::ParallelDeadlineGroup::End
void End(bool interrupted) override
The action to take when the command ends.
frc2::ParallelDeadlineGroup
A CommandGroup that runs a set of commands in parallel, ending only when a specific command (the "dea...
Definition: ParallelDeadlineGroup.h:31
frc2::ParallelDeadlineGroup::Execute
void Execute() override
The main body of a command.
frc2::ParallelDeadlineGroup::ParallelDeadlineGroup
ParallelDeadlineGroup(std::unique_ptr< Command > &&deadline, std::vector< std::unique_ptr< Command >> &&commands)
Creates a new ParallelDeadlineGroup.
frc2::ParallelDeadlineGroup::IsFinished
bool IsFinished() override
Whether the command has finished.
frc2::ParallelDeadlineGroup::ParallelDeadlineGroup
ParallelDeadlineGroup(T &&deadline, Types &&... commands)
Creates a new ParallelDeadlineGroup.
Definition: ParallelDeadlineGroup.h:61
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::ParallelDeadlineGroup::Initialize
void Initialize() override
The initial subroutine of a command.
frc2::ParallelDeadlineGroup::RunsWhenDisabled
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.