type conversion - Interfacing Ada enumerations and C enums -


let in c code defined:

typedef enum { a=1, b=2 } option_type;  void f(option_type option);

let have ada code:

type option_type (a, b); option_type'size use interfaces.c.int'size; option_type use (a=>1, b=>2);  x: option_type := a;

which of following code correct (accordingly rm)?

-- first code

declare    procedure f (option: option_type)       import, convention=>c, external_name=>"f"; begin    f(x); end;

or

-- second code

declare    procedure f (option: interfaces.c.unsigned)       import, convention=>c, external_name=>"f";    function conv new ada.unchecked_conversion(option_type, interfaces.c.unsigned); begin    f(conv(x)); end;

i think both first , second ada fragments correct not sure.

neither 100% correct.

in c:

typedef enum { a=1, b=2 } option_type; 

in ada:

type option_type (a, b); option_type'size use interfaces.c.int'size; option_type use (a=>1, b=>2); 

the ada code assumes c type option_type has same size c int. second snippet assumes has same representation c unsigned int.

neither assumption supported c standard.

quoting n1570 draft, section 6.7.2.2, paragraph 4:

each enumerated type shall compatible char, signed integer type, or unsigned integer type. choice of type implementation-defined, shall capable of representing values of members of enumeration.

so c type option_type narrow 1 byte or wide widest supported integer type (typically 8 bytes), , either signed or unsigned. c restricts values of enumeration constants range of type int, doesn't imply type compatible int -- or unsigned int.

if have knowledge of characteristics of particular c compiler you're using (the phrase "implementation-defined" means characteristics must documented), can rely on characteristics -- code going non-portable.

i'm not aware of portable way define ada type that's compatible given c enumeration type. (i've been away ada long time, missing something.)

the portable approach can think of write c wrapper function takes argument of specified integer type , calls f(). conversion integer type option_type handled c compiler, , wrapper exposes function argument of known type ada.

void f_wrapper(int option) {     f(option); /* conversion int option_type implicit */ } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -