Sign in Join

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

Mailing lists / fpc-pascal / 2012-03

class of - instance created is wrong

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

I've used the Class Of language feature a lot in the past. eg: tiOPF uses it a lot, I use it to generate various reports in our company projects etc.

So I tried to implement a "lazy mans" factory method in my project, so it can generate a class instance based on what the developer chooses.

I have the Class Of defined as follows...

  TfpgCanvasBase = class(TObject)
     ...
  end;
  TfpgCanvasBaseClass = class of TfpgCanvasBase;

I have a few Canvas class hierarchies. Here are two of them, which I'm trying to use in this example:

TfpgCanvas
|
+-- TfpgX11Canvas
|
+-- TfpgCanvasBase

TAgg2D
|
+-- TfpgCanvasBase

I then have a global variable which will point to the actual class type to instantiate.

  DefaultCanvasClass: TfpgCanvasBaseClass = nil;

Then somewhere early on in your project you can define which Canvas class to use by simply doing the following...

   DefaultCanvasClass := TfpgCanvas;
or
   DefaultCanvasClass := TAgg2D;

Here is the simple factory method that the Window class uses to instantiate a new Canvas object.
Excuse the Writeln() statements and comment - they are for debugging purposes.

function TfpgWindow.CreateCanvas: TfpgCanvasBase;
begin
  writeln('>> TfpgWindow.CreateCanvas');
  writeln('     DefaultCanvasClass = ' + DefaultCanvasClass.ClassName);
//  result := TAgg2D.Create(self);
//  result := TfpgCanvas.Create(self);
  Result := DefaultCanvasClass.Create(self);
  writeln('<< TfpgWindow.CreateCanvas');
end;

I then have Writeln() statements in each Canvas constructor outputting the name of that class. So when a TfpgCanvas instance is created, the output should be similar to the class hierarchy listed above.

Now the problem...

Using the DefaultCanvasClass to create an instance, it seems to leave out the latest class in the hierarchy. So if DefaultCanvasClass = TfpgCanvas, I see console output as follows;

creating... TfpgX11Canvas
creating... TfpgCanvasBase

NOTE the missing TfpgCanvas. Needless to say my program crashes instantly.

The same happens when DefaultCanvasClass = TAgg2D. The console output is then.

creating... TfpgCanvasBase

Again, the actual TAgg2D class constructor is by-passed somehow?? Yet the writeln() statement inside the CreateCanvas() method, which asks for the ClassName of DefaultCanvasClass, DOES output the correct class name. TfpgCanvas or TAgg2D.

Now if I change CreateCanvas() to create a instance of a Canvas class directly - without the usage of DefaultCanvasClass... One of the two commented out lines of code, then everything works fine. But obviously, I don't want to hard-code which canvas class to use.

Can anybody see what I am doing wrong? I've tested with FPC 2.6.0, 2.6.1 and 2.7.1 under 64-bit Linux. All compilers give the same results. Like I've said, I have used the Factory Method pattern many times (though a bit more advanced than what I did here - normally using a mapping class), and it always works well.

BTW:
I took this global variable idea from the fptimer implementation, which is part of FPC. I assume the fptimer implementation works correctly.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net
Mattias Gaertner
to Graeme Geldenhuys

Graeme Geldenhuys hat am 15. März 2012 um 10:07
geschrieben:

Show quoted text 28 lines
[...] TfpgCanvas | +-- TfpgX11Canvas | +-- TfpgCanvasBase TAgg2D | +-- TfpgCanvasBase [...] DefaultCanvasClass: TfpgCanvasBaseClass = nil; [...] function TfpgWindow.CreateCanvas: TfpgCanvasBase; begin writeln('>> TfpgWindow.CreateCanvas'); writeln(' DefaultCanvasClass = ' + DefaultCanvasClass.ClassName); // result := TAgg2D.Create(self); // result := TfpgCanvas.Create(self); Result := DefaultCanvasClass.Create(self); writeln('<< TfpgWindow.CreateCanvas'); end; [...] creating... TfpgX11Canvas creating... TfpgCanvasBase NOTE the missing TfpgCanvas. Needless to say my program crashes

instantly.

[...] Can anybody see what I am doing wrong?

Is the constructor virtual?

I've tested with FPC 2.6.0, 2.6.1 and 2.7.1 under 64-bit Linux. All

compilers give the same

results. [...]

Mattias

Graeme Geldenhuys
to Mattias Gaertner
Mattias Gaertner wrote
Is the constructor virtual?

I owe you a beer! :-)

The original constructor in TfpgCanvasBase was virtual, but some descendant classes did a 'reintroduce' of the constructor which wasn't virtual. Looking at the code now, the reintroduce isn't needed any more and thus removed.

It works perfectly now, thanks!! fpGUI developers can now easily switch between X11, GDI and AggPas canvas's. :-D

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net
Leonardo M. Ramé
to Graeme Geldenhuys
Show quoted text 14 lines
________________________________ From: Graeme Geldenhuys To: FPC-Pascal users discussions Sent: Thursday, March 15, 2012 7:11 AM Subject: Re: [fpc-pascal] class of - instance created is wrong On 15 March 2012 11:36, Mattias Gaertner  wrote: > > Is the constructor virtual? It works perfectly now, thanks!!  fpGUI developers can now easily switch between X11, GDI and AggPas canvas's. :-D

Hi Graeme, this is completely off topic. Does AggPas works in 64bits Linux? I remember it didn't compile last year.

Regards,
Leonardo M. Ramé
http://leonardorame.blogspot.com

Graeme Geldenhuys
to Leonardo M. Ramé
Leonardo M. Ramé wrote
Hi Graeme, this is completely off topic. Does AggPas works in 64bits Linux? I remember it didn't compile last year.

I does now! ;-)

I haven't publish my copy of AggPas yet (which contains the 64-bit fixes). I'll probably push those changes to the fpGUI repository later today or tomorrow.

I'll also supply some of the vital patches to the Lazarus community, so they can fix their AggPas copy.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net
Zaher Dirkey
to Graeme Geldenhuys

On Thu, Mar 15, 2012 at 12:52 PM, Graeme Geldenhuys <

Show quoted text 16 lines
wrote: On 15 March 2012 12:31, Leonardo M. Ramé wrote: > > > Hi Graeme, this is completely off topic. Does AggPas works in 64bits Linux? I remember it didn't compile last year. > I does now! ;-) I haven't publish my copy of AggPas yet (which contains the 64-bit fixes). I'll probably push those changes to the fpGUI repository later today or tomorrow. Would u push it to new repository (under ur account).

I'll also supply some of the vital patches to the Lazarus community,

so they can fix their AggPas copy. And why there is many of AggPas, what happened to the original one?

Best Regards
Zaher Dirkey

Graeme Geldenhuys
to Zaher Dirkey
Zaher Dirkey wrote
> Would u push it to new repository (under ur account).

I'm planning to publish it to the fpGUI repository today.

And why there is many of AggPas, what happened to the original one?

The original AggPas author only released AggPas as a zip / tarball archive. There never was any public source code repository. The original author also moved on, and created a commercial venture from AggPas, now called CrossGL (Cross platform Graphics Library), so he doesn't work on the original AggPas version any more.

Also, fpGUI can now use AggPas as the graphics library (instead of XLib or GDI) - see the screenshot in the fpgui.development newsgroup. Once fully tested, it will become the default graphics library for fpGUI, so a copy of AggPas must be included with fpGUI to compile.

As for Lazarus including a copy of AggPas - I have no idea what was the plans behind that. They will never use AggPas as a graphics library for LCL, because it is not "native". Lazarus's AggPas package is also not a default installed package in Lazarus IDE as far as I know. So it seems the thinking was to supply an optional and more powerful Canvas class, compared to the default LCL Canvas, for those developers that need it in specialised projects.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net
Graeme Geldenhuys
Zaher Dirkey wrote
But that mean you force us to use fpGUI :P

No, the AggPas code is still kept in a separate folder. The only mixing between fpGUI & AppPas is localised to a single unit, and that unit is not needed for any of AggPas specific code or demos.

Regards,   - Graeme - _______________________________________________ fpGUI - a cross-platform Free Pascal GUI toolkit http://fpgui.sourceforge.net