[comp.lang.pascal] Procedural Types and Object Methods

ins845b@monu4.cc.monash.edu.au (mr k.l. lentin) (05/27/91)

I have come across this problem and have fixed it but would like to know if
there is a better way of doing so.

I am writing a program to simulate a warehousing operation. THere are a number
of different types of objects in the simulation such as trucks, load crews,
order points, fork lift drivers etc. Each is an object type which inherits a
base object called entity.

Entity contains a pointer to an event record which is placed on an event
queue. An event record contains the time the event must happen and which
object the event concerns. e.g. a certain turck will have to do something at
time 10. 

In each descendant of entity I define a couple of procedures which represent
the actions an entity can do. Eg. the truck has 2 methods (so far) called
arrive and leave.

What I want to be able to do is store the next action in the entity record (or
the event record) and let my scheduling algorithm (which just removes an event
from the queue and executes it) just call the object it finds in the event
record and get it to call the appropriate action WITHOUT using a case
statement.

All this has a very simple solution: PROCEDURAL TYPES. I have apointer to a
procedure in the entity record (called action) so the scheduler would do
something like firstevent^.entity^.action BUT here is the problem, the
procedure getting called (ie stored in action) are methods not procedures.
Thus the correct object (entity) is called and its self parameter is sdet up
correctly, however when action is called, no self paramenter is passed becuase
action is a pointer to a procedure not method. RESULT: MAJOR SYSTEM HANG!!!

What I would like to do is to be able to define action as a pointer to a
method but there is no way to do this (in TP6.0 - sorry to mention that so
late :-). The solution I have found is to declare the procedural type as
^procedure(self : entityObj);
instead of
^procedure; 
Then when i call it in the scheduler I call entity^.action(entity) thus
creating the self parameter. On the inside of the object the methods are still
defined as 
procedure actionname;
without a parameter and now all works FINE! only problem is I have to hand
this in as a university assignment and now I have declared and called a
procedure with different parameter lists - NOT A GOOD THING TO DO for an
assignment.

Is there a solution? Or will I have to do it this way.?


|/
|\evin (sorry this got so long :-)