Skip to content

Commit 6b73ded

Browse files
committed
Merge commit 'cc9f140ef40b762918df4cc7fb23b60efd4674b0' into upstream
2 parents 69f6a33 + cc9f140 commit 6b73ded

File tree

9 files changed

+142
-81
lines changed

9 files changed

+142
-81
lines changed

commands2/src/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() {

commands2/src/cpp/frc2/command/CommandPtr.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "frc2/command/CommandPtr.h"
66

7+
#include <frc/Errors.h>
8+
79
#include "frc2/command/CommandScheduler.h"
810
#include "frc2/command/ConditionalCommand.h"
911
#include "frc2/command/InstantCommand.h"
@@ -20,12 +22,21 @@
2022

2123
using namespace frc2;
2224

25+
void CommandPtr::AssertValid() const {
26+
if (!m_ptr) {
27+
throw FRC_MakeError(frc::err::CommandIllegalUse,
28+
"Moved-from CommandPtr object used!");
29+
}
30+
}
31+
2332
CommandPtr CommandPtr::Repeatedly() && {
33+
AssertValid();
2434
m_ptr = std::make_unique<RepeatCommand>(std::move(m_ptr));
2535
return std::move(*this);
2636
}
2737

2838
CommandPtr CommandPtr::AsProxy() && {
39+
AssertValid();
2940
m_ptr = std::make_unique<ProxyScheduleCommand>(std::move(m_ptr));
3041
return std::move(*this);
3142
}
@@ -44,6 +55,7 @@ class RunsWhenDisabledCommand : public WrapperCommand {
4455
};
4556

4657
CommandPtr CommandPtr::IgnoringDisable(bool doesRunWhenDisabled) && {
58+
AssertValid();
4759
m_ptr = std::make_unique<RunsWhenDisabledCommand>(std::move(m_ptr),
4860
doesRunWhenDisabled);
4961
return std::move(*this);
@@ -67,25 +79,29 @@ class InterruptBehaviorCommand : public WrapperCommand {
6779

6880
CommandPtr CommandPtr::WithInterruptBehavior(
6981
InterruptionBehavior interruptBehavior) && {
82+
AssertValid();
7083
m_ptr = std::make_unique<InterruptBehaviorCommand>(std::move(m_ptr),
7184
interruptBehavior);
7285
return std::move(*this);
7386
}
7487

7588
CommandPtr CommandPtr::AndThen(std::function<void()> toRun,
7689
std::span<Subsystem* const> requirements) && {
90+
AssertValid();
7791
return std::move(*this).AndThen(CommandPtr(
7892
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
7993
}
8094

8195
CommandPtr CommandPtr::AndThen(
8296
std::function<void()> toRun,
8397
std::initializer_list<Subsystem*> requirements) && {
98+
AssertValid();
8499
return std::move(*this).AndThen(CommandPtr(
85100
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
86101
}
87102

88103
CommandPtr CommandPtr::AndThen(CommandPtr&& next) && {
104+
AssertValid();
89105
std::vector<std::unique_ptr<Command>> temp;
90106
temp.emplace_back(std::move(m_ptr));
91107
temp.emplace_back(std::move(next).Unwrap());
@@ -95,18 +111,21 @@ CommandPtr CommandPtr::AndThen(CommandPtr&& next) && {
95111

96112
CommandPtr CommandPtr::BeforeStarting(
97113
std::function<void()> toRun, std::span<Subsystem* const> requirements) && {
114+
AssertValid();
98115
return std::move(*this).BeforeStarting(CommandPtr(
99116
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
100117
}
101118

102119
CommandPtr CommandPtr::BeforeStarting(
103120
std::function<void()> toRun,
104121
std::initializer_list<Subsystem*> requirements) && {
122+
AssertValid();
105123
return std::move(*this).BeforeStarting(CommandPtr(
106124
std::make_unique<InstantCommand>(std::move(toRun), requirements)));
107125
}
108126

109127
CommandPtr CommandPtr::BeforeStarting(CommandPtr&& before) && {
128+
AssertValid();
110129
std::vector<std::unique_ptr<Command>> temp;
111130
temp.emplace_back(std::move(before).Unwrap());
112131
temp.emplace_back(std::move(m_ptr));
@@ -115,6 +134,7 @@ CommandPtr CommandPtr::BeforeStarting(CommandPtr&& before) && {
115134
}
116135

117136
CommandPtr CommandPtr::WithTimeout(units::second_t duration) && {
137+
AssertValid();
118138
std::vector<std::unique_ptr<Command>> temp;
119139
temp.emplace_back(std::make_unique<WaitCommand>(duration));
120140
temp.emplace_back(std::move(m_ptr));
@@ -123,6 +143,7 @@ CommandPtr CommandPtr::WithTimeout(units::second_t duration) && {
123143
}
124144

125145
CommandPtr CommandPtr::Until(std::function<bool()> condition) && {
146+
AssertValid();
126147
std::vector<std::unique_ptr<Command>> temp;
127148
temp.emplace_back(std::make_unique<WaitUntilCommand>(std::move(condition)));
128149
temp.emplace_back(std::move(m_ptr));
@@ -131,13 +152,15 @@ CommandPtr CommandPtr::Until(std::function<bool()> condition) && {
131152
}
132153

133154
CommandPtr CommandPtr::Unless(std::function<bool()> condition) && {
155+
AssertValid();
134156
m_ptr = std::make_unique<ConditionalCommand>(
135157
std::make_unique<InstantCommand>(), std::move(m_ptr),
136158
std::move(condition));
137159
return std::move(*this);
138160
}
139161

140162
CommandPtr CommandPtr::DeadlineWith(CommandPtr&& parallel) && {
163+
AssertValid();
141164
std::vector<std::unique_ptr<Command>> vec;
142165
vec.emplace_back(std::move(parallel).Unwrap());
143166
m_ptr =
@@ -146,6 +169,7 @@ CommandPtr CommandPtr::DeadlineWith(CommandPtr&& parallel) && {
146169
}
147170

148171
CommandPtr CommandPtr::AlongWith(CommandPtr&& parallel) && {
172+
AssertValid();
149173
std::vector<std::unique_ptr<Command>> vec;
150174
vec.emplace_back(std::move(m_ptr));
151175
vec.emplace_back(std::move(parallel).Unwrap());
@@ -154,6 +178,7 @@ CommandPtr CommandPtr::AlongWith(CommandPtr&& parallel) && {
154178
}
155179

156180
CommandPtr CommandPtr::RaceWith(CommandPtr&& parallel) && {
181+
AssertValid();
157182
std::vector<std::unique_ptr<Command>> vec;
158183
vec.emplace_back(std::move(m_ptr));
159184
vec.emplace_back(std::move(parallel).Unwrap());
@@ -179,11 +204,13 @@ class FinallyCommand : public WrapperCommand {
179204
} // namespace
180205

181206
CommandPtr CommandPtr::FinallyDo(std::function<void(bool)> end) && {
207+
AssertValid();
182208
m_ptr = std::make_unique<FinallyCommand>(std::move(m_ptr), std::move(end));
183209
return std::move(*this);
184210
}
185211

186212
CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
213+
AssertValid();
187214
return std::move(*this).FinallyDo(
188215
[handler = std::move(handler)](bool interrupted) {
189216
if (interrupted) {
@@ -192,30 +219,40 @@ CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
192219
});
193220
}
194221

195-
Command* CommandPtr::get() const {
222+
CommandBase* CommandPtr::get() const {
223+
AssertValid();
196224
return m_ptr.get();
197225
}
198226

199-
std::unique_ptr<Command> CommandPtr::Unwrap() && {
227+
std::unique_ptr<CommandBase> CommandPtr::Unwrap() && {
228+
AssertValid();
200229
return std::move(m_ptr);
201230
}
202231

203232
void CommandPtr::Schedule() const {
233+
AssertValid();
204234
CommandScheduler::GetInstance().Schedule(*this);
205235
}
206236

207237
void CommandPtr::Cancel() const {
238+
AssertValid();
208239
CommandScheduler::GetInstance().Cancel(*this);
209240
}
210241

211242
bool CommandPtr::IsScheduled() const {
243+
AssertValid();
212244
return CommandScheduler::GetInstance().IsScheduled(*this);
213245
}
214246

215247
bool CommandPtr::HasRequirement(Subsystem* requirement) const {
248+
AssertValid();
216249
return m_ptr->HasRequirement(requirement);
217250
}
218251

252+
CommandPtr::operator bool() const {
253+
return m_ptr.operator bool();
254+
}
255+
219256
std::vector<std::unique_ptr<Command>> CommandPtr::UnwrapVector(
220257
std::vector<CommandPtr>&& vec) {
221258
std::vector<std::unique_ptr<Command>> ptrs;

commands2/src/cpp/frc2/command/Commands.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "frc2/command/PrintCommand.h"
1515
#include "frc2/command/ProxyScheduleCommand.h"
1616
#include "frc2/command/RunCommand.h"
17-
#include "frc2/command/SelectCommand.h"
1817
#include "frc2/command/SequentialCommandGroup.h"
1918
#include "frc2/command/WaitCommand.h"
2019
#include "frc2/command/WaitUntilCommand.h"
@@ -100,14 +99,6 @@ CommandPtr cmd::Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
10099
.ToPtr();
101100
}
102101

103-
template <typename Key>
104-
CommandPtr cmd::Select(std::function<Key()> selector,
105-
std::vector<std::pair<Key, CommandPtr>> commands) {
106-
return SelectCommand(std::move(selector),
107-
CommandPtr::UnwrapVector(std::move(commands)))
108-
.ToPtr();
109-
}
110-
111102
CommandPtr cmd::Sequence(std::vector<CommandPtr>&& commands) {
112103
return SequentialCommandGroup(CommandPtr::UnwrapVector(std::move(commands)))
113104
.ToPtr();

commands2/src/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

commands2/src/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
}

commands2/src/include/frc2/command/CommandPtr.h

Lines changed: 11 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.
@@ -259,14 +259,20 @@ class CommandPtr final {
259259
*/
260260
bool HasRequirement(Subsystem* requirement) const;
261261

262+
/**
263+
* Check if this CommandPtr object is valid and wasn't moved-from.
264+
*/
265+
explicit operator bool() const;
266+
262267
/**
263268
* Convert a vector of CommandPtr objects to their underlying unique_ptrs.
264269
*/
265270
static std::vector<std::unique_ptr<Command>> UnwrapVector(
266271
std::vector<CommandPtr>&& vec);
267272

268273
private:
269-
std::unique_ptr<Command> m_ptr;
274+
std::unique_ptr<CommandBase> m_ptr;
275+
void AssertValid() const;
270276
};
271277

272278
} // namespace frc2

commands2/src/include/frc2/command/CommandScheduler.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ class CommandScheduler final : public nt::NTSendable,
182182
throw FRC_MakeError(frc::err::CommandIllegalUse,
183183
"Default commands must require their subsystem!");
184184
}
185-
if (defaultCommand.IsFinished()) {
186-
throw FRC_MakeError(frc::err::CommandIllegalUse,
187-
"Default commands should not end!");
188-
}
189185
SetDefaultCommandImpl(subsystem,
190186
std::make_unique<std::remove_reference_t<T>>(
191187
std::forward<T>(defaultCommand)));

0 commit comments

Comments
 (0)