7 #include "Commands/CommandGroup.h"
15 m_currentCommandIndex = -1;
25 m_currentCommandIndex = -1;
28 CommandGroup::~CommandGroup()
48 wpi_setWPIErrorWithContext(NullParameter,
"command");
56 m_commands.push_back(
CommandGroupEntry(command, CommandGroupEntry::kSequence_InSequence));
59 Command::SubsystemSet::iterator iter = requirements.begin();
60 for (; iter != requirements.end(); iter++)
85 wpi_setWPIErrorWithContext(NullParameter,
"command");
92 wpi_setWPIErrorWithContext(ParameterOutOfRange,
"timeout < 0.0");
98 m_commands.push_back(
CommandGroupEntry(command, CommandGroupEntry::kSequence_InSequence, timeout));
101 Command::SubsystemSet::iterator iter = requirements.begin();
102 for (; iter != requirements.end(); iter++)
129 wpi_setWPIErrorWithContext(NullParameter,
"command");
137 m_commands.push_back(
CommandGroupEntry(command, CommandGroupEntry::kSequence_BranchChild));
140 Command::SubsystemSet::iterator iter = requirements.begin();
141 for (; iter != requirements.end(); iter++)
173 wpi_setWPIErrorWithContext(NullParameter,
"command");
180 wpi_setWPIErrorWithContext(ParameterOutOfRange,
"timeout < 0.0");
186 m_commands.push_back(
CommandGroupEntry(command, CommandGroupEntry::kSequence_BranchChild, timeout));
189 Command::SubsystemSet::iterator iter = requirements.begin();
190 for (; iter != requirements.end(); iter++)
194 void CommandGroup::_Initialize()
196 m_currentCommandIndex = -1;
199 void CommandGroup::_Execute()
203 bool firstRun =
false;
205 if (m_currentCommandIndex == -1)
208 m_currentCommandIndex = 0;
211 while ((
unsigned)m_currentCommandIndex < m_commands.size())
215 if (entry.IsTimedOut())
225 m_currentCommandIndex++;
232 entry = m_commands[m_currentCommandIndex];
235 switch (entry.m_state)
237 case CommandGroupEntry::kSequence_InSequence:
238 cmd = entry.m_command;
242 CancelConflicts(cmd);
247 case CommandGroupEntry::kSequence_BranchPeer:
248 m_currentCommandIndex++;
249 entry.m_command->
Start();
252 case CommandGroupEntry::kSequence_BranchChild:
253 m_currentCommandIndex++;
254 CancelConflicts(entry.m_command);
255 entry.m_command->StartRunning();
256 m_children.push_back(entry);
262 CommandList::iterator iter = m_children.begin();
263 for (; iter != m_children.end();)
266 Command *child = entry.m_command;
267 if (entry.IsTimedOut())
273 iter = m_children.erase(iter);
282 void CommandGroup::_End()
285 if (m_currentCommandIndex != -1 && (
unsigned)m_currentCommandIndex < m_commands.size())
287 Command *cmd = m_commands[m_currentCommandIndex].m_command;
292 CommandList::iterator iter = m_children.begin();
293 for (; iter != m_children.end(); iter++)
295 Command *cmd = iter->m_command;
302 void CommandGroup::_Interrupted()
329 return (
unsigned)m_currentCommandIndex >= m_commands.size() && m_children.empty();
332 bool CommandGroup::IsInterruptible()
337 if (m_currentCommandIndex != -1 && (
unsigned)m_currentCommandIndex < m_commands.size())
339 Command *cmd = m_commands[m_currentCommandIndex].m_command;
344 CommandList::iterator iter = m_children.begin();
345 for (; iter != m_children.end(); iter++)
347 if (!iter->m_command->IsInterruptible())
354 void CommandGroup::CancelConflicts(
Command *command)
356 CommandList::iterator childIter = m_children.begin();
357 for (; childIter != m_children.end();)
359 Command *child = childIter->m_command;
363 Command::SubsystemSet::iterator requirementIter = requirements.begin();
364 for (; requirementIter != requirements.end(); requirementIter++)
370 childIter = m_children.erase(childIter);
380 int CommandGroup::GetSize()
382 return m_children.size();
void AddSequential(Command *command)
bool AssertUnlocked(const char *message)
void Requires(Subsystem *s)
virtual void Interrupted()
void AddParallel(Command *command)
void SetParent(CommandGroup *parent)
bool DoesRequire(Subsystem *subsystem)
virtual void Initialize()
virtual bool IsFinished()
SubsystemSet GetRequirements()