Pierre Phaneuf wrote:
Ah, I didn't knew the size was at the beginning of the VMT... But this isn't quite the Right Way (if the VMT format changes for whatever reason), no?
Though the VMT format might not be likely to change, perhaps it would be better to have a built-in function of the compiler to return the object size of a given VMT link (should be easy to implement).
- Call the object's `Load' constructor.
This could cause problems. How shall I call the Load constructor?
function CreateObject(VMTLink: pointer; S: PStream): PObject; var P: PObject; Size: ^word; begin Size:=VMTLink; getmem(P, Size^); TypeOf(P):=VMTLink; P^.Load(S); end;
This wouldn't work (in BP at least), since this will call TObject's Load constructor and it will reset the type of the object to TObject, whatever has been set with TypeOf().
Couldn't "Load" be a virtual method instead of a constructor (perhaps a Boolean function that returns if it didn't fail)? I'm not sure if a constructor does anything special besides setting the VMT link which has been done here anyway, so if it does more, this suggestion might not be possible.
But if it's possible, you also wouldn't have to register the Load constructor (neither Store, of course), so the registration could also be simplified to something like RegisterType(ObjType, VMTLink:Word); and the programs that use this function don't have to declare a "StreamRec" manually, which would be another simplification.
And it would be even better if the compiler could do this automatically, i.e. each object type could (optionally) declare an "ObjType" value, and the compiler would provide functions to get the ObjType from a VMT link and vice versa.
Thinking about how to implement this, I realize this is basically the same as object constants -- something that's missing in BP and I could've used sometimes. This could be a useful extension to BP's objects. (The constants and their values would be inherited if not redeclared, just like virtual methods.)
"ObjType" could then be a normal object constant with only 2 differences: the compiler should make sure that no two types within a program have the same ObjType (especially, that no type accidentally inherits its parent's value!), and there must be a special function to get the VMT link from an ObjType.
And while I'm at it, class variables would also be nice, i.e. variables that exist only once for all instances of an object type (aka class). Again, I had some situations where I liked to have them. (For variables, there is a difference between object and class variables, for constants this difference doesn't matter.)
Just some ideas...