comp.lang.ada
 help / color / mirror / Atom feed
From: Ron Wills <ron.rwsoft@gmail.com>
Subject: Re: Interfacing C type, unconstrained array with record
Date: Sat, 16 Oct 2010 12:19:18 -0700 (PDT)
Date: 2010-10-16T12:19:18-07:00	[thread overview]
Message-ID: <ea2bf467-ca86-42db-ae4c-7c462b4889fd@l8g2000yql.googlegroups.com> (raw)
In-Reply-To: xtj9ihygde9q$.t30hjfo8ikh2.dlg@40tude.net

On Oct 16, 12:36 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Sat, 16 Oct 2010 10:35:39 -0700 (PDT), Ron Wills wrote:
> > Hi all
>
> > I've started learning Ada and started porting some software I had in C/
> > C++ as a learning exercise. The software uses SDL, so I started a thin
> > wrapper around it. I've been very successful with it, but with the
> > exception of the palette structure which in C is defined as:
>
> > typedef struct {
> >   int ncolors;
> >   SDL_Color colors[]; // Actually is SDL_Color *colors but this is how
> > it is laid out in memory
> > } SDL_Palette;
>
> > In Ada I'm trying to do something like:
>
> > type SDL_Color is
> >   record
> >     r, g, b, unused : Uint8;
> >   end;
> > type SDL_Color_Array is array(Positive range <>) of Color;
> > type SDL_Palette is
> >   record
> >     ncolors : Integer;
> >     colors : SDL_Color_Array;
> >   end;
>
> > Now Ada (GNAT) won't compile this because the colors component is
> > unconstrained, which is understandable. I've been googling for a
> > couple of days for a way of modeling this in Ada and still having
> > access to all the colors in the colors component. In other SDL
> > interface implementations, all I've seen is that the colors component
> > is made into a type of "Dummy" component and made more or less
> > useless. I've also been combing the reference manual, but nothing I've
> > noticed seems to solve this issue. I'm sure I'm not the only one that
> > has come across this problem and was wondering what solutions are
> > available.
>
> You need a flat array, e.g.
>
>    with Interfaces;  use Interfaces;
>    with Interfaces.C;  use Interfaces.C;
>       ...
>    type SDL_Color is record
>       R, G, B, Unused : Unsigned_8;
>    end record;
>    pragma Convention (C, SDL_Color);
>    type SDL_Color_Array is array (Positive) of aliased SDL_Color;
>    pragma Convention (C, SDL_Color_Array);
>    type SDL_Palette is record
>       N_Colors : Int;
>       Colors   : SDL_Color_Array;
>    end record;
>    pragma Convention (C, SDL_Palette);

The "pragma Convention" did the trick! I must say, Ada is the one
language I've encountered that seems to have the largest learning
curve because of the most cryptic references I ever seen ;)

> Now, you cannot directly declare a SDL_Palette variable, because it is too
> large. You should do it as you would do in C, i.e. allocate some amount of
> memory and then set structure pointer to it. You do not need 'new' or
> access types in Ada. It can be on the stack like this:
>
>    with System.Storage_Elements;  use System.Storage_Elements;
>       ...
>    Raw_Memory : Storage_Array (1..1_024); -- Memory
>    Palette    : SDL_Palette;
>    for Palette'Address use Raw_Memory'Address;
> begin
>    Palette.N_Colors := 20;
>
> Of course computing memory size from N_Colors is up to you (as in C).

Thanks for the tip, but I don't actually need to allocate any palette
records (this should never be done). The palette is a read-only field
within the pixel information structure of a surface. I just need this
record definition to define the constant access pointer to be able to
access the palette information.

Many thanks, now I move forward again :D

> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de




  reply	other threads:[~2010-10-16 19:19 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-16 17:35 Interfacing C type, unconstrained array with record Ron Wills
2010-10-16 18:36 ` Dmitry A. Kazakov
2010-10-16 19:19   ` Ron Wills [this message]
2010-10-16 22:15     ` Jeffrey Carter
2010-10-17 10:20       ` Simon Wright
2010-10-17 12:38         ` Robert A Duff
2010-10-17  8:35     ` Dmitry A. Kazakov
2010-10-17 12:30       ` Robert A Duff
2010-10-16 21:41   ` Robert A Duff
2010-10-16 23:34     ` tmoran
2010-10-17  6:59     ` J-P. Rosen
2010-10-17 12:34       ` Robert A Duff
2010-10-17  7:45     ` Dmitry A. Kazakov
replies disabled

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