WPILibC++ 2024.3.2
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 <concepts>
13#include <memory>
14#include <utility>
15
18
19namespace frc2 {
20/**
21 * A class used internally to wrap commands while overriding a specific method;
22 * all other methods will call through to the wrapped command.
23 *
24 * <p>Wrapped commands may only be used through the wrapper, trying to directly
25 * schedule them or add them to a group will throw an exception.
26 */
27class WrapperCommand : public CommandHelper<Command, WrapperCommand> {
28 public:
29 /**
30 * Wrap a command.
31 *
32 * @param command the command being wrapped. Trying to directly schedule this
33 * command or add it to a group will throw an exception.
34 */
35 explicit WrapperCommand(std::unique_ptr<Command>&& command);
36
37 /**
38 * Wrap a command.
39 *
40 * @param command the command being wrapped. Trying to directly schedule this
41 * command or add it to a group will throw an exception.
42 */
43 template <std::derived_from<Command> T>
44 // NOLINTNEXTLINE(bugprone-forwarding-reference-overload)
45 explicit WrapperCommand(T&& command)
47 std::make_unique<std::decay_t<T>>(std::forward<T>(command))) {}
48
49 WrapperCommand(WrapperCommand&& other) = default;
50
51 // No copy constructors for command groups
52 WrapperCommand(const WrapperCommand& other) = delete;
53
54 // Prevent template expansion from emulating copy ctor
56
57 void Initialize() override;
58
59 void Execute() override;
60
61 bool IsFinished() override;
62
63 void End(bool interrupted) override;
64
65 bool RunsWhenDisabled() const override;
66
67 InterruptionBehavior GetInterruptionBehavior() const override;
68
70
71 protected:
72 /// Command being wrapped.
73 std::unique_ptr<Command> m_command;
74};
75} // namespace frc2
76
77#ifdef _WIN32
78#pragma warning(pop)
79#endif
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
A class used internally to wrap commands while overriding a specific method; all other methods will c...
Definition: WrapperCommand.h:27
WrapperCommand(T &&command)
Wrap a command.
Definition: WrapperCommand.h:45
WrapperCommand(const WrapperCommand &other)=delete
void Execute() override
WrapperCommand(std::unique_ptr< Command > &&command)
Wrap a command.
bool RunsWhenDisabled() const override
void Initialize() override
bool IsFinished() override
std::unique_ptr< Command > m_command
Command being wrapped.
Definition: WrapperCommand.h:73
void End(bool interrupted) override
WrapperCommand(WrapperCommand &&other)=default
InterruptionBehavior GetInterruptionBehavior() const override
wpi::SmallSet< Subsystem *, 4 > GetRequirements() const override
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
Definition: TrapezoidProfileSubsystem.h:12
Definition: array.h:89
typename std::decay< T >::type decay_t
Definition: expected:231