mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[commands] Change C++ CommandPtr to use CommandBase (#4677)
This commit is contained in:
@@ -35,28 +35,24 @@ void Command::Execute() {}
|
||||
void Command::End(bool interrupted) {}
|
||||
|
||||
CommandPtr Command::WithTimeout(units::second_t duration) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership()).WithTimeout(duration);
|
||||
return std::move(*this).ToPtr().WithTimeout(duration);
|
||||
}
|
||||
|
||||
CommandPtr Command::Until(std::function<bool()> condition) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.Until(std::move(condition));
|
||||
return std::move(*this).ToPtr().Until(std::move(condition));
|
||||
}
|
||||
|
||||
CommandPtr Command::IgnoringDisable(bool doesRunWhenDisabled) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.IgnoringDisable(doesRunWhenDisabled);
|
||||
return std::move(*this).ToPtr().IgnoringDisable(doesRunWhenDisabled);
|
||||
}
|
||||
|
||||
CommandPtr Command::WithInterruptBehavior(
|
||||
InterruptionBehavior interruptBehavior) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.WithInterruptBehavior(interruptBehavior);
|
||||
return std::move(*this).ToPtr().WithInterruptBehavior(interruptBehavior);
|
||||
}
|
||||
|
||||
CommandPtr Command::WithInterrupt(std::function<bool()> condition) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.Until(std::move(condition));
|
||||
return std::move(*this).ToPtr().Until(std::move(condition));
|
||||
}
|
||||
|
||||
CommandPtr Command::BeforeStarting(
|
||||
@@ -68,8 +64,8 @@ CommandPtr Command::BeforeStarting(
|
||||
|
||||
CommandPtr Command::BeforeStarting(
|
||||
std::function<void()> toRun, std::span<Subsystem* const> requirements) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.BeforeStarting(std::move(toRun), requirements);
|
||||
return std::move(*this).ToPtr().BeforeStarting(std::move(toRun),
|
||||
requirements);
|
||||
}
|
||||
|
||||
CommandPtr Command::AndThen(std::function<void()> toRun,
|
||||
@@ -80,8 +76,7 @@ CommandPtr Command::AndThen(std::function<void()> toRun,
|
||||
|
||||
CommandPtr Command::AndThen(std::function<void()> toRun,
|
||||
std::span<Subsystem* const> requirements) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.AndThen(std::move(toRun), requirements);
|
||||
return std::move(*this).ToPtr().AndThen(std::move(toRun), requirements);
|
||||
}
|
||||
|
||||
PerpetualCommand Command::Perpetually() && {
|
||||
@@ -91,26 +86,23 @@ PerpetualCommand Command::Perpetually() && {
|
||||
}
|
||||
|
||||
CommandPtr Command::Repeatedly() && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership()).Repeatedly();
|
||||
return std::move(*this).ToPtr().Repeatedly();
|
||||
}
|
||||
|
||||
CommandPtr Command::AsProxy() && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership()).AsProxy();
|
||||
return std::move(*this).ToPtr().AsProxy();
|
||||
}
|
||||
|
||||
CommandPtr Command::Unless(std::function<bool()> condition) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.Unless(std::move(condition));
|
||||
return std::move(*this).ToPtr().Unless(std::move(condition));
|
||||
}
|
||||
|
||||
CommandPtr Command::FinallyDo(std::function<void(bool)> end) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.FinallyDo(std::move(end));
|
||||
return std::move(*this).ToPtr().FinallyDo(std::move(end));
|
||||
}
|
||||
|
||||
CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
|
||||
return CommandPtr(std::move(*this).TransferOwnership())
|
||||
.HandleInterrupt(std::move(handler));
|
||||
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
|
||||
}
|
||||
|
||||
void Command::Schedule() {
|
||||
|
||||
@@ -219,12 +219,12 @@ CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
|
||||
});
|
||||
}
|
||||
|
||||
Command* CommandPtr::get() const {
|
||||
CommandBase* CommandPtr::get() const {
|
||||
AssertValid();
|
||||
return m_ptr.get();
|
||||
}
|
||||
|
||||
std::unique_ptr<Command> CommandPtr::Unwrap() && {
|
||||
std::unique_ptr<CommandBase> CommandPtr::Unwrap() && {
|
||||
AssertValid();
|
||||
return std::move(m_ptr);
|
||||
}
|
||||
|
||||
@@ -345,6 +345,12 @@ safe) semantics.
|
||||
|
||||
virtual std::string GetName() const;
|
||||
|
||||
/**
|
||||
* Transfers ownership of this command to a unique pointer. Used for
|
||||
* decorator methods.
|
||||
*/
|
||||
virtual CommandPtr ToPtr() && = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Transfers ownership of this command to a unique pointer. Used for
|
||||
|
||||
@@ -29,7 +29,7 @@ class CommandHelper : public Base {
|
||||
public:
|
||||
CommandHelper() = default;
|
||||
|
||||
CommandPtr ToPtr() && {
|
||||
CommandPtr ToPtr() && override {
|
||||
return CommandPtr(
|
||||
std::make_unique<CRTP>(std::move(*static_cast<CRTP*>(this))));
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "frc2/command/Command.h"
|
||||
#include "frc2/command/CommandBase.h"
|
||||
|
||||
namespace frc2 {
|
||||
/**
|
||||
@@ -26,7 +26,7 @@ namespace frc2 {
|
||||
*/
|
||||
class CommandPtr final {
|
||||
public:
|
||||
explicit CommandPtr(std::unique_ptr<Command>&& command)
|
||||
explicit CommandPtr(std::unique_ptr<CommandBase>&& command)
|
||||
: m_ptr(std::move(command)) {}
|
||||
|
||||
template <class T, typename = std::enable_if_t<std::is_base_of_v<
|
||||
@@ -222,12 +222,12 @@ class CommandPtr final {
|
||||
/**
|
||||
* Get a raw pointer to the held command.
|
||||
*/
|
||||
Command* get() const;
|
||||
CommandBase* get() const;
|
||||
|
||||
/**
|
||||
* Convert to the underlying unique_ptr.
|
||||
*/
|
||||
std::unique_ptr<Command> Unwrap() &&;
|
||||
std::unique_ptr<CommandBase> Unwrap() &&;
|
||||
|
||||
/**
|
||||
* Schedules this command.
|
||||
@@ -271,7 +271,7 @@ class CommandPtr final {
|
||||
std::vector<CommandPtr>&& vec);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Command> m_ptr;
|
||||
std::unique_ptr<CommandBase> m_ptr;
|
||||
void AssertValid() const;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user