/* * repeattask.cpp * * Created on: 06.06.2013 * Author: trifon */ #include "repeattask.h" #include using namespace std; RepeatTask::RepeatTask(char const* _name, int _total, Task const* _prototype) : SimpleTask(_name, _total), prototype(_prototype), current(NULL) { reset(); } Cloneable* RepeatTask::clone() const { return new RepeatTask(*this); } void RepeatTask::reset() { delete current; current = (Task*)prototype->clone(); } void RepeatTask::print() const { cout << "Повтаряща се "; BaseTask::print(); cout << ", от която са изпълнени вече "; cout << progress << " повторения от общо "; cout << total << ", като текущата задача е "; current->print(); } int RepeatTask::time() const { return total * prototype->time(); } int RepeatTask::getProgress() const { return progress * prototype->time() + current->getProgress(); } int RepeatTask::work(int t) { /* while (progress < total && t > 0) { t = current->work(t); if (current->isFinished()) { progress++; if (progress < total) reset(); } } return t;*/ if (progress < total && t > 0) { t = current->work(t); if (current->isFinished()) { reset(); int repetitionsToDo = t / prototype->time() + 1; int repetitionsLeft = SimpleTask::work(repetitionsToDo); t = repetitionsLeft * prototype->time() + t % prototype->time(); if (progress < total) t = current->work(t); // сигурни сме, че !current->isFinished() } } return t; } RepeatTask::RepeatTask(RepeatTask const& rt) : SimpleTask(rt), prototype(rt.prototype), current((Task*)rt.current->clone()) {} RepeatTask& RepeatTask::operator=(RepeatTask const& rt) { if (&rt != this) { delete current; prototype = rt.prototype; current = (Task*)rt.current->clone(); } return *this; } RepeatTask::~RepeatTask() { delete current; }