WPILibC++  2020.3.2
SequentialCommandGroup.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 <limits>
16 #include <memory>
17 #include <type_traits>
18 #include <utility>
19 #include <vector>
20 
21 #include <frc/ErrorBase.h>
22 #include <frc/WPIErrors.h>
23 #include <wpi/ArrayRef.h>
24 
25 #include "frc2/command/CommandGroupBase.h"
26 #include "frc2/command/CommandHelper.h"
27 
28 namespace frc2 {
29 
30 const size_t invalid_index = std::numeric_limits<size_t>::max();
31 
39  : public CommandHelper<CommandGroupBase, SequentialCommandGroup> {
40  public:
48  explicit SequentialCommandGroup(
49  std::vector<std::unique_ptr<Command>>&& commands);
50 
58  template <class... Types,
59  typename = std::enable_if_t<std::conjunction_v<
60  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
61  explicit SequentialCommandGroup(Types&&... commands) {
62  AddCommands(std::forward<Types>(commands)...);
63  }
64 
66 
67  // No copy constructors for command groups
69 
70  // Prevent template expansion from emulating copy ctor
72 
73  template <class... Types,
74  typename = std::enable_if_t<std::conjunction_v<
75  std::is_base_of<Command, std::remove_reference_t<Types>>...>>>
76  void AddCommands(Types&&... commands) {
77  std::vector<std::unique_ptr<Command>> foo;
78  ((void)foo.emplace_back(std::make_unique<std::remove_reference_t<Types>>(
79  std::forward<Types>(commands))),
80  ...);
81  AddCommands(std::move(foo));
82  }
83 
84  void Initialize() override;
85 
86  void Execute() override;
87 
88  void End(bool interrupted) override;
89 
90  bool IsFinished() override;
91 
92  bool RunsWhenDisabled() const override;
93 
94  private:
95  void AddCommands(std::vector<std::unique_ptr<Command>>&& commands) final;
96 
98  size_t m_currentCommandIndex{invalid_index};
99  bool m_runWhenDisabled{true};
100 };
101 } // namespace frc2
102 
103 #ifdef _WIN32
104 #pragma warning(pop)
105 #endif
frc2::SequentialCommandGroup::Execute
void Execute() override
The main body of a command.
frc2::SequentialCommandGroup::End
void End(bool interrupted) override
The action to take when the command ends.
frc2::SequentialCommandGroup::SequentialCommandGroup
SequentialCommandGroup(Types &&... commands)
Creates a new SequentialCommandGroup.
Definition: SequentialCommandGroup.h:61
frc2::SequentialCommandGroup::Initialize
void Initialize() override
The initial subroutine of a command.
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::SequentialCommandGroup::RunsWhenDisabled
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
frc2::SequentialCommandGroup::IsFinished
bool IsFinished() override
Whether the command has finished.
frc2::SequentialCommandGroup
A CommandGroups that runs a list of commands in sequence.
Definition: SequentialCommandGroup.h:38
wpi::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:856
frc2::SequentialCommandGroup::SequentialCommandGroup
SequentialCommandGroup(std::vector< std::unique_ptr< Command >> &&commands)
Creates a new SequentialCommandGroup.