comp.lang.ada
 help / color / mirror / Atom feed
From: Jeff Carter <jrcarter001@my-deja.com>
Subject: Re: Interfacing to C non-void procedure with "out" parameters
Date: 2000/01/14
Date: 2000-01-14T00:00:00+00:00	[thread overview]
Message-ID: <85nqaf$95o$1@nnrp1.deja.com> (raw)
In-Reply-To: 85ngl3$10s$1@nnrp1.deja.com

In article <85ngl3$10s$1@nnrp1.deja.com>,
  Ted Dennison <dennison@telepath.com> wrote:
> In article <387f1c30.12554815@news.rdg.ac.uk>,
> For instance, suppose we have the following (made up) C routine:
>
>    int esGetValue (int key, const int *value);
>
> The idea being that you pass in a key and get back a value for it. The
> function returns 1 on success, 0 on failure.
>
> On the Ada side, we'd much prefer to access this routine thusly:
>
>    Get_Failed : exception; -- raised by Get_Value when the get fails
>
>    function Get_Value (Key : Integer) return Integer;
>
> First you interface to the routine to create your thin binding:
>
>    function esGetValue (Key   : in Interfaces.C.Int;
>                         Value : in System.Address
>                        ) return Interfaces.C.Int;
>    pragma Import (C, esGetValue, "esGetValue");
>
> Note that for the "int *" parameter I used System.Address. This is not
> officially portable, but in practice works on every normal platform.
You
> could instead use a type instantiated from Interfaces.C.Pointers for
int
> to achieve theoretical portability.

There's no reason to use System.Address here. Use an access parameter:

function Es_Get_Value
   (Key : Interfaces.C.Int; Value : access Interfaces.C.Int)
return Interfaces.C.Int;
pragma Import (C, ...);

>
> Finally we create our thick binding with the desired interface.
>
>    function Get_Value (Key : Integer) return Integer is
>       Value : aliased Interfaces.C.Int;
>       use type Interfaces.C.Int; -- Give access to "=" operator
>    begin
>       if
>         esGetValue
>           (Key   => Interfaces.C.Int(Key),
>            Value => Value
>           ) = 0
>       then
>          raise Get_Failed;
>       end if;
>       return Integer(Value);
>    end Get_Value;

Note that the call here should not compile; Value is not of type
System.Address. With an access parameter, use Value => Value'access and
everything should be fine.
--
Jeff Carter
"Now go away or I shall taunt you a second time."
-- Monty Python and the Holy Grail


Sent via Deja.com http://www.deja.com/
Before you buy.




  reply	other threads:[~2000-01-14  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-01-14  0:00 Interfacing to C non-void procedure with "out" parameters Dr Steve Sangwine
2000-01-14  0:00 ` Alain Le Guennec
2000-01-14  0:00 ` Ted Dennison
2000-01-14  0:00   ` Jeff Carter [this message]
2000-01-14  0:00     ` Ted Dennison
2000-01-15  0:00       ` Jeff Carter
2000-01-17  0:00 ` Matthew Heaney
2000-01-18  0:00   ` Dr Steve Sangwine
replies disabled

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