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