Dynamic object methods

Prof A Olowofoyeku (The African Chief) chiefsoft at bigfoot.com
Fri Apr 25 00:08:28 CEST 2003


On 24 Apr 2003 at 10:55, Frank Heckenbach wrote:

[...]
> > I may just be about to reveal my ignorance - but can the integer
> > values for dynamic methods not be stored somewhere in the VMT or some
> > other such place?
> 
> Sure, storing them is easy. The question is what to do with them
> afterwards?

That can be left to the programmer to do something with it, or to do 
nothing with it. Never underestimate the ability of programmers to do 
something with something ;-/
 
> > And if they can, is there some way of accessing the VMT or
> > its data?
> 
> `TypeOf' returns a `PObjectType' pointer which contains the
> invarying parts of VMTs (see the manual).
> 
> It is possible with some dirty tricks (type-casting etc.) to go
> beyond these fields and access the further contents of the VMT. But I'm
> not sure we should introduce an "official" feature for it. It wouldn't
> be too difficult to get the address of some virtual method, but the
> problem is to store the type (parameter etc.) information -- and much
> more, checking them on calling.
> 
> I.e., when you read the address of a method directly from the VMT,
> it will just be an untyped pointer, and the compiler can't guess the
> right type if the method is found at a non-constant place (e.g., by
> searching for a dynamic index).
> 
> So doing this would forfeit all type-checking. This is typical for
> type-casts, so as said above, I prefer to not make it possible
> unless one uses type-casts (on the VMT pointer in this case) ...
> 
> And again, I don't recommend this for this dispatcher. As I said, I
> can't see this index doing more than a `case' statement (if it does,
> then please explain ...), and writing one `case' statement is a small
> job (one time, and probably mostly automatic), compared to getting
> mysterious crashes etc. due to the lack of type-checking (and that for
> possibly every user of the library).

It can do more than a case statement. In fact, it can save one from 
having to use a whole load of case statements (which is what I had to 
do in my OOP framework, and I could have avoided all that, had this 
facility been available). Look at this rough (and probably defective, 
since I haven't really thought this through) example:

Type
TMethodType = Procedure (Sender : pObject);

Procedure Dispatcher (Sender : pObject; index: integer);
Var p : TMethodType;
begin
  If index <> 0 
  then begin
     p := GetMethodPtr (GetVMT (Sender), index);
     If Assigned (p) 
        then p (Sender) 
          else {raise an exception, be silent, or whatever}
  end;
end;

If I could have done this (or anything vaguely resembling it), my life 
would have been a lot easier when I was working on the OOP framework. 
Yes, this would have been type unsafe, etc., but it would only have 
been done in one place (the above dispatcher would be at the lowest 
level), and all that the user of the framework needs to know is that 
dynamic methods in the framework must have the format;
   procedure foo (Sender : pObject); virtual index_number;

Everything else is taken care of at a lower level, and users will not 
need to worry about producing case statements in subclasses, or 
whatever.

The above example of course presupposes that one can readily access the 
VMT and can retrieve various things for it (even if it would mean some 
hackery or dirty tricks).

Best regards, The Chief
--------
Prof. Abimbola A. Olowofoyeku (The African Chief) 
web:  http://www.bigfoot.com/~african_chief/




More information about the Gpc mailing list