aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction
diff options
context:
space:
mode:
authorNicola Mettifogo2007-04-10 18:46:34 +0000
committerNicola Mettifogo2007-04-10 18:46:34 +0000
commit6f8e77eb76c4311d69ac99ba3bcc704a216896c6 (patch)
tree48ad7112d7391aa029d71d7254f43b5ae5e54ddf /engines/parallaction
parentad3a3fe1d1d545a6818121a848eddaeaaf947c68 (diff)
downloadscummvm-rg350-6f8e77eb76c4311d69ac99ba3bcc704a216896c6.tar.gz
scummvm-rg350-6f8e77eb76c4311d69ac99ba3bcc704a216896c6.tar.bz2
scummvm-rg350-6f8e77eb76c4311d69ac99ba3bcc704a216896c6.zip
Moved Jobs to ManagedList. Since Jobs must be ordered according to their priority, a new insertSorted method has been added to the implementation.
svn-id: r26451
Diffstat (limited to 'engines/parallaction')
-rw-r--r--engines/parallaction/defs.h14
-rw-r--r--engines/parallaction/parallaction.cpp43
-rw-r--r--engines/parallaction/parallaction.h7
3 files changed, 41 insertions, 23 deletions
diff --git a/engines/parallaction/defs.h b/engines/parallaction/defs.h
index 41450bfbbe..37495e8f46 100644
--- a/engines/parallaction/defs.h
+++ b/engines/parallaction/defs.h
@@ -51,6 +51,8 @@ public:
typedef typename Common::List<T> Common_List;
typedef typename Common::List<T>::iterator iterator;
+ typedef int (*CompareFunction) (const T& a, const T& b);
+
~ManagedList() {
clear();
}
@@ -71,6 +73,17 @@ public:
return Common_List::erase(first, last);
}
+ // keeps list ordered in *ascending* order, as expressed by the compare function
+ void insertSorted(const T& element, CompareFunction compare) {
+ iterator it = Common_List::begin();
+ for ( ; it != Common_List::end(); it++)
+ if (compare(element, *it) < 0) break;
+
+ if (it == Common_List::end())
+ Common_List::push_back(element);
+ else
+ Common_List::insert(it, element);
+ }
};
} // namespace Parallaction
@@ -80,3 +93,4 @@ public:
+
diff --git a/engines/parallaction/parallaction.cpp b/engines/parallaction/parallaction.cpp
index fc0399f853..58ee2dcd78 100644
--- a/engines/parallaction/parallaction.cpp
+++ b/engines/parallaction/parallaction.cpp
@@ -834,6 +834,14 @@ void removeNode(Node *n) {
return;
}
+/*
+ helper function to provide *descending* ordering of the job list
+ (higher priorities values comes first in the list)
+*/
+int compareJobPriority(const JobPointer &j1, const JobPointer &j2) {
+ if (j1->_tag == j2->_tag) return 0;
+ return (j1->_tag >= j2->_tag ? -1 : 1);
+}
Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) {
@@ -845,14 +853,7 @@ Job *Parallaction::addJob(JobFn fn, void *parm, uint16 tag) {
v8->_finished = 0;
v8->_count = 0;
- Job *v4 = &_jobs;
-
- // TODO (LIST): the loop will be useless once we have an ordered
- // list _jobs. So this code will just be: _jobs.insert(v8)
- while (v4->_next && ((Job*)(v4->_next))->_tag > tag) {
- v4 = (Job*)v4->_next;
- }
- addNode(v4, v8);
+ _jobs.insertSorted(v8, compareJobPriority);
return v8;
}
@@ -876,22 +877,22 @@ void Parallaction::runJobs() {
if (_engineFlags & kEnginePauseJobs) return;
- Job *j = (Job*)_jobs._next;
- while (j) {
- debugC(3, kDebugJobs, "runJobs: %i", j->_tag);
-
- Job *v4 = (Job*)j->_next;
-
- if (j->_finished == 1) {
- removeNode(j);
- delete j;
- } else {
- (*j->_fn)(j->_parm, j);
- }
+ JobList::iterator it = _jobs.begin();
+ while (it != _jobs.end()) {
+ if ((*it)->_finished == 1)
+ it = _jobs.erase(it);
+ else
+ it++;
+ }
- j = v4;
+ it = _jobs.begin();
+ while (it != _jobs.end()) {
+ debugC(3, kDebugJobs, "runJobs: %i", (*it)->_tag);
+ (*(*it)->_fn)((*it)->_parm, (*it));
+ it++;
}
+
return;
}
diff --git a/engines/parallaction/parallaction.h b/engines/parallaction/parallaction.h
index 5ac53c932f..8786998eea 100644
--- a/engines/parallaction/parallaction.h
+++ b/engines/parallaction/parallaction.h
@@ -135,7 +135,7 @@ struct PARALLACTIONGameDescription;
struct Job;
typedef void (*JobFn)(void*, Job*);
-struct Job : public Node {
+struct Job {
uint16 _count; // # of executions left
uint16 _tag; // used for ordering
uint16 _finished;
@@ -147,6 +147,9 @@ public:
}
};
+typedef Job* JobPointer;
+typedef ManagedList<JobPointer> JobList;
+
struct Credit {
const char *_role;
const char *_name;
@@ -399,7 +402,7 @@ protected: // data
int16 _keyDown;
- Job _jobs;
+ JobList _jobs;
Node helperNode; // used for freeZones: to be removed