WPILibC++ 2024.3.2
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
12
13namespace frc2 {
14/**
15 * A command that allows the user to pass in functions for each of the basic
16 * command methods through the constructor. Useful for inline definitions of
17 * complex commands - note, however, that if a command is beyond a certain
18 * complexity it is usually better practice to write a proper class for it than
19 * to inline it.
20 *
21 * This class is provided by the NewCommands VendorDep
22 */
23class FunctionalCommand : public CommandHelper<Command, FunctionalCommand> {
24 public:
25 /**
26 * Creates a new FunctionalCommand.
27 *
28 * @param onInit the function to run on command initialization
29 * @param onExecute the function to run on command execution
30 * @param onEnd the function to run on command end
31 * @param isFinished the function that determines whether the command has
32 * finished
33 * @param requirements the subsystems required by this command
34 */
35 FunctionalCommand(std::function<void()> onInit,
36 std::function<void()> onExecute,
37 std::function<void(bool)> onEnd,
38 std::function<bool()> isFinished,
39 Requirements requirements = {});
40
42
43 FunctionalCommand(const FunctionalCommand& other) = default;
44
45 void Initialize() override;
46
47 void Execute() override;
48
49 void End(bool interrupted) override;
50
51 bool IsFinished() override;
52
53 private:
54 std::function<void()> m_onInit;
55 std::function<void()> m_onExecute;
56 std::function<void(bool)> m_onEnd;
57 std::function<bool()> m_isFinished;
58};
59} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
A command that allows the user to pass in functions for each of the basic command methods through the...
Definition: FunctionalCommand.h:23
void End(bool interrupted) override
FunctionalCommand(std::function< void()> onInit, std::function< void()> onExecute, std::function< void(bool)> onEnd, std::function< bool()> isFinished, Requirements requirements={})
Creates a new FunctionalCommand.
FunctionalCommand(FunctionalCommand &&other)=default
void Initialize() override
bool IsFinished() override
FunctionalCommand(const FunctionalCommand &other)=default
void Execute() override
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition: Requirements.h:20
Definition: TrapezoidProfileSubsystem.h:12