Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check that a descendent class overrides all virtual methods?

Context

In DelphiAST there is a base class: TmwSimplePasPar that parses Delphi code.
In addition there is a class: TPasSyntaxTreeBuilder that is supposed to override every method in its base class.

Question

Is there a way to check that a descendent class has overridden every single virtual method in its base class?
It would be nice to be able to put in a assert.

TPasSyntaxTreeBuilder.Create;
begin
  Assert(Self.OverridenMethods.Count = (BaseClass.VirtualMethodCount - TObject.VirtualMethodCount)); 
  ....

Note that I'm talking about all virtual methods, not just abstract ones (not implementing abstract methods issues a warning).

like image 673
Johan Avatar asked Mar 08 '23 23:03

Johan


1 Answers

Look at the VMT slots of the class and compare them again the VMT slots of a base class.

In Spring4D there is this function (in Spring.VirtualClass.pas):

function IsVirtualMethodOverride(baseClass, classType: TClass; method: Pointer): Boolean;

I am not posting the entire code here because it works on some internal VMT structure record - take a look at the code yourself.

FWIW: There is a small bug inside that routine that I just found which I will fix asap.

like image 179
Stefan Glienke Avatar answered Apr 19 '23 23:04

Stefan Glienke