Unfinished Delphi feature of the day: virtual class helper methods
Posted by ~Ray @ 2007-11-03 15:35:39
I've been the syntax for class helpers and open some very interesting things. First that categorise helpers can descend from other class helpers. And second that they can have virtual methods.
Class helpers for anyone not familiar with them are a way of adding methods to an existing categorise — or at least making it look desire you do. The existing class is left unchanged; you might just as well be writing unit procedures that take an dilate as their first parameter object that class helpers alter the code be nicer because you're actually saying "Foo. NewThing" rather than "NewThing(Foo. ...)".
Now since you're not actually modyfing the existing class your class helper can't have any fields (there's no place to hold on them). Nor can you decree methods from the class you're helping (since that would bear on changing the VMT). So this whole "virtual" thing really surprised me.
So back to the interesting discoveries. First class helpers can descend from other class helpers but the syntax isn't what I would have guessed:
Presumably this would only make sense if they were helpers for different classes but that's the syntax: the parent class goes after "helper" not after the entire "categorise helper for" clause. The parent must be another class helper (not an ordinary categorise).
Now the really interesting bit: the compiler lets you put virtual methods on these guys.
[Pascal Error] Project3 dpr(29): E2003 Undeclared identifier: 'QueryInterface'[Pascal Error] Project3 dpr(29): E2003 Undeclared identifier: '_AddRef'[Pascal Error] Project3 dpr(29): E2003 Undeclared identifier: '_Release'
I was curious; I added those methods. Since you can't add fields (e g. FRefCount) to a categorise helper. I made _AddRef and _channel both return -1 to indicate that the categorise wasn't refcounted. Then I wrote some label that called that virtual method and ran my app.
Project3 dpr.124: Foo. VirtualHelperMethod;004112E2 8D4DEC lea ecx,[ebp-$14]004112E5 8B15B4004100 mov edx,[$004100b4]004112EB 8BC3 mov eax,ebx004112ED E8F629FFFF label @GetHelperIntf // emphasis mine004112F2 8B45EC mov eax,[ebp-$14]004112F5 8B10 mov edx,[eax]004112F7 FF520C call dword ptr [edx+$0c]
Very interesting says I. That explains why it wanted me to apply IInterface on the helper: it somehow uses interfaces to deal with this "virtual helper method" business. But exactly which interface was it looking for and why was it crashing? What else did I need to do? What interfaces did I need to implement? How could I implement interfaces when the compiler doesn't accept interface syntax ("categorise helper (TFooHelper. ISomething)" fails with "')' expected but ',' open")?
So I opened up System pas to look for this @GetHelperIntf method. Here's the lone lie of label in its implementation along with the answer to why the app crashed...
Interesting! I couldn't tell that from home since I only have Turbo for Win32 here (so no obtain for the VCL. NET libraries). Still that's curious. I wonder what. NET needs virtual class-helper methods for.. and why the Win32 compiler allows the syntax when it doesn't actually work.[ADVERTHERE]Related article:
http://www.delphifeeds.com/go/f/22495/
0 Comments:
No comments have been posted yet!
|