comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: generic imports?  void* -> generics?
Date: Fri, 19 Sep 2003 22:40:01 GMT
Date: 2003-09-19T22:40:01+00:00	[thread overview]
Message-ID: <uwuc4mmeo.fsf@earthlink.net> (raw)
In-Reply-To: FBIab.3309$DM5.35123@newsfep4-glfd.server.ntli.net

chris <spamoff.danx@ntlworld.com> writes:

> void *lua_touserdata (Lua_State* L, int pos)

I prefer thin bindings, so you just do it this way:

   function lua_touserdata
     (L   : Lua_State_Access;
      Pos : C.int) return System.Address;

   pragma Import (C, lua_touserdata);
   pragma Convention (C, lua_touserdata);

There's no binding to C type void* in Interfaces.C, but type
System.Address should work OK.  That way you can say:

   Get_MyUserData:
   declare
      Data : MyUserData_Type;
      for Data'Address use lua_touserdata(L, Pos);
   begin
      ... -- manipulate Data as appropriate
   end Get_MyUserData;

I suppose you could wrap this in a generic, which is implemented using
the function above.


> I was thinking of something like
> 
> generic
>     type Userdata is private;
> function to_userdata (L   : in Lua_State_Access;
>                        Pos : in Index) return Userdata;
> 
> but am unsure how to complete it...  I tried
> 
>     function To_Userdata (L   : in Lua_State_Access;
>                           Pos : in Index) return Userdata is
>        function Lua_ToUserData (L   : in Lua_State_Access;
>                                 Pos : in Index) return Userdata;
>        pragma Import (C, Lua_ToUserData);
>     begin
>        return Lua_Touserdata (L, Pos);
>     end To_Userdata;
> 
> and it compiles ok, but is it right?  It looks odd to me.  Will the
> function Lua_ToUserData be imported upon instantiation of the
> To_Userdata function?


This is wrong, because your Ada function to_userdata returns an object,
not a pointer to object, as the C function does.  You need to bind to an
access type, like this:

   function To_Userdata
     (L : lua_state_access;
      Pos : Index) return Userdata is

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

      function lua_touserdata
        (L   : Lua_State_Access;
         Pos : C.int) return Userdata_Access;  --ok now

      pragma Import (C, lua_touserdata);
      pragma Convention (C, lua_touserdata);

      Result : constant Userdata_Access :=
         lua_touserdata(L, Pos);
   begin
      return Result.all;
   end;






  parent reply	other threads:[~2003-09-19 22:40 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-19 19:20 generic imports? void* -> generics? chris
2003-09-19 20:46 ` chris
2003-09-19 21:18 ` Nick Roberts
2003-09-19 23:12   ` chris
2003-09-20 16:52   ` Simon Wright
2003-09-22 21:30     ` Randy Brukardt
2003-09-23  5:45       ` Simon Wright
2003-09-23 19:07         ` Randy Brukardt
2003-09-23 20:28           ` Simon Wright
2003-09-24 18:16             ` Randy Brukardt
2003-09-19 22:40 ` Matthew Heaney [this message]
2003-09-19 23:01   ` chris
2003-09-20  1:59   ` Jeffrey Carter
2003-09-20 13:52     ` Matthew Heaney
2003-09-23 22:39 ` chris
replies disabled

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