4
4
5
5
#include " frc2/command/CommandPtr.h"
6
6
7
+ #include < frc/Errors.h>
8
+
7
9
#include " frc2/command/CommandScheduler.h"
8
10
#include " frc2/command/ConditionalCommand.h"
9
11
#include " frc2/command/InstantCommand.h"
20
22
21
23
using namespace frc2 ;
22
24
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
+
23
32
CommandPtr CommandPtr::Repeatedly () && {
33
+ AssertValid ();
24
34
m_ptr = std::make_unique<RepeatCommand>(std::move (m_ptr));
25
35
return std::move (*this );
26
36
}
27
37
28
38
CommandPtr CommandPtr::AsProxy () && {
39
+ AssertValid ();
29
40
m_ptr = std::make_unique<ProxyScheduleCommand>(std::move (m_ptr));
30
41
return std::move (*this );
31
42
}
@@ -44,6 +55,7 @@ class RunsWhenDisabledCommand : public WrapperCommand {
44
55
};
45
56
46
57
CommandPtr CommandPtr::IgnoringDisable (bool doesRunWhenDisabled) && {
58
+ AssertValid ();
47
59
m_ptr = std::make_unique<RunsWhenDisabledCommand>(std::move (m_ptr),
48
60
doesRunWhenDisabled);
49
61
return std::move (*this );
@@ -67,25 +79,29 @@ class InterruptBehaviorCommand : public WrapperCommand {
67
79
68
80
CommandPtr CommandPtr::WithInterruptBehavior (
69
81
InterruptionBehavior interruptBehavior) && {
82
+ AssertValid ();
70
83
m_ptr = std::make_unique<InterruptBehaviorCommand>(std::move (m_ptr),
71
84
interruptBehavior);
72
85
return std::move (*this );
73
86
}
74
87
75
88
CommandPtr CommandPtr::AndThen (std::function<void ()> toRun,
76
89
std::span<Subsystem* const > requirements) && {
90
+ AssertValid ();
77
91
return std::move (*this ).AndThen (CommandPtr (
78
92
std::make_unique<InstantCommand>(std::move (toRun), requirements)));
79
93
}
80
94
81
95
CommandPtr CommandPtr::AndThen (
82
96
std::function<void ()> toRun,
83
97
std::initializer_list<Subsystem*> requirements) && {
98
+ AssertValid ();
84
99
return std::move (*this ).AndThen (CommandPtr (
85
100
std::make_unique<InstantCommand>(std::move (toRun), requirements)));
86
101
}
87
102
88
103
CommandPtr CommandPtr::AndThen (CommandPtr&& next) && {
104
+ AssertValid ();
89
105
std::vector<std::unique_ptr<Command>> temp;
90
106
temp.emplace_back (std::move (m_ptr));
91
107
temp.emplace_back (std::move (next).Unwrap ());
@@ -95,18 +111,21 @@ CommandPtr CommandPtr::AndThen(CommandPtr&& next) && {
95
111
96
112
CommandPtr CommandPtr::BeforeStarting (
97
113
std::function<void ()> toRun, std::span<Subsystem* const > requirements) && {
114
+ AssertValid ();
98
115
return std::move (*this ).BeforeStarting (CommandPtr (
99
116
std::make_unique<InstantCommand>(std::move (toRun), requirements)));
100
117
}
101
118
102
119
CommandPtr CommandPtr::BeforeStarting (
103
120
std::function<void ()> toRun,
104
121
std::initializer_list<Subsystem*> requirements) && {
122
+ AssertValid ();
105
123
return std::move (*this ).BeforeStarting (CommandPtr (
106
124
std::make_unique<InstantCommand>(std::move (toRun), requirements)));
107
125
}
108
126
109
127
CommandPtr CommandPtr::BeforeStarting (CommandPtr&& before) && {
128
+ AssertValid ();
110
129
std::vector<std::unique_ptr<Command>> temp;
111
130
temp.emplace_back (std::move (before).Unwrap ());
112
131
temp.emplace_back (std::move (m_ptr));
@@ -115,6 +134,7 @@ CommandPtr CommandPtr::BeforeStarting(CommandPtr&& before) && {
115
134
}
116
135
117
136
CommandPtr CommandPtr::WithTimeout (units::second_t duration) && {
137
+ AssertValid ();
118
138
std::vector<std::unique_ptr<Command>> temp;
119
139
temp.emplace_back (std::make_unique<WaitCommand>(duration));
120
140
temp.emplace_back (std::move (m_ptr));
@@ -123,6 +143,7 @@ CommandPtr CommandPtr::WithTimeout(units::second_t duration) && {
123
143
}
124
144
125
145
CommandPtr CommandPtr::Until (std::function<bool ()> condition) && {
146
+ AssertValid ();
126
147
std::vector<std::unique_ptr<Command>> temp;
127
148
temp.emplace_back (std::make_unique<WaitUntilCommand>(std::move (condition)));
128
149
temp.emplace_back (std::move (m_ptr));
@@ -131,13 +152,15 @@ CommandPtr CommandPtr::Until(std::function<bool()> condition) && {
131
152
}
132
153
133
154
CommandPtr CommandPtr::Unless (std::function<bool ()> condition) && {
155
+ AssertValid ();
134
156
m_ptr = std::make_unique<ConditionalCommand>(
135
157
std::make_unique<InstantCommand>(), std::move (m_ptr),
136
158
std::move (condition));
137
159
return std::move (*this );
138
160
}
139
161
140
162
CommandPtr CommandPtr::DeadlineWith (CommandPtr&& parallel) && {
163
+ AssertValid ();
141
164
std::vector<std::unique_ptr<Command>> vec;
142
165
vec.emplace_back (std::move (parallel).Unwrap ());
143
166
m_ptr =
@@ -146,6 +169,7 @@ CommandPtr CommandPtr::DeadlineWith(CommandPtr&& parallel) && {
146
169
}
147
170
148
171
CommandPtr CommandPtr::AlongWith (CommandPtr&& parallel) && {
172
+ AssertValid ();
149
173
std::vector<std::unique_ptr<Command>> vec;
150
174
vec.emplace_back (std::move (m_ptr));
151
175
vec.emplace_back (std::move (parallel).Unwrap ());
@@ -154,6 +178,7 @@ CommandPtr CommandPtr::AlongWith(CommandPtr&& parallel) && {
154
178
}
155
179
156
180
CommandPtr CommandPtr::RaceWith (CommandPtr&& parallel) && {
181
+ AssertValid ();
157
182
std::vector<std::unique_ptr<Command>> vec;
158
183
vec.emplace_back (std::move (m_ptr));
159
184
vec.emplace_back (std::move (parallel).Unwrap ());
@@ -179,11 +204,13 @@ class FinallyCommand : public WrapperCommand {
179
204
} // namespace
180
205
181
206
CommandPtr CommandPtr::FinallyDo (std::function<void (bool )> end) && {
207
+ AssertValid ();
182
208
m_ptr = std::make_unique<FinallyCommand>(std::move (m_ptr), std::move (end));
183
209
return std::move (*this );
184
210
}
185
211
186
212
CommandPtr CommandPtr::HandleInterrupt (std::function<void (void )> handler) && {
213
+ AssertValid ();
187
214
return std::move (*this ).FinallyDo (
188
215
[handler = std::move (handler)](bool interrupted) {
189
216
if (interrupted) {
@@ -192,30 +219,40 @@ CommandPtr CommandPtr::HandleInterrupt(std::function<void(void)> handler) && {
192
219
});
193
220
}
194
221
195
- Command* CommandPtr::get () const {
222
+ CommandBase* CommandPtr::get () const {
223
+ AssertValid ();
196
224
return m_ptr.get ();
197
225
}
198
226
199
- std::unique_ptr<Command> CommandPtr::Unwrap () && {
227
+ std::unique_ptr<CommandBase> CommandPtr::Unwrap () && {
228
+ AssertValid ();
200
229
return std::move (m_ptr);
201
230
}
202
231
203
232
void CommandPtr::Schedule () const {
233
+ AssertValid ();
204
234
CommandScheduler::GetInstance ().Schedule (*this );
205
235
}
206
236
207
237
void CommandPtr::Cancel () const {
238
+ AssertValid ();
208
239
CommandScheduler::GetInstance ().Cancel (*this );
209
240
}
210
241
211
242
bool CommandPtr::IsScheduled () const {
243
+ AssertValid ();
212
244
return CommandScheduler::GetInstance ().IsScheduled (*this );
213
245
}
214
246
215
247
bool CommandPtr::HasRequirement (Subsystem* requirement) const {
248
+ AssertValid ();
216
249
return m_ptr->HasRequirement (requirement);
217
250
}
218
251
252
+ CommandPtr::operator bool () const {
253
+ return m_ptr.operator bool ();
254
+ }
255
+
219
256
std::vector<std::unique_ptr<Command>> CommandPtr::UnwrapVector (
220
257
std::vector<CommandPtr>&& vec) {
221
258
std::vector<std::unique_ptr<Command>> ptrs;
0 commit comments