WPILibC++ 2023.4.3
RepeatCommand.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#ifdef _WIN32
8#pragma warning(push)
9#pragma warning(disable : 4521)
10#endif
11
12#include <memory>
13#include <utility>
14
17
18namespace frc2 {
19/**
20 * A command that runs another command repeatedly, restarting it when it ends,
21 * until this command is interrupted. Command instances that are passed to it
22 * cannot be added to any other groups, or scheduled individually.
23 *
24 * <p>The rules for command compositions apply: command instances that are
25 * passed to it are owned by the composition and cannot be added to any other
26 * composition or scheduled individually, and the composition requires all
27 * subsystems its components require.
28 *
29 * <p>This class is provided by the NewCommands VendorDep
30 */
31class RepeatCommand : public CommandHelper<CommandBase, RepeatCommand> {
32 public:
33 /**
34 * Creates a new RepeatCommand. Will run another command repeatedly,
35 * restarting it whenever it ends, until this command is interrupted.
36 *
37 * @param command the command to run repeatedly
38 */
39 explicit RepeatCommand(std::unique_ptr<Command>&& command);
40
41 /**
42 * Creates a new RepeatCommand. Will run another command repeatedly,
43 * restarting it whenever it ends, until this command is interrupted.
44 *
45 * @param command the command to run repeatedly
46 */
47 template <class T, typename = std::enable_if_t<std::is_base_of_v<
48 Command, std::remove_reference_t<T>>>>
49 explicit RepeatCommand(T&& command)
50 : RepeatCommand(std::make_unique<std::remove_reference_t<T>>(
51 std::forward<T>(command))) {}
52
53 RepeatCommand(RepeatCommand&& other) = default;
54
55 // No copy constructors for command groups
56 RepeatCommand(const RepeatCommand& other) = delete;
57
58 // Prevent template expansion from emulating copy ctor
60
61 void Initialize() override;
62
63 void Execute() override;
64
65 bool IsFinished() override;
66
67 void End(bool interrupted) override;
68
69 bool RunsWhenDisabled() const override;
70
72
73 void InitSendable(wpi::SendableBuilder& builder) override;
74
75 private:
76 std::unique_ptr<Command> m_command;
77 bool m_ended;
78};
79} // namespace frc2
80
81#ifdef _WIN32
82#pragma warning(pop)
83#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
Command()=default
InterruptionBehavior
An enum describing the command's behavior when another command with a shared requirement is scheduled...
Definition: Command.h:104
A command that runs another command repeatedly, restarting it when it ends, until this command is int...
Definition: RepeatCommand.h:31
RepeatCommand(RepeatCommand &&other)=default
void End(bool interrupted) override
The action to take when the command ends.
void InitSendable(wpi::SendableBuilder &builder) override
Initializes this Sendable object.
void Execute() override
The main body of a command.
bool RunsWhenDisabled() const override
Whether the given command should run when the robot is disabled.
void Initialize() override
The initial subroutine of a command.
Command::InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
RepeatCommand(const RepeatCommand &other)=delete
RepeatCommand(RepeatCommand &)=delete
RepeatCommand(std::unique_ptr< Command > &&command)
Creates a new RepeatCommand.
RepeatCommand(T &&command)
Creates a new RepeatCommand.
Definition: RepeatCommand.h:49
bool IsFinished() override
Whether the command has finished.
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