comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: How do I get this to work??
Date: 1999/01/22
Date: 1999-01-22T00:00:00+00:00	[thread overview]
Message-ID: <78a1oe$5m5$1@plug.news.pipex.net> (raw)
In-Reply-To: 76tbvv$ba5$1@nntp3.uunet.ca

Dear Chris,

The solution to your problem is (as ever) terrifically simple: declare the
variable Session at the same static level as the type Session_Ptr_Type. You
do not need a Session_Ptr variable at all. For example:


package Connection_Management is

   procedure Open_Connection;
   procedure Close_Connection;

   Connection_Error: exception;

end Connection_Management;


with Interfaces.C;
package body Connection_Management is

   type Session_Type is
      record
         Status : Interfaces.C.int := 0;
      end record;

   type Session_Ptr_Type is access all Session_Type;
   pragma Convention(C,Session_Ptr_Type);

   The_Session: aliased Session_Type;
   pragma Volatile(The_Session);

   function Open_Port (Session: in Session_Ptr_Type) return
Interfaces.C.int;
   pragma Import(C,Open_Port,"OpenPort");

   function Close_Port (Session: in Session_Ptr_Type) return
Interfaces.C.int;
   pragma Import(C,Close_Port,"ClosePort");

   procedure Open_Connection is
   begin
      if Open_Port(The_Session'Access) /= 0 then
         raise Connection_Error;
      end if;
   end Open_Connection;

   procedure Close_Connection is
   begin
      if Close_Port(The_Session'Access) /= 0 then
         raise Connection_Error;
      end if;
   end Close_Connection;

end Connection_Management;


For clarity, I have renamed Session as The_Session.

Of course, all this is to illustrate the declaration and use of a C pointer
type in Ada. In reality, the body of the above package would be programmed
as follows:


with Interfaces.C;
package body Connection_Management is

   type Session_Type is
      record
         Status : Interfaces.C.int := 0;
      end record;

   The_Session: Session_Type;
   pragma Volatile(The_Session);

   function Open_Port (Session: in out Session_Type) return
Interfaces.C.int;
   pragma Import(C,Open_Port,"OpenPort");

   function Close_Port (Session: in out Session_Type) return
Interfaces.C.int;
   pragma Import(C,Close_Port,"ClosePort");

   procedure Open_Connection is
   begin
      if Open_Port(The_Session) /= 0 then
         raise Connection_Error;
      end if;
   end Open_Connection;

   procedure Close_Connection is
   begin
      if Close_Port(The_Session) /= 0 then
         raise Connection_Error;
      end if;
   end Close_Connection;

end Connection_Management;


Ada automatically converts between an 'in out' parameter and the appropriate
C pointer type. Don't forget the Volatile pragma, or your compiler is
entitled to do something brown and sticky to you.

--
Nick Roberts
2104: Witty remark not found - try again later

Chris Warwick wrote in message <76tbvv$ba5$1@nntp3.uunet.ca>...
|Sorry, perhaps I should have been a little more specific...
|
|Chris Warwick wrote in message <76s0dp$1v4$1@nntp3.uunet.ca>...
|>   package Ada_Front_End is
|>
|>      type Session_Type is
|>         record
|>            Status : integer := 0;
|>         end record;
|>
|>      type Session_Ptr_Type is access Session_Type;
|>
|>      function Close_Port (Session : in Session_Ptr_Type) return integer;
|>   end Ada_Front_End;
|>
|>   procedure Close_Connection is
|>      Session : aliased constant Ada_Front_End.Session_Type;
|>      Session_Ptr : Session_Ptr_Type := Session'Access;
|
|It barfs here, because Session_Ptr_Type has greater scope than Session. My
|problem is Close_Port needs a pointer to a Session, so I have to define the
|Session_Ptr_Type at the highest level...
|
|>   begin
|>      Ada_Front_End.Close_Port
|>            (Session => Session_Ptr);
|>   end Close_Connection;









  parent reply	other threads:[~1999-01-22  0:00 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-04  0:00 How do I get this to work?? Chris Warwick
1999-01-05  0:00 ` Chris Warwick
1999-01-05  0:00   ` Stephen Leake
1999-01-05  0:00     ` Tom Moran
1999-01-06  0:00       ` robert_dewar
1999-01-06  0:00         ` Larry Kilgallen
1999-01-06  0:00           ` Tom Moran
1999-01-06  0:00         ` Tom Moran
1999-01-07  0:00           ` robert_dewar
1999-01-07  0:00             ` Tom Moran
1999-01-10  0:00               ` robert_dewar
1999-01-10  0:00                 ` Tom Moran
1999-01-10  0:00                   ` robert_dewar
1999-01-10  0:00                     ` Pat Rogers
1999-01-10  0:00                     ` Tom Moran
1999-01-10  0:00                   ` robert_dewar
1999-01-06  0:00         ` Tom Moran
1999-01-07  0:00           ` robert_dewar
1999-01-07  0:00             ` Tom Moran
1999-01-10  0:00               ` robert_dewar
1999-01-06  0:00     ` Chris Warwick
1999-01-06  0:00       ` Tom Moran
1999-01-07  0:00       ` Stephen Leake
1999-01-08  0:00         ` Simon Wright
1999-01-07  0:00       ` robert_dewar
1999-01-09  0:00         ` Chris Warwick
1999-01-09  0:00           ` Simon Wright
1999-01-10  0:00             ` robert_dewar
1999-01-11  0:00               ` Simon Wright
1999-01-16  0:00               ` Chris Warwick
1999-01-16  0:00                 ` Matthew Heaney
1999-01-16  0:00                   ` robert_dewar
1999-01-18  0:00                   ` Chris Warwick
1999-01-18  0:00                     ` Matthew Heaney
1999-01-18  0:00                     ` robert_dewar
1999-01-18  0:00                       ` Tucker Taft
1999-01-19  0:00                         ` Chris Warwick
1999-01-19  0:00                           ` Tom Moran
1999-01-19  0:00                           ` Stephen Leake
1999-01-19  0:00                           ` robert_dewar
1999-01-20  0:00                             ` Jeff Carter
1999-01-20  0:00                               ` robert_dewar
1999-01-21  0:00                                 ` Chris Warwick
1999-01-18  0:00                       ` dennison
1999-01-18  0:00                     ` dennison
1999-01-16  0:00                 ` Simon Wright
1999-01-16  0:00                 ` robert_dewar
1999-01-18  0:00                   ` Chris Warwick
1999-01-18  0:00                     ` Matthew Heaney
1999-01-09  0:00           ` Brian Rogoff
1999-01-10  0:00             ` Matthew Heaney
1999-01-06  0:00     ` robert_dewar
1999-01-06  0:00   ` Simon Wright
1999-01-22  0:00   ` Nick Roberts [this message]
1999-01-05  0:00 ` Tom Moran
replies disabled

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