comp.lang.ada
 help / color / mirror / Atom feed
* linking to C++
@ 2003-06-04  9:05 Anthony Moss
  2003-06-04 12:10 ` Larry Kilgallen
  2003-06-04 14:09 ` Matthew Heaney
  0 siblings, 2 replies; 3+ messages in thread
From: Anthony Moss @ 2003-06-04  9:05 UTC (permalink / raw)


Is there any good documentation on calling C++ functions from a piece of ADA
code.
please repond
anthony.moss@baesystems.com

Thank you.





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: linking to C++
  2003-06-04  9:05 linking to C++ Anthony Moss
@ 2003-06-04 12:10 ` Larry Kilgallen
  2003-06-04 14:09 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Larry Kilgallen @ 2003-06-04 12:10 UTC (permalink / raw)


In article <3eddb5dd$1@baen1673807.greenlnk.net>, "Anthony Moss" <a.m@baesystems.com> writes:

> Is there any good documentation on calling C++ functions from a piece of ADA
> code.

It is not part of the Ada standard, so the answer will be specific to
the operating system and compiler you are using.

If looking at your compiler documentation does not help, you could
reveal information about your environment here to get advice.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: linking to C++
  2003-06-04  9:05 linking to C++ Anthony Moss
  2003-06-04 12:10 ` Larry Kilgallen
@ 2003-06-04 14:09 ` Matthew Heaney
  1 sibling, 0 replies; 3+ messages in thread
From: Matthew Heaney @ 2003-06-04 14:09 UTC (permalink / raw)


"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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-06-04 14:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox