comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: linking to C++
Date: 4 Jun 2003 07:09:14 -0700
Date: 2003-06-04T14:09:14+00:00	[thread overview]
Message-ID: <1ec946d1.0306040609.60873482@posting.google.com> (raw)
In-Reply-To: 3eddb5dd$1@baen1673807.greenlnk.net

"Anthony Moss" <a.m@baesystems.com> wrote in message news:<3eddb5dd$1@baen1673807.greenlnk.net>...
> Is there any good documentation on calling C++ functions from a piece of ADA
> code.
> please repond
> anthony.moss@baesystems.com
> 
> Thank you.

I posted an answer to a similar question in the thread "Rational Apex
Ada - Pragma interface with C++", which occured in Oct 2002.

<http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=uqbtbu6rtkk7b%40corp.supernews.com>

I got the idea from James Coplien's book Adanced C++ Programming
Styles and Idioms.  The basic idea is to bind to a factory function
written in C++, and then manipulate the object from the Ada side, e.g.

class X
{
public:
   void f();
};

extern "C" X* make_x()
{
   return new (std::nothrow) X();
}

extern "C" void free_x(X* p)
{
   delete p;
}

extern "C" void X_f(X* x)
{
   x->f();
}

You'll need to write a binding on the Ada side, e.g.

package X_Types is

   type X (<>) is limited private;

   type X_Access is access all X;
   pragma Convention (C, X_Access);
   for X_Access'Storage_Size use 0;

   function New_X return X_Access;

   procedure Free (X : in out X_Access);

   procedure F (X : X_Access);
   --or procedure F (X : access X);

private

   type X is limited null record;

end X_Types;

Implement New_X, Free, and F by calling the C++ functions, which have
been imported to the Ada program:

package body X_Types is

   function C_New_X return X_Access;
   pragma Convention (C, C_New_X);
   pragma Import (C_New_X, "new_x");  //something like that

   function New_X return X_Access is
   begin
      return C_New_X;
   end;

   ...
end X_Types;

Make sure that access types are passed by *value* to the C++ function.



      parent reply	other threads:[~2003-06-04 14:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-04  9:05 linking to C++ Anthony Moss
2003-06-04 12:10 ` Larry Kilgallen
2003-06-04 14:09 ` Matthew Heaney [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox