comp.lang.ada
 help / color / mirror / Atom feed
* Making ada (types) "visable" in C
@ 2005-01-27 13:10 Sebastian
  2005-01-27 13:36 ` Lutz Donnerhacke
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sebastian @ 2005-01-27 13:10 UTC (permalink / raw)


Hi,

How shall I make specific Ada types visable in C code. E.g. If I want
to call an Ada procedure from C that has own type defintions how do I
do? In this example I want to call an Ada procedure that is called
Initialize from
a c-file/function. The Ada procedure has own types how shall I
"convert" them to C-types?

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

   type State_Buff is (Ok, Failed);
 
   type Baud_Type is (R9600, R19200); 

   type Parity_Type is (None, Odd, Even); 

   type Stop_Bit_Type is (One, Two); 
        
   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:

?



------------------------------------------------------------
Regards

di98



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making ada (types) "visable" in C
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Lutz Donnerhacke @ 2005-01-27 13:36 UTC (permalink / raw)


* Sebastian wrote:
> ------------------------------------------------------------
> ADA side:
>
>    type State_Buff is (Ok, Failed);
>  
>    type Baud_Type is (R9600, R19200); 
>
>    type Parity_Type is (None, Odd, Even); 
>
>    type Stop_Bit_Type is (One, Two); 
>         
>    Initialize (
>       Baudrate  : in Baud_Type; 
>       Parity    : in Parity_Type;
>       Stop_Bits : in Stop_Bit_Type;
>       Status    : out State_Buff);
>
>    pragma Export(C, Initialize, "MyInitializeInC");

     pragma Export(C, Ok, "state_ok");
     pragma Export(C, Failed, "state_failed");
     ...
     pragma Export(C, Two, "stop_bit_two");

> ------------------------------------------------------------

> C side:

extern void MyInitializeInC (int baudrate, int parity, int stop_bits,
       	    		     int * state);

BTW:
    function Initialize (Baudrate ...) return State_Buff;
becomes
    extern int MyInitializeInC (int baudrate, int parity, int stop_bits);



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making ada (types) "visable" in C
  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
  2 siblings, 0 replies; 4+ messages in thread
From: tmoran @ 2005-01-27 17:59 UTC (permalink / raw)


>  type State_Buff is (Ok, Failed);
>
>  type Baud_Type is (R9600, R19200);
>
>  type Parity_Type is (None, Odd, Even);
>
>  type Stop_Bit_Type is (One, Two);

  Ada deals with things at the logical level, eg, a State_Buf object
either has the value Ok or the value Failed.  C deals with bit patterns,
and different C compilers running on different OSes or hardware use
different bit patterns.  Ada provides a way, "representation clauses",
to make the compiler use specific bit patterns for its logical types.
So, assuming that for your specific application the C bit pattern
for a State_Buf object is 32 bits with a value of 0 or 0xFFFFFFFF, you
would say
   type State_Buff is (Ok, Failed);
   for State_Buff use (Ok => 0, Failed => 16#FFFFFFFF#);
   for State_Buff'size use 32;
Similarly for the other types.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making ada (types) "visable" in C
  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
  2 siblings, 0 replies; 4+ messages in thread
From: Nick Roberts @ 2005-01-27 23:06 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-01-27 23:06 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox