WPILibC++ 2023.4.3
WrapperCommand.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 class used internally to wrap commands while overriding a specific method;
21 * all other methods will call through to the wrapped command.
22 *
23 * <p>Wrapped commands may only be used through the wrapper, trying to directly
24 * schedule them or add them to a group will throw an exception.
25 */
26class WrapperCommand : public CommandHelper<CommandBase, WrapperCommand> {
27 public:
28 /**
29 * Wrap a command.
30 *
31 * @param command the command being wrapped. Trying to directly schedule this
32 * command or add it to a group will throw an exception.
33 */
34 explicit WrapperCommand(std::unique_ptr<Command>&& command);
35
36 /**
37 * Wrap a command.
38 *
39 * @param command the command being wrapped. Trying to directly schedule this
40 * command or add it to a group will throw an exception.
41 */
42 template <class T, typename = std::enable_if_t<std::is_base_of_v<
43 Command, std::remove_reference_t<T>>>>
44 explicit WrapperCommand(T&& command)
45 : WrapperCommand(std::make_unique<std::remove_reference_t<T>>(
46 std::forward<T>(command))) {}
47
48 WrapperCommand(WrapperCommand&& other) = default;
49
50 // No copy constructors for command groups
51 WrapperCommand(const WrapperCommand& other) = delete;
52
53 // Prevent template expansion from emulating copy ctor
55
56 void Initialize() override;
57
58 void Execute() override;
59
60 bool IsFinished() override;
61
62 void End(bool interrupted) override;
63
64 bool RunsWhenDisabled() const override;
65
67
69
70 protected:
71 std::unique_ptr<Command> m_command;
72};
73} // namespace frc2
74
75#ifdef _WIN32
76#pragma warning(pop)
77#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 class used internally to wrap commands while overriding a specific method; all other methods will c...
Definition: WrapperCommand.h:26
WrapperCommand(const WrapperCommand &other)=delete
WrapperCommand(T &&command)
Wrap a command.
Definition: WrapperCommand.h:44
void Execute() override
The main body of a command.
WrapperCommand(std::unique_ptr< Command > &&command)
Wrap 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.
bool IsFinished() override
Whether the command has finished.
std::unique_ptr< Command > m_command
Definition: WrapperCommand.h:71
void End(bool interrupted) override
The action to take when the command ends.
WrapperCommand(WrapperCommand &&other)=default
InterruptionBehavior GetInterruptionBehavior() const override
How the command behaves when another command with a shared requirement is scheduled.
wpi::SmallSet< Subsystem *, 4 > GetRequirements() const override
Gets the Subsystem requirements of the command.
WrapperCommand(WrapperCommand &)=delete
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
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