WPILibC++ 2023.4.3
CommandHelper.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 <memory>
8#include <type_traits>
9#include <utility>
10
13
14namespace frc2 {
15
16/**
17 * CRTP implementation to allow polymorphic decorator functions in Command.
18 *
19 * <p>Note: ALWAYS create a subclass by extending CommandHelper<Base, Subclass>,
20 * or decorators will not function!
21 *
22 * This class is provided by the NewCommands VendorDep
23 */
24template <typename Base, typename CRTP,
25 typename = std::enable_if_t<std::is_base_of_v<Command, Base>>>
26class CommandHelper : public Base {
27 using Base::Base;
28
29 public:
30 CommandHelper() = default;
31
32 CommandPtr ToPtr() && override {
33 return CommandPtr(
34 std::make_unique<CRTP>(std::move(*static_cast<CRTP*>(this))));
35 }
36
37 protected:
38 std::unique_ptr<Command> TransferOwnership() && override {
39 return std::make_unique<CRTP>(std::move(*static_cast<CRTP*>(this)));
40 }
41};
42} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
std::unique_ptr< Command > TransferOwnership() &&override
Definition: CommandHelper.h:38
CommandHelper()=default
CommandPtr ToPtr() &&override
Definition: CommandHelper.h:32
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition: CommandPtr.h:28
Definition: InstantCommand.h:14