Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Ada Variant Record be binary compatible to a C++ union?

I am designing a communication middleware for use in an application which has a module in Ada and many modules in C++ which communicates passing parameters (scalar values) and structures. The application runs in MS Windows XP and Windows 7, the C++ part is being developed in MSVC++ 2008, the Ada part is being developed using GPS/GNAT. Ada version is 1995 but we're in the middle of a compiler migration (newer version of GPS/GNAT) with the possibility of using newer Ada spec.

The middleware is being written in C++ and I would like to use a union type containing all types that are passed between modules so I won't need to specify one put/get function for each type that is used on the system.

The question is, are C++ unions binary compatible to Ada variant records? In other words, if I pass a C++ union to Ada code will it be able to read it as a Variant record? (and vice-versa)

I think that for this to be possible some adjustments will be necessary... (Eg.: C++ unions do not contain a member which describes its content while Ada variant records do)

like image 387
Guarita Avatar asked Nov 03 '11 11:11

Guarita


2 Answers

Possibly.

Ada 2005 provides the Unchecked_Union pragma which allows a program to "[specify] an interface correspondence between a given discriminated type and some C union. The pragma specifies that the associated type shall be given a representation that leaves no space for its discriminant(s)."

From my reading of the RM section, one declares an Ada type with the discriminant(s) needed to define a variant record, but no storage space is allocated for the discriminant(s). I take it this means on the Ada side that the discriminant cannot subsequently be referenced. (There are other restrictions as well, like all the fields must be C-compatible, see RM B.3.3 for more info.)

I've never used this pragma, and I can't help but think that it will require some experimentation to get it to (hopefully) work with your system. Good luck!

like image 86
Marc C Avatar answered Nov 09 '22 12:11

Marc C


Yes.

Ada is compatible with C/C++ Unions. See here for how to do it (pdf) In particular Page 5 shows how to do it with Unions & Tags. It should be the same for using Discriminant records. (Caveat: it is probably not the compiler you are using, but i would be very surprised if yours didnt behave the same way!)

like image 5
NWS Avatar answered Nov 09 '22 10:11

NWS