On Tuesday 30 August 2005 10:31, Michael Van Canneyt wrote:
> On Mon, 29 Aug 2005, Chris Gordon-Smith wrote:
> > Hullo All
> >
> > This is to announce the release of version 0.2 of my SimSoup Artificial
> > Chemistry simulator program.
> >
> > As far as I am aware, this is the first program that combines use of the
> > Lazarus Component Library (LCL) for the presentation of the user
> > interface with C++ for other program logic.
> >
> > The integration of C++ with Lazarus / FPC provides a programming
> > environment similar to that of C++ Builder (version 0.1 of the program
> > was developed with C++ Builder).
> >
> > For screenshots see:-
> >  http://www.simsoup.info/SimSoup_Screenshot.html
> >
> > I will welcome any feedback at the email address mentioned on my Home
> > page (see below).
>
> Looks very nice indeed :-)
>
> I think it would be a help for everybody if you could add some pages
> to the Lazarus-ccr wiki about the C++ integration. This way more people
> will benefit from your knowledge/efforts, it seems to me.
>
> Michael.

Thanks. I'll try to put some documentation / notes together. In the meantime,
here is a summary:-

1)    Calling Mechanism

The C++ code has to be able to call Free Pascal code, and vice versa.

This is achieved in the C++ code by declaring functions as:-

extern "C"

This applies both in the case of functions that are implemented in C++ (ie
those that are called from Free Pascal) and functions that are implemented in
Free Pascal (ie those that are called from C++).

The Free Pascal code has to have an 'exports' section mentioning each of the
Free Pascal functions / procedures that are called from C++.

2)   Low Tech Communication

Both the C++ code and the Free Pascal code are object oriented. However, the
mechanisms for calling methods of objects are not compatible. To deal with
this, all of the calls between C++ and Free Pascal take place through a 'Low
Tech Communication' layer in which information is passed using the C calling
convention. Wrapper functions are used to isolate the main part of the object
oriented application from this.

3)   VCL Emulation

In order to enable C++ Builder-like code to compile with minimum changes,
there are a set of VCL Emulation classes in the C++ code. Essentially these
provide the declarations for VCL components, thereby enabling the C++
Builder-like code to compile. The C++ code that defines the behaviour of the
C++ function simply calls the relevant Free Pascal function / procedure.

As an example, the C++ declaration of TCheckbox looks like this:-

class  TCheckBox: public TControl
{
    public:
        bool  Get_Checked();
        void  Set_Checked(bool Checked);
};

The "public TControl" part means that it inherits many of its properties from
TControl (which has a separate declaration)

Note that I have "Get_Checked()" and "Set_Checked()" functions rather than a
"Checked" property as in C++ Builder. This entails modifications to the
syntax of the C++ Builder code. I'm not sure whether it would have been
possible to avoid this. Possibly I could have done something with C++
operator overloading. In any case I don't see it as a substantial problem.
The syntactical changes required are very straightforward. It would have been
far more difficult to make the changes required to use a different widget
set.

My VCL Emulation classes only declare and implement a fairly small number of
classes and methods corresponding to those parts of the VCL that I actually
use. The basic technique is of course extendable, so that if I want to use a
Component or method that I have not used before, I just have to add it to the
VCL Emulation code.

4)   Pointer Passing

In order for the C++ code to be able to control the Lazarus GUI, it has to
have a pointer to each widget. This is achieved by having the C++ code call a
function to get a pointer to the widget. Eg, one such Free Pascal function
is:-

function Laz_SimControl_Get_Action_Repeat_CheckBox_Ptr(Laz_Form_Ptr:
TMainForm):TCheckBox; cdecl;

//  Return a pointer to the Simulation Control Action Repeat CheckBox

begin
    Result := Laz_Form_Ptr.Action_Repeat_CheckBox;
end;

Once the C++ code has the pointer, it can then treat it as a pointer to a C++
object and call VCL Emulation functions for that object. For example, if the
C++ code stores the result of the above function in CheckBox_Ptr, it can then
say:-

CheckBox_Ptr->Set_Checked(true);

to make the CheckBox checked.

5)  That's about it for now. If you want a closer look then the code is
available on the website.


--
Chris Gordon-Smith
London
http://www.simsoup.info

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to