WPILibC++ 2024.1.1-beta-4
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 */
29class ConditionalCommand : public CommandHelper<Command, 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 <std::derived_from<Command> Command1,
39 std::derived_from<Command> Command2>
40 ConditionalCommand(Command1&& onTrue, Command2&& onFalse,
41 std::function<bool()> condition)
42 : ConditionalCommand(std::make_unique<std::decay_t<Command1>>(
43 std::forward<Command1>(onTrue)),
44 std::make_unique<std::decay_t<Command2>>(
45 std::forward<Command2>(onFalse)),
46 condition) {}
47
48 /**
49 * Creates a new ConditionalCommand.
50 *
51 * @param onTrue the command to run if the condition is true
52 * @param onFalse the command to run if the condition is false
53 * @param condition the condition to determine which command to run
54 */
55 ConditionalCommand(std::unique_ptr<Command>&& onTrue,
56 std::unique_ptr<Command>&& onFalse,
57 std::function<bool()> condition);
58
60
61 // No copy constructors for command groups
62 ConditionalCommand(const ConditionalCommand& other) = delete;
63
64 void Initialize() override;
65
66 void Execute() override;
67
68 void End(bool interrupted) override;
69
70 bool IsFinished() override;
71
72 bool RunsWhenDisabled() const override;
73
74 InterruptionBehavior GetInterruptionBehavior() 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:25
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:41
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(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:40
ConditionalCommand(ConditionalCommand &&other)=default
void End(bool interrupted) override
InterruptionBehavior GetInterruptionBehavior() const override
void Execute() override
Definition: SendableBuilder.h:18
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89