WPILibC++ 2023.4.3
ConditionalCommand.h
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#include <functional>
8#include <memory>
9#include <utility>
10
13
14namespace frc2 {
15/**
16 * A command composition that runs one of two commands, depending on the value
17 * of the given condition when this command is initialized.
18 *
19 * <p>The rules for command compositions apply: command instances that are
20 * passed to it are owned by the composition and cannot be added to any other
21 * composition or scheduled individually, and the composition requires all
22 * subsystems its components require.
23 *
24 * This class is provided by the NewCommands VendorDep
25 *
26 * @see ScheduleCommand
27 */
29 : public CommandHelper<CommandBase, ConditionalCommand> {
30 public:
31 /**
32 * Creates a new ConditionalCommand.
33 *
34 * @param onTrue the command to run if the condition is true
35 * @param onFalse the command to run if the condition is false
36 * @param condition the condition to determine which command to run
37 */
38 template <class T1, class T2,
39 typename = std::enable_if_t<
40 std::is_base_of_v<Command, std::remove_reference_t<T1>>>,
41 typename = std::enable_if_t<
42 std::is_base_of_v<Command, std::remove_reference_t<T2>>>>
43 ConditionalCommand(T1&& onTrue, T2&& onFalse, std::function<bool()> condition)
44 : ConditionalCommand(std::make_unique<std::remove_reference_t<T1>>(
45 std::forward<T1>(onTrue)),
46 std::make_unique<std::remove_reference_t<T2>>(
47 std::forward<T2>(onFalse)),
48 condition) {}
49
50 /**
51 * Creates a new ConditionalCommand.
52 *
53 * @param onTrue the command to run if the condition is true
54 * @param onFalse the command to run if the condition is false
55 * @param condition the condition to determine which command to run
56 */
57 ConditionalCommand(std::unique_ptr<Command>&& onTrue,
58 std::unique_ptr<Command>&& onFalse,
59 std::function<bool()> condition);
60
62
63 // No copy constructors for command groups
64 ConditionalCommand(const ConditionalCommand& other) = delete;
65
66 void Initialize() override;
67
68 void Execute() override;
69
70 void End(bool interrupted) override;
71
72 bool IsFinished() override;
73
74 bool RunsWhenDisabled() const override;
75
76 void InitSendable(wpi::SendableBuilder& builder) override;
77
78 private:
79 std::unique_ptr<Command> m_onTrue;
80 std::unique_ptr<Command> m_onFalse;
81 std::function<bool()> m_condition;
82 Command* m_selectedCommand{nullptr};
83 bool m_runsWhenDisabled = true;
84};
85} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:47
A command composition that runs one of two commands, depending on the value of the given condition wh...
Definition: ConditionalCommand.h:29
ConditionalCommand(std::unique_ptr< Command > &&onTrue, std::unique_ptr< Command > &&onFalse, std::function< bool()> condition)
Creates a new ConditionalCommand.
ConditionalCommand(T1 &&onTrue, T2 &&onFalse, std::function< bool()> condition)
Creates a new ConditionalCommand.
Definition: ConditionalCommand.h:43
ConditionalCommand(const ConditionalCommand &other)=delete
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
bool IsFinished() override
Whether the command has finished.
void Initialize() override
The initial subroutine of a command.
ConditionalCommand(ConditionalCommand &&other)=default
void End(bool interrupted) override
The action to take when the command ends.
void Execute() override
The main body of a command.
Definition: SendableBuilder.h:18
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:298
typename std::remove_reference< T >::type remove_reference_t
Definition: core.h:303
Definition: InstantCommand.h:14
Definition: StdDeque.h:50