/* * complextask.cpp * * Created on: 06.06.2013 * Author: trifon */ #include "complextask.h" #include "stack.cpp" template<> void Stack::print() { Task* x; while(pop(x)) x->print(); cout << endl; } ComplexTask::ComplexTask(char const* _name) : BaseTask(_name), progress(0) {} Cloneable* ComplexTask::clone() const { return new ComplexTask(*this); } void ComplexTask::print() const { cout << "Сложна задача "; BaseTask::print(); cout << ", която съдържа задачите:" << endl; Stack s = *this; s.print(); } int ComplexTask::time() const { Stack tmp = *this; Task* current; int result = 0; while (tmp.pop(current)) result += current->time(); return result; } Task* ComplexTask::getCurrent() const { if (empty()) return NULL; return top->inf; } int ComplexTask::getProgress() const { if (getCurrent() == NULL) return progress; return progress + getCurrent()->getProgress(); } int ComplexTask::work(int t) { while(t > 0 && !empty()) { t = getCurrent()->work(t); if (getCurrent()->isFinished()) { Task* finished; pop(finished); progress += finished->time(); delete finished; } } return t; } void ComplexTask::cloneAll() { elem* p = top; while (p) { p->inf = (Task*)p->inf->clone(); p = p->link; } } void ComplexTask::deleteAll() { elem* p = top; while (p) { delete p->inf; p = p->link; } } ComplexTask::ComplexTask(ComplexTask const& ct) : Stack(ct), BaseTask(ct), progress(ct.progress) { cloneAll(); } ComplexTask& ComplexTask::operator=(ComplexTask const& ct) { if (&ct != this) { deleteAll(); BaseTask::operator=(ct); Stack::operator=(ct); cloneAll(); } return *this; } ComplexTask::~ComplexTask() { deleteAll(); }