Board index » kylix » Migration to Lazarus

Migration to Lazarus


2004-05-23 11:14:25 PM
kylix1
I am currently porting my C++ Builder / Kylix (C++) artificial chemistry
simulator to Lazarus. This entails developing techniques for integrating
C++ code with the Free Pascal based Lazarus (LCL) library that provides
features similar to VCL.
A while ago I posted details of the proof of concept for the C++/Lazarus
integration on my website. I have now updated the website with a screenshot
of the first screen from the simulator, which has been ported to use
Lazarus. The logic for the screen is coded in C++, with Lazarus being used
for the GUI display.
The URL for the C++/Lazarus Integration page is
graffiti.{*word*269}.net/c.gordon-smith/SimSoup/Lazarus_Integration.html
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 
 

Re:Migration to Lazarus

Readers of this newsgroup may be interested in my posting in
borland.public.kylix.non-technical on the above topic
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

Re:Migration to Lazarus

Not bad !
"Chris Gordon-Smith" < XXXX@XXXXX.COM >schrieb im Newsbeitrag
Quote
I am currently porting my C++ Builder / Kylix (C++) artificial chemistry
simulator to Lazarus. This entails developing techniques for integrating
C++ code with the Free Pascal based Lazarus (LCL) library that provides
features similar to VCL.

A while ago I posted details of the proof of concept for the C++/Lazarus
integration on my website. I have now updated the website with a
screenshot
of the first screen from the simulator, which has been ported to use
Lazarus. The logic for the screen is coded in C++, with Lazarus being used
for the GUI display.

The URL for the C++/Lazarus Integration page is

graffiti.{*word*269}.net/c.gordon-smith/SimSoup/Lazarus_Integration.html
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

{smallsort}

Re:Migration to Lazarus

Not bad. However one of the beauties of BCB is that it works with
Pascal-objects and although the VCL is written in pascal you actual don't
need write any Pascal but can code your GUI handling in C++. A thing I guess
is only possible because Borland's Pascal and c++ share a common memory
model for objects.
In other words it is definitely a workable solution, as long as you write
your GUI handling in Pascal and use C++ for computation functions.
/Palle
Ps. I am not a linux user (yet), however we have been looking at lazarus and
coming from BCB i would also like som C++ interaction.
"Chris Gordon-Smith" < XXXX@XXXXX.COM >skrev i en meddelelse
Quote
I am currently porting my C++ Builder / Kylix (C++) artificial chemistry
simulator to Lazarus. This entails developing techniques for integrating
C++ code with the Free Pascal based Lazarus (LCL) library that provides
features similar to VCL.

A while ago I posted details of the proof of concept for the C++/Lazarus
integration on my website. I have now updated the website with a
screenshot
of the first screen from the simulator, which has been ported to use
Lazarus. The logic for the screen is coded in C++, with Lazarus being used
for the GUI display.

The URL for the C++/Lazarus Integration page is

graffiti.{*word*269}.net/c.gordon-smith/SimSoup/Lazarus_Integration.html
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

Re:Migration to Lazarus

Quote
In other words it is definitely a workable solution, as long as you write
your GUI handling in Pascal and use C++ for computation functions.
I hope at some point in time Free Pascal will share the GCC ABI, so that
linking Free Pascal / Lazarus code to C++ code compiled by GCC will be
simple.
-Michael
 

Re:Migration to Lazarus

Palle Meinert wrote:
Quote
Not bad. However one of the beauties of BCB is that it works with
Pascal-objects and although the VCL is written in pascal you actual don't
need write any Pascal but can code your GUI handling in C++. A thing I
guess is only possible because Borland's Pascal and c++ share a common
memory model for objects.



In other words it is definitely a workable solution, as long as you write
your GUI handling in Pascal and use C++ for computation functions.


The entry point for GUI handling is Pascal, but I transfer control
immediately to C++. Here is a typical Free Pascal Event Handler:-
procedure Tsimparams.OK_ButtonClick(Sender: TObject);
begin
CPP_SimParams_OK_ButtonClick(Sender); // C++ function
end;
All the logic is in CPP_SimParams_OK_ButtonClick, which is a C++ function.
Once in CPP_SimParams_OK_ButtonClick I get a pointer to a C++ object
representing the form, transfer control to its OK_ButtonClick method, and
I'm back in my original C++ Builder / Kylix (C++) code.
I only write a very small amount of Pascal code, just enough to catch the
event and pass control to C++
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

Re:Migration to Lazarus

Quote

procedure Tsimparams.OK_ButtonClick(Sender: TObject);
begin
CPP_SimParams_OK_ButtonClick(Sender); // C++ function
end;

I did not see that in your 'proof of concept'.
Quote
All the logic is in CPP_SimParams_OK_ButtonClick, which is a C++ function.
Once in CPP_SimParams_OK_ButtonClick I get a pointer to a C++ object
I would say that you get a pointer to a Pascal-object. Which I think is
problematic because you need to know the exact memory layout and mirror it
in a similar C++ definition. And even then I am not sure that the memory
layout of a object is the same among different C++ compilers and different
pascal compilers, i guess not.
Quote
representing the form, transfer control to its OK_ButtonClick method, and
I'm back in my original C++ Builder / Kylix (C++) code.

I only write a very small amount of Pascal code, just enough to catch the
event and pass control to C++
If indeed it is possible to use the same memory map of a free Pascal object
in C++, you would still have to maintain a definition of it both in C++ and
Pascal. Which i think is easily error prone and just too much work to
maintain.
I would say a more feasible solution would bee to keep the GUI part in the
Pascal form object and make a non-visual C++ object which methods could be
called when e.g. validations, computations etc. is needed.
Which bring up the next question is it even possible to call member
functions of a C++ object from Pascal. This is not even a easy task between
Delphi and BCB. Else you need wrapper functions (which again is extra work).
One of the things I like much about bcb is its ease to call Pascal-code. It
is just a shame that Borland did not make it just a simple to call C++ code
from Delphi.
/Palle
 

Re:Migration to Lazarus

Palle Meinert wrote:
Quote
>
>procedure Tsimparams.OK_ButtonClick(Sender: TObject);
>begin
>CPP_SimParams_OK_ButtonClick(Sender); // C++ function
>end;
>
I did not see that in your 'proof of concept'.

Its not in the original proof of concept, its part of the Simulation
Parameters screen logic that I have ported since.
Quote
>All the logic is in CPP_SimParams_OK_ButtonClick, which is a C++
>function. Once in CPP_SimParams_OK_ButtonClick I get a pointer to a C++
>object

I would say that you get a pointer to a Pascal-object. Which I think is
problematic because you need to know the exact memory layout and mirror it
in a similar C++ definition. And even then I am not sure that the memory
layout of a object is the same among different C++ compilers and different
pascal compilers, i guess not.

In this particular case, its a pointer to a C++ object. This is the object
that in my BCB / Kylix (C+) code inherited from TForm. It includes the data
and function methods that are specific to my Simulation Parameters form.
There are cases when I get a pointer to a Pascal object. For example, when
an event happens in Lazarus, I call a C++ event handler (using the non
object oriented C calling interface), passing the pointer to the Pascal
object that generated the event ("Sender") as an argument. I can then call
a function such as the following:-
char* TEdit::Get_Text(TEdit* TEdit_Ptr)
// Get the text associated with the specified Lazarus TEdit object
{
return Laz_Edit_Get_Text(TEdit_Ptr);
}
This is a C++ method that calls the Pascal function Laz_Edit_Get_Text. This
function then returns the value of the Text property for the Pascal TEdit
object pointed to by TEdit_Ptr.
As you can probably see, I am building up classes in my C++ code that my C++
code can use in the same way that it used to use the VCL classes. The
difference is that these 'emulator' classes work by invoking Lazarus
functions.
Here is the declaration (so far) of my TEdit emulator class.
class TEdit: public TControl
{
public:
static char* Get_Text(TEdit* TEdit_Ptr);
static void Set_Text(TEdit* TEdit_Ptr, const char* Text);
};
There is no question of me having to know the exact memory layout of the
Pascal objects. I think that this would be very difficult to get working,
and there would be no guarantee that the layout would not change with a new
version of Lazarus.
Instead, I am just providing my C++ code with a set of classes that emulate
the VCL classes by calling methods of LCL classes (LCL being the Lazarus
'clone' of VCL).
Quote
>representing the form, transfer control to its OK_ButtonClick method, and
>I'm back in my original C++ Builder / Kylix (C++) code.
>
>I only write a very small amount of Pascal code, just enough to catch the
>event and pass control to C++

If indeed it is possible to use the same memory map of a free Pascal
object in C++, you would still have to maintain a definition of it both in
C++ and Pascal. Which i think is easily error prone and just too much work
to maintain.
Yes - it would be.
Quote

I would say a more feasible solution would bee to keep the GUI part in the
Pascal form object and make a non-visual C++ object which methods could be
called when e.g. validations, computations etc. is needed.

Which bring up the next question is it even possible to call member
functions of a C++ object from Pascal.
It may be, but I have not figured out how to do it directly. Instead, I use
a 'low tech' C calling interface, passing pointers to objects. Once I have
crossed the C++ / Lazarus boundary, I then use the pointer to invoke a
method on the object to which it points.
Quote
This is not even a easy task
between Delphi and BCB. Else you need wrapper functions (which again is
extra work).

What I am doing can be described as 'wrapper functions'. It is indeed extra
work, but I think its viable, on my project at any rate.
I don't claim that this is an elegant solution. However, it is workable. I
want my project to have a long term future, and I don't see BCB or
Kylix (C++) as a safe bet for the future.
Quote
One of the things I like much about bcb is its ease to call Pascal-code.
It is just a shame that Borland did not make it just a simple to call C++
code from Delphi.

/Palle
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

Re:Migration to Lazarus

On Tue, 25 May 2004 18:44:22 +0100, Chris Gordon-Smith
< XXXX@XXXXX.COM >wrote:
Quote

I only write a very small amount of Pascal code, just enough to catch the
event and pass control to C++
First, kudos for the cool project and work you've done so far.
I'm wondering what is the point, though:
a) to create a system that you can migrate existing projects to; or
b) to create a BCB-like system for new development; or
c) something else entirely.
If (a), why not just use BCB for maintenance?
If (b), why not, assuming lazarus/fpc gives you the development
environment you want, just use pascal? I know people have language
preference but jumping though a lot of hoops just to satisfy a
preference seems like banging your head against a wall for little
benefit.
 

Re:Migration to Lazarus

ckd wrote:
Quote
On Tue, 25 May 2004 18:44:22 +0100, Chris Gordon-Smith
< XXXX@XXXXX.COM >wrote:

>
>I only write a very small amount of Pascal code, just enough to catch the
>event and pass control to C++

First, kudos for the cool project and work you've done so far.

I'm wondering what is the point, though:

a) to create a system that you can migrate existing projects to; or

b) to create a BCB-like system for new development; or

c) something else entirely.

If (a), why not just use BCB for maintenance?

If (b), why not, assuming lazarus/fpc gives you the development
environment you want, just use pascal? I know people have language
preference but jumping though a lot of hoops just to satisfy a
preference seems like banging your head against a wall for little
benefit.
I have an existing project (SimSoup - an artificial chemistry simulator)
that I expect to continue developing in the coming months and years. Its a
spare time project, which to some degree accounts for its long timescale.
I worked out a C++ / Lazarus integration technique so that I can migrate
SimSoup to a gcc / Lazarus environment. SimSoup currently compiles under
both BCB and Kylix (C++). My main platform is Linux, but ideally I would
like SimSoup to continue to run on Windows.
As you say, I could continue to use BCB, and I could also continue with
Kylix on Linux. However, Kylix looks like a discontinued product, and can
already be difficult to get working on current distributions. I had to
install a patch to get it to work on my SuSE 9.0 setup. BCB also looks like
a product that is being dropped, although there have been some rumours
recently about a new version.
In short, SimSoup is a long term project which I expect to continue
developing in the years ahead, and I don't see Borland as a safe bet for a
future development environment.
The gcc compiler undoubtedly has a long term future, and I think that
Lazarus also has good prospects. Even if Lazarus development slows or
stops, it sits on top of gtk and is open source and so I would have some
control over it.
If SimSoup was a completed project, then I could use BCB / Kylix for
maintenance as you say, but I wouldn't want to do this for long term
development.
Moving entirely to Pascal is not really viable when I already have a lot of
C++ code (and anyway I like C++).
A secondary motivation for developing the C++ / Lazarus integration
technique was also just out of curiosity to see whether it was possible!
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page
 

Re:Migration to Lazarus

Quote
As you can probably see, I am building up classes in my C++ code that my
C++
code can use in the same way that it used to use the VCL classes. The
difference is that these 'emulator' classes work by invoking Lazarus
functions.
That will be a load of wrapper functions and even though there is not very
many components for Lazarus (yet) I think for your approach to be viable,
you should make a program which would auto-generate all these wrapper
functions. And while you are at it, you should also write a program which
would mirror a Lazarus form object into the corresponding C++ object. If
possible the last translation should be performed during compilation.
If you can manage these tasks and a find a consistent naming standard I
think your approach could become interesting. However it will still be just
a kind of emulation. What about inheritance and other OOP idioms, which will
difficult to emulate with low-tech c-function calls.
I still think the easiest way to combine Lazarus with C++ is to keep all the
GUI handling in Pascal and just invoke C++ routines when needed.
/Palle
 

Re:Migration to Lazarus

Palle Meinert wrote:
Quote
>As you can probably see, I am building up classes in my C++ code that my
C++
>code can use in the same way that it used to use the VCL classes. The
>difference is that these 'emulator' classes work by invoking Lazarus
>functions.

That will be a load of wrapper functions and even though there is not very
many components for Lazarus (yet) I think for your approach to be viable,
you should make a program which would auto-generate all these wrapper
functions. And while you are at it, you should also write a program which
would mirror a Lazarus form object into the corresponding C++ object.
That would be nice. My main priority however is just to get my own project
running, rather than build a complete development environment. Perhaps
someone would like to take it to the next stage.
Quote
possible the last translation should be performed during compilation.



If you can manage these tasks and a find a consistent naming standard I
think your approach could become interesting. However it will still be
just a kind of emulation. What about inheritance and other OOP idioms,
which will difficult to emulate with low-tech c-function calls.

My C++ code can (and does) still have inheritance. The low tech C function
calls are merely a communication channel from C++ code to Pascal code. All
the real work is done in C++ code which is fully object oriented.
Quote


I still think the easiest way to combine Lazarus with C++ is to keep all
the GUI handling in Pascal and just invoke C++ routines when needed.



/Palle
--
Chris Gordon-Smith
London
Homepage: graffiti.{*word*269}.net/c.gordon-smith/
Email Address: Please see my Home Page