2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <functional>
|
2019-08-25 23:55:59 -04:00
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#include "frc2/command/CommandBase.h"
|
|
|
|
|
#include "frc2/command/CommandHelper.h"
|
2019-08-25 23:55:59 -04:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
2022-12-07 07:13:31 +02:00
|
|
|
* A command composition that runs one of two commands, depending on the value
|
|
|
|
|
* of the given condition when this command is initialized.
|
2019-08-25 23:55:59 -04:00
|
|
|
*
|
2022-12-07 07:13:31 +02:00
|
|
|
* <p>The rules for command compositions apply: command instances that are
|
|
|
|
|
* passed to it are owned by the composition and cannot be added to any other
|
|
|
|
|
* composition or scheduled individually, and the composition requires all
|
|
|
|
|
* subsystems its components require.
|
2019-08-25 23:55:59 -04:00
|
|
|
*
|
2022-01-08 11:11:34 -08:00
|
|
|
* This class is provided by the NewCommands VendorDep
|
|
|
|
|
*
|
2019-08-25 23:55:59 -04:00
|
|
|
* @see ScheduleCommand
|
|
|
|
|
*/
|
|
|
|
|
class ConditionalCommand
|
|
|
|
|
: public CommandHelper<CommandBase, ConditionalCommand> {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new ConditionalCommand.
|
|
|
|
|
*
|
|
|
|
|
* @param onTrue the command to run if the condition is true
|
|
|
|
|
* @param onFalse the command to run if the condition is false
|
|
|
|
|
* @param condition the condition to determine which command to run
|
|
|
|
|
*/
|
|
|
|
|
template <class T1, class T2,
|
|
|
|
|
typename = std::enable_if_t<
|
2019-09-13 20:14:37 -07:00
|
|
|
std::is_base_of_v<Command, std::remove_reference_t<T1>>>,
|
2019-08-25 23:55:59 -04:00
|
|
|
typename = std::enable_if_t<
|
2019-09-13 20:14:37 -07:00
|
|
|
std::is_base_of_v<Command, std::remove_reference_t<T2>>>>
|
2019-08-25 23:55:59 -04:00
|
|
|
ConditionalCommand(T1&& onTrue, T2&& onFalse, std::function<bool()> condition)
|
|
|
|
|
: ConditionalCommand(std::make_unique<std::remove_reference_t<T1>>(
|
|
|
|
|
std::forward<T1>(onTrue)),
|
|
|
|
|
std::make_unique<std::remove_reference_t<T2>>(
|
|
|
|
|
std::forward<T2>(onFalse)),
|
|
|
|
|
condition) {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new ConditionalCommand.
|
|
|
|
|
*
|
|
|
|
|
* @param onTrue the command to run if the condition is true
|
|
|
|
|
* @param onFalse the command to run if the condition is false
|
|
|
|
|
* @param condition the condition to determine which command to run
|
|
|
|
|
*/
|
|
|
|
|
ConditionalCommand(std::unique_ptr<Command>&& onTrue,
|
|
|
|
|
std::unique_ptr<Command>&& onFalse,
|
|
|
|
|
std::function<bool()> condition);
|
|
|
|
|
|
|
|
|
|
ConditionalCommand(ConditionalCommand&& other) = default;
|
|
|
|
|
|
|
|
|
|
// No copy constructors for command groups
|
|
|
|
|
ConditionalCommand(const ConditionalCommand& other) = delete;
|
|
|
|
|
|
|
|
|
|
void Initialize() override;
|
|
|
|
|
|
|
|
|
|
void Execute() override;
|
|
|
|
|
|
|
|
|
|
void End(bool interrupted) override;
|
|
|
|
|
|
|
|
|
|
bool IsFinished() override;
|
|
|
|
|
|
|
|
|
|
bool RunsWhenDisabled() const override;
|
|
|
|
|
|
2022-12-16 04:28:52 +02:00
|
|
|
void InitSendable(wpi::SendableBuilder& builder) override;
|
|
|
|
|
|
2019-08-25 23:55:59 -04:00
|
|
|
private:
|
|
|
|
|
std::unique_ptr<Command> m_onTrue;
|
|
|
|
|
std::unique_ptr<Command> m_onFalse;
|
|
|
|
|
std::function<bool()> m_condition;
|
|
|
|
|
Command* m_selectedCommand{nullptr};
|
|
|
|
|
bool m_runsWhenDisabled = true;
|
|
|
|
|
};
|
|
|
|
|
} // namespace frc2
|