comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: Making ada (types) "visable" in C
Date: Thu, 27 Jan 2005 23:06:36 +0000
Date: 2005-01-27T23:06:36+00:00	[thread overview]
Message-ID: <gemini.ib007000lqtu0008g.nick.roberts@acm.org> (raw)
In-Reply-To: 6acda821.0501270510.1dbfe551@posting.google.com

di98mase@hotmail.com (Sebastian) wrote:

> How shall I make specific Ada types visable in C code?

------------------------------------------------------------ ADA side:

   type State_Buff is (Ok, Failed);
   for State_Buff use (Ok => 0, Failed => 1);
   for State_Buff'Size use Interfaces.C.int'Size;

   type Baud_Type is (R9600, R19200); 
   for Baud_Type use (R9600 => 0, R19200 => 1); 
   for Baud_Type'Size use Interfaces.C.int'Size;

   type Parity_Type is (None, Odd, Even); 
   for Parity_Type use (None => 0, Odd => 1, Even => 2); 
   for Parity_Type'Size use Interfaces.C.int'Size;

   type Stop_Bit_Type is (One, Two); 
   for Stop_Bit_Type use (One => 1, Two => 2); 
   for Stop_Bit_Type'Size use Interfaces.C.int'Size;
        
   procedure Initialize (
      Baudrate  : in Baud_Type; 
      Parity    : in Parity_Type;
      Stop_Bits : in Stop_Bit_Type;
      Status    : out State_Buff);

   pragma Export(C, Initialize, "MyInitializeInC");

------------------------------------------------------------ C side:

   enum {STATEBUF_OK     = 0,
         STATEBUF_FAILED = 1} statebuf_t;

   enum {BAUD_R9600  = 0,
         BAUD_R19200 = 1} baudrate_t;

   enum {PARITY_NONE = 0,
         PARITY_ODD  = 1,
         PARITY_EVEN = 2} parity_t;

   typedef int stopbits_t; // 1 or 2

   void MyInitalizeInC( baudrate_t baudrate,
                        parity_t   parity,
                        stopbits_t stopbits,
                        statebuf_t *statebuf );

------------------------------------------------------------ end

This is untested, and interfacing is always potentially dependent on
implementation details.

In cases where a procedure has one 'out' parameter of an elementary type, it
may be more appropriate to use a function instead (although this is not
typical Ada idiom, it is typical C idiom). For example:

   function Initialize (
      Baudrate  : in Baud_Type; 
      Parity    : in Parity_Type;
      Stop_Bits : in Stop_Bit_Type) return State_Buff;

   statebuf_t MyInitalizeInC( baudrate_t baudrate,
                              parity_t   parity,
                              stopbits_t stopbits );

HTH

-- 
Nick Roberts



      parent reply	other threads:[~2005-01-27 23:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-27 13:10 Making ada (types) "visable" in C Sebastian
2005-01-27 13:36 ` Lutz Donnerhacke
2005-01-27 17:59 ` tmoran
2005-01-27 23:06 ` Nick Roberts [this message]
replies disabled

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