Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-05

How to know if a class implements a method

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Joao Morais

Hello,

Any idea how I know if a class, in a class pointer, overrides a virtual method? Eg:

tfooclass = class of tfoo;
tfoo = class
   procedure sample; virtual;
end;
tboo1 = class(tfoo)
end;
tboo2 = class(tfoo)
   procedure sample; override;
end;

...

vfooclass := tboo1;
// vfooclass doesn't implement sample.
vfooclass := tboo2;
// vfooclass implements sample.

At this moment I know that:

1. I could cast a method pointer with tmethod if sample was a class method;

2. I could cast this same method pointer if I had an instance of vfooclass.

But is there another way beyond creating an instance?

Thanks.

Joao Morais

Reply by email

Matt Emson
to Joao Morais
Any idea how I know if a class, in a class pointer, overrides a virtual method? Eg:

<snip>

Show quoted text 5 lines
vfooclass := tboo1; // vfooclass doesn't implement sample. vfooclass := tboo2; // vfooclass implements sample.

You need to implement a virtual method, even if it does nothing. Are you sure you're not meaning abstract method?

e.g.

TBlah = class
  public
     procedure blah; virtual;
     procedure blahblah; virtual; abstract;
 end;

Will only compile if "TBlah.blah" has a body.... where as TBlah.blahblah" does not require itself to be implemnted.

At this moment I know that: 1. I could cast a method pointer with tmethod if sample was a class
method;
2. I could cast this same method pointer if I had an instance of

vfooclass.

But is there another way beyond creating an instance?

You can always hack the RTTI/VMT, but that might not be a safe way.

Might it be that you need to have a more complex inheritence tree? So add a few levels?

tfooBaseclass = class of tfooBase;
tfooclass = class of tfoo;
tfooBase = class
private
  procedure sample; virtual;
end;
tfoo = class(tfooBase)
public
  procedure sample; override; //a base implementation
end;
tboo1 = class(tfooBase)
   //sample is still private and not publicly accessable
end;
tboo2 = class(tfoo)
public
   procedure sample; override; //whatever
end;
if (x is tfoo ) then {supports the sample};
if (x is tfooBase ) then {does not make public the sample};

You then know that tfooBaseclass never implement sample, but that all tfooClass have that method. It all seems a little confusing though. I'm not really clear what you are trying to achieve. Inheritence wants you to either make methods static, or virtual. If it is static, you can't override it, only "hide" (Replace) it.

You coule also use interfaces. Then you don't need a base class:

ifoo = interface
private
  procedure sample; virtual;
end;
tboo1 = class( ifoo)
   procedure sample;
end;
tboo2 = class
end;
if (tboo is ifoo) then {supports the ifoo interface};

Reply by email

Joao Morais
to Matt Emson

Hello Matt, thanks for your fast input.

Matt Emson wrote 13 lines
> Any idea how I know if a class, in a class pointer, overrides a virtual > method? Eg: <snip> > vfooclass := tboo1; > // vfooclass doesn't implement sample. > > vfooclass := tboo2; > // vfooclass implements sample. You need to implement a virtual method, even if it does nothing. Are you sure you're not meaning abstract method?

No, no... I just want to know if the class referenced by vfooclass overrides the "sample" method, implementing something different from the virtual implementation that, btw, isn't abstract and it's empty.

Show quoted text 9 lines
> At this moment I know that: > > 1. I could cast a method pointer with tmethod if sample was a class method; > 2. I could cast this same method pointer if I had an instance of vfooclass. > But is there another way beyond creating an instance? You can always hack the RTTI/VMT, but that might not be a safe way.

I though about this, dunno it's better than creating an useless instance.

Might it be that you need to have a more complex inheritence tree? So add a few levels?

Even more complex? No, no! =)

This is a piece of an OPF framework. I want to know if a business class implement something in a virtual method. Such class belongs to the application and the framework doesn't have direct access to it. If the class doesn't implement I don't need to retrieve an instance from the database, btw an expensive task for absolutely nothing.

So, at this moment I know that I can:

1. Use a safe, magic and efficient way, but I need to know if it exists;
2. Create a "light" instance using vfooclass.NewInstance method;
3. hack the vmt.

Which approach do you use?

Thanks.

Joao Morais

Reply by email

Matt Emson
to Joao Morais
Show quoted text 7 lines
So, at this moment I know that I can: 1. Use a safe, magic and efficient way, but I need to know if it exists; 2. Create a "light" instance using vfooclass.NewInstance method; 3. hack the vmt. Which approach do you use?

I have worked with a bespoke OPF a few years ago. The way we got around this was to use a multitier approach and literally have the client ask the server questions in this kind of situation. The server was multithreaded and it cached information, so it usually didn't have much of an impact. So long as the network traffic was minimal and only happened once per session (the reply being cached) it worked well.

We also used a complex inheritance tree with interfaces being passed back from the server rather than class references. There were also tight rules about implementing methods. Methods with no implementation were not allowed in persistently stored objects.

I don't think there's much more really you can do. Other than caching the RTTI info for each class you use in this way.

HTH

Reply by email