Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2017. 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 <string> |
| 11 | |
| 12 | #include "Commands/Command.h" |
| 13 | #include "Commands/InstantCommand.h" |
| 14 | |
| 15 | namespace frc { |
| 16 | |
| 17 | /** |
| 18 | * A {@link ConditionalCommand} is a {@link Command} that starts one of two |
| 19 | * commands. |
| 20 | * |
| 21 | * <p> |
| 22 | * A {@link ConditionalCommand} uses m_condition to determine whether it should |
| 23 | * run m_onTrue or m_onFalse. |
| 24 | * </p> |
| 25 | * |
| 26 | * <p> |
| 27 | * A {@link ConditionalCommand} adds the proper {@link Command} to the {@link |
| 28 | * Scheduler} during {@link ConditionalCommand#initialize()} and then {@link |
| 29 | * ConditionalCommand#isFinished()} will return true once that {@link Command} |
| 30 | * has finished executing. |
| 31 | * </p> |
| 32 | * |
| 33 | * <p> |
| 34 | * If no {@link Command} is specified for m_onFalse, the occurrence of that |
| 35 | * condition will be a no-op. |
| 36 | * </p> |
| 37 | * |
| 38 | * @see Command |
| 39 | * @see Scheduler |
| 40 | */ |
| 41 | class ConditionalCommand : public Command { |
| 42 | public: |
| 43 | explicit ConditionalCommand(Command* onTrue, |
| 44 | Command* onFalse = new InstantCommand()); |
| 45 | ConditionalCommand(const std::string& name, Command* onTrue, |
| 46 | Command* onFalse = new InstantCommand()); |
| 47 | virtual ~ConditionalCommand() = default; |
| 48 | |
| 49 | protected: |
| 50 | /** |
| 51 | * The Condition to test to determine which Command to run. |
| 52 | * |
| 53 | * @return true if m_onTrue should be run or false if m_onFalse should be run. |
| 54 | */ |
| 55 | virtual bool Condition() = 0; |
| 56 | |
| 57 | void _Initialize() override; |
| 58 | void _Cancel() override; |
| 59 | bool IsFinished() override; |
| 60 | void Interrupted() override; |
| 61 | |
| 62 | private: |
| 63 | /** |
| 64 | * The Command to execute if {@link ConditionalCommand#Condition()} returns |
| 65 | * true |
| 66 | */ |
| 67 | Command* m_onTrue; |
| 68 | |
| 69 | /** |
| 70 | * The Command to execute if {@link ConditionalCommand#Condition()} returns |
| 71 | * false |
| 72 | */ |
| 73 | Command* m_onFalse; |
| 74 | |
| 75 | /** |
| 76 | * Stores command chosen by condition |
| 77 | */ |
| 78 | Command* m_chosenCommand = nullptr; |
| 79 | }; |
| 80 | |
| 81 | } // namespace frc |