diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp index e26717914b..0abb5d820c 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/Command.cpp @@ -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 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 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 toRun, std::span 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 toRun, @@ -80,8 +76,7 @@ CommandPtr Command::AndThen(std::function toRun, CommandPtr Command::AndThen(std::function toRun, std::span 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 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 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 handler) && { - return CommandPtr(std::move(*this).TransferOwnership()) - .HandleInterrupt(std::move(handler)); + return std::move(*this).ToPtr().HandleInterrupt(std::move(handler)); } void Command::Schedule() { diff --git a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp index b3f8f2f12e..8777e08023 100644 --- a/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp +++ b/wpilibNewCommands/src/main/native/cpp/frc2/command/CommandPtr.cpp @@ -219,12 +219,12 @@ CommandPtr CommandPtr::HandleInterrupt(std::function handler) && { }); } -Command* CommandPtr::get() const { +CommandBase* CommandPtr::get() const { AssertValid(); return m_ptr.get(); } -std::unique_ptr CommandPtr::Unwrap() && { +std::unique_ptr CommandPtr::Unwrap() && { AssertValid(); return std::move(m_ptr); } diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h index 6609712880..dcfc1c817b 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/Command.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/Command.h @@ -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 diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/CommandHelper.h b/wpilibNewCommands/src/main/native/include/frc2/command/CommandHelper.h index 0ff55add80..76ee6cd262 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/CommandHelper.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/CommandHelper.h @@ -29,7 +29,7 @@ class CommandHelper : public Base { public: CommandHelper() = default; - CommandPtr ToPtr() && { + CommandPtr ToPtr() && override { return CommandPtr( std::make_unique(std::move(*static_cast(this)))); } diff --git a/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h b/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h index d849a4a284..d67ce777c3 100644 --- a/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h +++ b/wpilibNewCommands/src/main/native/include/frc2/command/CommandPtr.h @@ -11,7 +11,7 @@ #include #include -#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) + explicit CommandPtr(std::unique_ptr&& command) : m_ptr(std::move(command)) {} template Unwrap() &&; + std::unique_ptr Unwrap() &&; /** * Schedules this command. @@ -271,7 +271,7 @@ class CommandPtr final { std::vector&& vec); private: - std::unique_ptr m_ptr; + std::unique_ptr m_ptr; void AssertValid() const; };