WPILibC++  2020.3.2
ConditionalCommand.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 #include <functional>
11 #include <memory>
12 #include <utility>
13 
14 #include "frc2/command/CommandBase.h"
15 #include "frc2/command/CommandGroupBase.h"
16 #include "frc2/command/CommandHelper.h"
17 
18 namespace frc2 {
38  : public CommandHelper<CommandBase, ConditionalCommand> {
39  public:
47  template <class T1, class T2,
48  typename = std::enable_if_t<
49  std::is_base_of_v<Command, std::remove_reference_t<T1>>>,
50  typename = std::enable_if_t<
51  std::is_base_of_v<Command, std::remove_reference_t<T2>>>>
52  ConditionalCommand(T1&& onTrue, T2&& onFalse, std::function<bool()> condition)
53  : ConditionalCommand(std::make_unique<std::remove_reference_t<T1>>(
54  std::forward<T1>(onTrue)),
55  std::make_unique<std::remove_reference_t<T2>>(
56  std::forward<T2>(onFalse)),
57  condition) {}
58 
66  ConditionalCommand(std::unique_ptr<Command>&& onTrue,
67  std::unique_ptr<Command>&& onFalse,
68  std::function<bool()> condition);
69 
70  ConditionalCommand(ConditionalCommand&& other) = default;
71 
72  // No copy constructors for command groups
73  ConditionalCommand(const ConditionalCommand& other) = delete;
74 
75  void Initialize() override;
76 
77  void Execute() override;
78 
79  void End(bool interrupted) override;
80 
81  bool IsFinished() override;
82 
83  bool RunsWhenDisabled() const override;
84 
85  private:
86  std::unique_ptr<Command> m_onTrue;
87  std::unique_ptr<Command> m_onFalse;
88  std::function<bool()> m_condition;
89  Command* m_selectedCommand{nullptr};
90  bool m_runsWhenDisabled = true;
91 };
92 } // namespace frc2
frc2::ConditionalCommand::ConditionalCommand
ConditionalCommand(T1 &&onTrue, T2 &&onFalse, std::function< bool()> condition)
Creates a new ConditionalCommand.
Definition: ConditionalCommand.h:52
frc2::ConditionalCommand::RunsWhenDisabled
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
frc2::ConditionalCommand::IsFinished
bool IsFinished() override
Whether the command has finished.
frc2::ConditionalCommand::End
void End(bool interrupted) override
The action to take when the command ends.
frc2::Command
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::ConditionalCommand::Execute
void Execute() override
The main body of a command.
frc2::ConditionalCommand
Runs one of two commands, depending on the value of the given condition when this command is initiali...
Definition: ConditionalCommand.h:37
frc2::ConditionalCommand::Initialize
void Initialize() override
The initial subroutine of a command.