comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@cs.nyu.edu (Robert Dewar)
Subject: Re: C Interface question
Date: 1996/07/09
Date: 1996-07-09T00:00:00+00:00	[thread overview]
Message-ID: <dewar.836955108@schonberg> (raw)
In-Reply-To: DuA7BB.21@jvdsys.nextjk.stuyts.nl


Jerry asks

        int *function(int value, int *result);

  the function is called with a value and both the returned pointer
  and the result pointer contain data.

  So how do I interface to this ?

  If I use a procedure I lose the returned value and if I use a procedure
  I lose the result value.

There are two obvious answers. You can pass an access to integer value
as the second parameter, defining an access type. Or, if the pointer
will never be null, then you can use a parameter that is access integer
on the Ada side.

  Of course I could write a interface function in C, but then I might as
  well drop Ada althogether and write the whole package in C... :-(

Seems a bit extreme ... but anyway, there is a trivially easy way to
interface as above. Note that indeed passing an access type by value is
in any case closest to the C semantics, which does not have call by
reference.

Note that in general the issue of passing out and in-out parameters to
externally interfaced functions can be a bit irritating. For some reason
people would not go along with the suggestion of allowing out and in out
parameters on functions, even with the concession of restricting it to
imported subporgrams.

Dec has a pragma Import_Valued_Procedure that allows a function in the
external world to be treated as a procedure with an initial out parameter
that is the returned value on the Ada side. Using this pragma, we would
have a third way of interfacing:

   procedure C_Function
     (Return_Val : out int; Value : int; Result : in out int);

   pragma Import (C, C_Function);
   pragma Import_Valued_Procedure
     (Internal  => C_Function,
      External  => "function",
      Mechanism => (value, value, reference));

This pragma (and all other DEC Ada pragmas and attributes) will be fully
implemented in the next version of GNAT (some of them only make sense on
VMS, but many of them, like the above example, are generally useful).
  




  parent reply	other threads:[~1996-07-09  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-09  0:00 C Interface question Jerry van Dijk
1996-07-09  0:00 ` Laurent Guerby
1996-07-11  0:00   ` Jerry van Dijk
1996-07-16  0:00     ` David Kirkland
1996-07-09  0:00 ` Robert Dewar [this message]
1996-07-10  0:00   ` Theodore E. Dennison
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00   ` Ken Garlington
1996-07-11  0:00     ` Robert Dewar
1996-07-11  0:00   ` Jerry van Dijk
1996-07-11  0:00 ` Kevin Krieser
1996-07-11  0:00   ` Robert Dewar
replies disabled

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