comp.lang.ada
 help / color / mirror / Atom feed
From: kevq@banana.demon.co.uk (Kevin F. Quinn)
Subject: Re: C++ to Ada95, help please
Date: Tue, 21 Mar 1995 23:27:35 GMT
Date: 1995-03-21T23:27:35+00:00	[thread overview]
Message-ID: <19950321.232735.72@banana.demon.co.uk> (raw)
In-Reply-To: 3kjd2m$d3t@jerry.rb.icl.co.uk

In article <3kjd2m$d3t@jerry.rb.icl.co.uk>,
          skj@rb.icl.co.uk (Simon Johnston) wrote:

> Hi, thanks everyone for helping with my last query. I have a new C nasty for
> you. I am converting some C headers into packages for use with GNAT and 
> have come accross the old chestnut:
> 
> typedef struct _STRUCT_NAME {.....type struct_STRUCT_NAME is
> ....................................record
>   int item1;..........................item1  : int;
>   union {                             :
>     int  u_item1;                     :
>     long u_item2;                     :
>   } u_name;                           :
>   int item2;..........................item2  : int;
> ....................................end record;
> } STRUCT_NAME;....................type STRUCT_NAME is new struct_STRUCT_NAME

> 
> this I know is a contrived example but it does show the problem, how do I
> describe the C struct on the left as an Ada record on the right. (The code
> I have put on the right is lifted from the windows.ads package spec provided
> in the GNAT examples directory for Windows NT.
> 
> Thanks.

Yeuch!  If "int" and "long" are not the same size, any amount of
tricksy mucking about could happen.  I would be tempted to write:


  type u_name_union_type is
    record
      u_item1 : Unsigned_Integer;            <- or whatever.
      u_item2 : Unsigned_LongInteger;        <-
    end record;

  type struct_STRUCT_NAME is
    record
      item1  : Integer;
      u_name : u_name_union_type;
      item2  : Integer;
    end record;


in the first instance.  The naming structure would then remain, although
the actual data layout would be different.  If the layout is important,
then how about:

  type struct_STRUCT_NAME is
    record
      item1  : Integer;
      u_name : LongInteger;          <-- type is the size of the union.
      item2  : Integer;
    end record;

  function u_item1(u_uname : in LongInteger) return Integer;
  function u_item2(u_uname : in LongInteger) return LongInteger;

where function u_item1 returns the lower half of the u_name parameter
(several ways to do this - best way will depend on the compiler).

Unfortunately, this mucks up the naming structure; you need to use

   item1_value := u_item1(struct.u_name);

instead of

   item1_value := struct.u_name.u_item1;

(which works with the top version).



Of course, unions are usually used in 'C' for variant records, which
you can do in Ada.  'twill depend on how well the compiler allows
you to specify the actual layout of the variant record that decides
whether this would work well enough.  It's unlikely, though, given the
layout above.

You could declare a separate type for each union element, then declare
several data items, and alias them  [fx: throws up :) ]

For example:

  type u_name_item1_type is
    record
      u_item1 : Integer;
      padding : Integer;     <-- Assuming LongInteger'size=2xInteger'size
    end record;                  Anyway, in general some padding will be
                                 required.
  type u_name_item2_type is
    record
      u_item2 : LongInteger;
    end record;

  type struct_STRUCT_NAME is
    record
      item1  : Integer;
      u_name : u_name_item1_type;
      item2  : Integer;
    end record;

  type struct_STRUCT_NAME_item2 is
    record
      item1  : Integer;
      u_name : u_name_item1_type;
      item2  : Integer;
    end record;

  DataItem : struct_STRUCT_NAME;
  DataItem2 : struct_STRUCT_NAME_item2;
  for DataItem2 use at DataItem'Address;

and use DataItem2 whenever you need to access .u_name.u_item2.

Hmm.  Interesting problem.  There's probably a nice simple solution;
I just can't see one :)
-- 
Kevin F. Quinn           * "That's not what you said when you sent him your
kevq@banana.demon.co.uk  * Navel."   "Novel, Baldrick, not navel."
kevq@cix.compulink.co.uk * "Well it sounds like a case of soggy grapefruits
Compu$erve: 100025,1525  * to me..."                        BlackAdder III
... Death is proven to be 99.9% fatal to all laboratory rats.



  reply	other threads:[~1995-03-21 23:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-20  8:06 C++ to Ada95, help please Simon Johnston
1995-03-21 23:27 ` Kevin F. Quinn [this message]
1995-03-22  5:07   ` Vladimir Vukicevic
1995-03-22  1:22 ` Tucker Taft
1995-03-22 11:38   ` Robb Nebbe
1995-03-23 12:28   ` Mike Meier
1995-03-23 18:31     ` Tucker Taft
1995-03-24 21:24     ` Robert Dewar
1995-03-27 14:58       ` Norman H. Cohen
1995-03-23 13:30   ` Robert Dewar
1995-03-23 18:01 ` Stephen A. Leake
1995-03-24  9:07   ` Vladimir Vukicevic
1995-03-25 10:02     ` Keith Thompson
  -- strict thread matches above, loose matches on Subject: below --
1995-03-08  7:54 Simon Johnston
1995-03-08 13:56 ` Robert A Duff
1995-03-09 21:23 ` Tucker Taft
replies disabled

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