Skip to content

Commit cc9f140

Browse files
authored
[commands] Change C++ CommandPtr to use CommandBase (#4677)
1 parent d6f2bfb commit cc9f140

File tree

5 files changed

+27
-29
lines changed

5 files changed

+27
-29
lines changed

cpp/frc2/command/Command.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,24 @@ void Command::Execute() {}
3535
void Command::End(bool interrupted) {}
3636

3737
CommandPtr Command::WithTimeout(units::second_t duration) && {
38-
return CommandPtr(std::move(*this).TransferOwnership()).WithTimeout(duration);
38+
return std::move(*this).ToPtr().WithTimeout(duration);
3939
}
4040

4141
CommandPtr Command::Until(std::function<bool()> condition) && {
42-
return CommandPtr(std::move(*this).TransferOwnership())
43-
.Until(std::move(condition));
42+
return std::move(*this).ToPtr().Until(std::move(condition));
4443
}
4544

4645
CommandPtr Command::IgnoringDisable(bool doesRunWhenDisabled) && {
47-
return CommandPtr(std::move(*this).TransferOwnership())
48-
.IgnoringDisable(doesRunWhenDisabled);
46+
return std::move(*this).ToPtr().IgnoringDisable(doesRunWhenDisabled);
4947
}
5048

5149
CommandPtr Command::WithInterruptBehavior(
5250
InterruptionBehavior interruptBehavior) && {
53-
return CommandPtr(std::move(*this).TransferOwnership())
54-
.WithInterruptBehavior(interruptBehavior);
51+
return std::move(*this).ToPtr().WithInterruptBehavior(interruptBehavior);
5552
}
5653

5754
CommandPtr Command::WithInterrupt(std::function<bool()> condition) && {
58-
return CommandPtr(std::move(*this).TransferOwnership())
59-
.Until(std::move(condition));
55+
return std::move(*this).ToPtr().Until(std::move(condition));
6056
}
6157

6258
CommandPtr Command::BeforeStarting(
@@ -68,8 +64,8 @@ CommandPtr Command::BeforeStarting(
6864

6965
CommandPtr Command::BeforeStarting(
7066
std::function<void()> toRun, std::span<Subsystem* const> requirements) && {
71-
return CommandPtr(std::move(*this).TransferOwnership())
72-
.BeforeStarting(std::move(toRun), requirements);
67+
return std::move(*this).ToPtr().BeforeStarting(std::move(toRun),
68+
requirements);
7369
}
7470

7571
CommandPtr Command::AndThen(std::function<void()> toRun,
@@ -80,8 +76,7 @@ CommandPtr Command::AndThen(std::function<void()> toRun,
8076

8177
CommandPtr Command::AndThen(std::function<void()> toRun,
8278
std::span<Subsystem* const> requirements) && {
83-
return CommandPtr(std::move(*this).TransferOwnership())
84-
.AndThen(std::move(toRun), requirements);
79+
return std::move(*this).ToPtr().AndThen(std::move(toRun), requirements);
8580
}
8681

8782
PerpetualCommand Command::Perpetually() && {
@@ -91,26 +86,23 @@ PerpetualCommand Command::Perpetually() && {
9186
}
9287

9388
CommandPtr Command::Repeatedly() && {
94-
return CommandPtr(std::move(*this).TransferOwnership()).Repeatedly();
89+
return std::move(*this).ToPtr().Repeatedly();
9590
}
9691

9792
CommandPtr Command::AsProxy() && {
98-
return CommandPtr(std::move(*this).TransferOwnership()).AsProxy();
93+
return std::move(*this).ToPtr().AsProxy();
9994
}
10095

10196
CommandPtr Command::Unless(std::function<bool()> condition) && {
102-
return CommandPtr(std::move(*this).TransferOwnership())
103-
.Unless(std::move(condition));
97+
return std::move(*this).ToPtr().Unless(std::move(condition));
10498
}
10599

106100
CommandPtr Command::FinallyDo(std::function<void(bool)> end) && {
107-
return CommandPtr(std::move(*this).TransferOwnership())
108-
.FinallyDo(std::move(end));
101+
return std::move(*this).ToPtr().FinallyDo(std::move(end));
109102
}
110103

111104
CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
112-
return CommandPtr(std::move(*this).TransferOwnership())
113-
.HandleInterrupt(std::move(handler));
105+
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
114106
}
115107

116108
void Command::Schedule() {

cpp/frc2/command/CommandPtr.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,12 @@ CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
219219
});
220220
}
221221

222-
Command* CommandPtr::get() const {
222+
CommandBase* CommandPtr::get() const {
223223
AssertValid();
224224
return m_ptr.get();
225225
}
226226

227-
std::unique_ptr<Command> CommandPtr::Unwrap() && {
227+
std::unique_ptr<CommandBase> CommandPtr::Unwrap() && {
228228
AssertValid();
229229
return std::move(m_ptr);
230230
}

include/frc2/command/Command.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ safe) semantics.
345345

346346
virtual std::string GetName() const;
347347

348+
/**
349+
* Transfers ownership of this command to a unique pointer. Used for
350+
* decorator methods.
351+
*/
352+
virtual CommandPtr ToPtr() && = 0;
353+
348354
protected:
349355
/**
350356
* Transfers ownership of this command to a unique pointer. Used for

include/frc2/command/CommandHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CommandHelper : public Base {
2929
public:
3030
CommandHelper() = default;
3131

32-
CommandPtr ToPtr() && {
32+
CommandPtr ToPtr() && override {
3333
return CommandPtr(
3434
std::make_unique<CRTP>(std::move(*static_cast<CRTP*>(this))));
3535
}

include/frc2/command/CommandPtr.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <utility>
1212
#include <vector>
1313

14-
#include "frc2/command/Command.h"
14+
#include "frc2/command/CommandBase.h"
1515

1616
namespace frc2 {
1717
/**
@@ -26,7 +26,7 @@ namespace frc2 {
2626
*/
2727
class CommandPtr final {
2828
public:
29-
explicit CommandPtr(std::unique_ptr<Command>&& command)
29+
explicit CommandPtr(std::unique_ptr<CommandBase>&& command)
3030
: m_ptr(std::move(command)) {}
3131

3232
template <class T, typename = std::enable_if_t<std::is_base_of_v<
@@ -222,12 +222,12 @@ class CommandPtr final {
222222
/**
223223
* Get a raw pointer to the held command.
224224
*/
225-
Command* get() const;
225+
CommandBase* get() const;
226226

227227
/**
228228
* Convert to the underlying unique_ptr.
229229
*/
230-
std::unique_ptr<Command> Unwrap() &&;
230+
std::unique_ptr<CommandBase> Unwrap() &&;
231231

232232
/**
233233
* Schedules this command.
@@ -271,7 +271,7 @@ class CommandPtr final {
271271
std::vector<CommandPtr>&& vec);
272272

273273
private:
274-
std::unique_ptr<Command> m_ptr;
274+
std::unique_ptr<CommandBase> m_ptr;
275275
void AssertValid() const;
276276
};
277277

0 commit comments

Comments
 (0)