WPILibC++ 2023.4.3
FunctionalCommand.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 <functional>
8#include <initializer_list>
9#include <span>
10
13
14namespace frc2 {
15/**
16 * A command that allows the user to pass in functions for each of the basic
17 * command methods through the constructor. Useful for inline definitions of
18 * complex commands - note, however, that if a command is beyond a certain
19 * complexity it is usually better practice to write a proper class for it than
20 * to inline it.
21 *
22 * This class is provided by the NewCommands VendorDep
23 */
24class FunctionalCommand : public CommandHelper<CommandBase, FunctionalCommand> {
25 public:
26 /**
27 * Creates a new FunctionalCommand.
28 *
29 * @param onInit the function to run on command initialization
30 * @param onExecute the function to run on command execution
31 * @param onEnd the function to run on command end
32 * @param isFinished the function that determines whether the command has
33 * finished
34 * @param requirements the subsystems required by this command
35 */
36 FunctionalCommand(std::function<void()> onInit,
37 std::function<void()> onExecute,
38 std::function<void(bool)> onEnd,
39 std::function<bool()> isFinished,
40 std::initializer_list<Subsystem*> requirements);
41
42 /**
43 * Creates a new FunctionalCommand.
44 *
45 * @param onInit the function to run on command initialization
46 * @param onExecute the function to run on command execution
47 * @param onEnd the function to run on command end
48 * @param isFinished the function that determines whether the command has
49 * finished
50 * @param requirements the subsystems required by this command
51 */
52 FunctionalCommand(std::function<void()> onInit,
53 std::function<void()> onExecute,
54 std::function<void(bool)> onEnd,
55 std::function<bool()> isFinished,
56 std::span<Subsystem* const> requirements = {});
57
59
60 FunctionalCommand(const FunctionalCommand& other) = default;
61
62 void Initialize() override;
63
64 void Execute() override;
65
66 void End(bool interrupted) override;
67
68 bool IsFinished() override;
69
70 private:
71 std::function<void()> m_onInit;
72 std::function<void()> m_onExecute;
73 std::function<void(bool)> m_onEnd;
74 std::function<bool()> m_isFinished;
75};
76} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A command that allows the user to pass in functions for each of the basic command methods through the...
Definition: FunctionalCommand.h:24
void End(bool interrupted) override
The action to take when the command ends.
FunctionalCommand(std::function< void()> onInit, std::function< void()> onExecute, std::function< void(bool)> onEnd, std::function< bool()> isFinished, std::initializer_list< Subsystem * > requirements)
Creates a new FunctionalCommand.
FunctionalCommand(FunctionalCommand &&other)=default
void Initialize() override
The initial subroutine of a command.
FunctionalCommand(std::function< void()> onInit, std::function< void()> onExecute, std::function< void(bool)> onEnd, std::function< bool()> isFinished, std::span< Subsystem *const > requirements={})
Creates a new FunctionalCommand.
bool IsFinished() override
Whether the command has finished.
FunctionalCommand(const FunctionalCommand &other)=default
void Execute() override
The main body of a command.
Definition: InstantCommand.h:14