From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,6343ad227ef07794 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nick Roberts Newsgroups: comp.lang.ada Subject: Re: Making ada (types) "visable" in C Date: Thu, 27 Jan 2005 23:06:36 +0000 Message-ID: References: <6acda821.0501270510.1dbfe551@posting.google.com> Content-Type: text/plain; charset=us-ascii X-Trace: individual.net UuWBdMzkynomB4cXRLUVtgusW2FjPCvDQ2oCi5yToWyaL9LAg= X-Orig-Path: not-for-mail User-Agent: Gemini/1.45d (Qt/3.3.2) (Windows-XP) Xref: g2news1.google.com comp.lang.ada:8031 Date: 2005-01-27T23:06:36+00:00 List-Id: 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