Skip to content

Commit a2da722

Browse files
committed
merge b4
2 parents 748c84c + b155f25 commit a2da722

File tree

11 files changed

+144
-87
lines changed

11 files changed

+144
-87
lines changed

commands2/src/cpp/frc2/command/Command.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,32 @@ void Command::End(bool interrupted) {}
3838

3939
/*
4040
CommandPtr Command::WithTimeout(units::second_t duration) && {
41-
return CommandPtr(std::move(*this).TransferOwnership()).WithTimeout(duration);
41+
return std::move(*this).ToPtr().WithTimeout(duration);
4242
}
4343
*/
4444

4545
/*
4646
CommandPtr Command::Until(std::function<bool()> condition) && {
47-
return CommandPtr(std::move(*this).TransferOwnership())
48-
.Until(std::move(condition));
47+
return std::move(*this).ToPtr().Until(std::move(condition));
4948
}
5049
*/
5150

5251
/*
5352
CommandPtr Command::IgnoringDisable(bool doesRunWhenDisabled) && {
54-
return CommandPtr(std::move(*this).TransferOwnership())
55-
.IgnoringDisable(doesRunWhenDisabled);
53+
return std::move(*this).ToPtr().IgnoringDisable(doesRunWhenDisabled);
5654
}
5755
*/
5856

5957
/*
6058
CommandPtr Command::WithInterruptBehavior(
6159
InterruptionBehavior interruptBehavior) && {
62-
return CommandPtr(std::move(*this).TransferOwnership())
63-
.WithInterruptBehavior(interruptBehavior);
60+
return std::move(*this).ToPtr().WithInterruptBehavior(interruptBehavior);
6461
}
6562
*/
6663

6764
/*
6865
CommandPtr Command::WithInterrupt(std::function<bool()> condition) && {
69-
return CommandPtr(std::move(*this).TransferOwnership())
70-
.Until(std::move(condition));
66+
return std::move(*this).ToPtr().Until(std::move(condition));
7167
}
7268
*/
7369

@@ -83,8 +79,8 @@ CommandPtr Command::BeforeStarting(
8379
/*
8480
CommandPtr Command::BeforeStarting(
8581
std::function<void()> toRun, std::span<Subsystem* const> requirements) && {
86-
return CommandPtr(std::move(*this).TransferOwnership())
87-
.BeforeStarting(std::move(toRun), requirements);
82+
return std::move(*this).ToPtr().BeforeStarting(std::move(toRun),
83+
requirements);
8884
}
8985
*/
9086

@@ -99,8 +95,7 @@ CommandPtr Command::AndThen(std::function<void()> toRun,
9995
/*
10096
CommandPtr Command::AndThen(std::function<void()> toRun,
10197
std::span<Subsystem* const> requirements) && {
102-
return CommandPtr(std::move(*this).TransferOwnership())
103-
.AndThen(std::move(toRun), requirements);
98+
return std::move(*this).ToPtr().AndThen(std::move(toRun), requirements);
10499
}
105100
*/
106101

@@ -114,34 +109,31 @@ PerpetualCommand Command::Perpetually() && {
114109

115110
/*
116111
CommandPtr Command::Repeatedly() && {
117-
return CommandPtr(std::move(*this).TransferOwnership()).Repeatedly();
112+
return std::move(*this).ToPtr().Repeatedly();
118113
}
119114
*/
120115

121116
/*
122117
CommandPtr Command::AsProxy() && {
123-
return CommandPtr(std::move(*this).TransferOwnership()).AsProxy();
118+
return std::move(*this).ToPtr().AsProxy();
124119
}
125120
*/
126121

127122
/*
128123
CommandPtr Command::Unless(std::function<bool()> condition) && {
129-
return CommandPtr(std::move(*this).TransferOwnership())
130-
.Unless(std::move(condition));
124+
return std::move(*this).ToPtr().Unless(std::move(condition));
131125
}
132126
*/
133127

134128
/*
135129
CommandPtr Command::FinallyDo(std::function<void(bool)> end) && {
136-
return CommandPtr(std::move(*this).TransferOwnership())
137-
.FinallyDo(std::move(end));
130+
return std::move(*this).ToPtr().FinallyDo(std::move(end));
138131
}
139132
*/
140133

141134
/*
142135
CommandPtr Command::HandleInterrupt(std::function<void(void)> handler) && {
143-
return CommandPtr(std::move(*this).TransferOwnership())
144-
.HandleInterrupt(std::move(handler));
136+
return std::move(*this).ToPtr().HandleInterrupt(std::move(handler));
145137
}
146138
*/
147139

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "frc2/command/ProxyScheduleCommand.h"
1616
#include "frc2/command/RepeatCommand.h"
1717
#include "frc2/command/RunCommand.h"
18-
#include "frc2/command/SelectCommand.h"
1918
#include "frc2/command/SequentialCommandGroup.h"
2019
#include "frc2/command/WaitCommand.h"
2120
#include "frc2/command/WaitUntilCommand.h"
@@ -96,16 +95,6 @@ std::shared_ptr<Command> cmd::Either(std::shared_ptr<Command> onTrue, std::share
9695
std::move(onFalse), std::move(selector));
9796
}
9897

99-
/*
100-
template <typename Key>
101-
CommandPtr cmd::Select(std::function<Key()> selector,
102-
std::vector<std::pair<Key, CommandPtr>> commands) {
103-
return SelectCommand(std::move(selector),
104-
CommandPtr::UnwrapVector(std::move(commands)))
105-
.ToPtr();
106-
}
107-
*/
108-
10998
std::shared_ptr<Command> cmd::Sequence(std::vector<std::shared_ptr<Command> >&& commands) {
11099
return std::make_shared<SequentialCommandGroup>(std::move(commands));
111100
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,14 @@ safe) semantics.
359359

360360
virtual std::string GetName() const;
361361

362+
/**
363+
* Transfers ownership of this command to a unique pointer. Used for
364+
* decorator methods.
365+
*/
366+
/*
367+
virtual CommandPtr ToPtr() && = 0;
368+
*/
369+
362370
protected:
363371
/**
364372
* Transfers ownership of this command to a unique pointer. Used for

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ class CommandHelper : public Base {
2727
public:
2828
CommandHelper() = default;
2929

30-
CommandPtr ToPtr() && {
30+
/*
31+
CommandPtr ToPtr() && override {
3132
return CommandPtr(
3233
std::make_unique<CRTP>(std::move(*static_cast<CRTP*>(this))));
3334
}
35+
*/
3436

3537
protected:
3638
std::unique_ptr<Command> TransferOwnership() && override {

0 commit comments

Comments
 (0)