void ProcessManager::UpdateProcesses(float elapsed_time) { // Iterate over all processes. ProcessList::iterator it = processes_.begin(); while (it != processes_.end()) { StrongProcessPtr process = *it; // If the process hasn't been initialized yet. if (process->GetState() == Process::State::UNINITIALIZED) { // Initialize it. process->OnInit(); } // If the process is running. if (process->GetState() == Process::State::RUNNING) { // Update it. process->OnUpdate(elapsed_time); } // If the process is dead. if (process->IsDead()) { // If he exited successfully. if (process->GetState() == Process::State::SUCCEEDED) { // Call the appropriate exit method. process->OnSuccess(); // Attach the children to the process manager if the process had some. const std::vector<StrongProcessPtr>& children = process->GetChildren(); if (!children.empty()) for (StrongProcessPtr child : children) AddProcess(child); process->ClearChildren(); } // If he exited with an failure. else if (process->GetState() == Process::State::FAILED) { process->OnFail(); } // If he was aborted. else if (process->GetState() == Process::State::ABORTED) { process->OnAbort(); } // Remove the process and set the iterator to the next process. it = processes_.erase(it); } // If the process isn't dead we have to increment the iterator. else { ++it; } } }
void ProcessManager::AbortAllProcesses(bool immediate) { auto it = m_ProcessList.begin(); while (it != m_ProcessList.end()) { auto tempIt = it; ++it; StrongProcessPtr pProcess = *tempIt; if (pProcess->IsAlive()) { pProcess->SetState(Process::ABORTED); // if immediate, call the abort code and erase the process now if (immediate) { pProcess->OnAbort(); m_ProcessList.erase(tempIt); } } } }
unsigned int ProcessManager::UpdateProcesses(float deltaTime) { unsigned short int successCount = 0; unsigned short int failCount = 0; auto it = m_ProcessList.begin(); while (it != m_ProcessList.end()) { StrongProcessPtr pCurrentProcess = (*it); // save an iterator to this process and increment our list iterator auto thisIt = it; ++it; // init the current process if it has not intialized yet if (pCurrentProcess->GetState() == Process::UNINITIALIZED) { pCurrentProcess->OnInit(); } // update a running process if (pCurrentProcess->GetState() == Process::RUNNING) { pCurrentProcess->OnUpdate(deltaTime); } // if a process is dead, run the correct exit method if (pCurrentProcess->IsDead()) { switch (pCurrentProcess->GetState()) { case Process::SUCCEEDED: { pCurrentProcess->OnSuccess(); StrongProcessPtr pChild = pCurrentProcess->RemoveChild(); if (pChild) AttachProcess(pChild); else ++successCount; break; } case Process::FAILED: { pCurrentProcess->OnFail(); ++failCount; break; } case Process::ABORTED: { pCurrentProcess->OnAbort(); ++failCount; break; } } // destroy the dead process m_ProcessList.erase(thisIt); } } return (successCount << 16) | failCount; }