From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,d4984245154c8ef1 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!l8g2000yql.googlegroups.com!not-for-mail From: Ron Wills Newsgroups: comp.lang.ada Subject: Re: Interfacing C type, unconstrained array with record Date: Sat, 16 Oct 2010 12:19:18 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 24.89.74.35 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1287256758 18019 127.0.0.1 (16 Oct 2010 19:19:18 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 16 Oct 2010 19:19:18 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: l8g2000yql.googlegroups.com; posting-host=24.89.74.35; posting-account=sfUYLwoAAADgN4x5cQtEVb7ua7QwdFn4 User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.10) Gecko/20100915 Ubuntu/10.04 (lucid) Firefox/3.6.10,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:15545 Date: 2010-10-16T12:19:18-07:00 List-Id: On Oct 16, 12:36=A0pm, "Dmitry A. Kazakov" 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 { > > =A0 int ncolors; > > =A0 SDL_Color colors[]; // Actually is SDL_Color *colors but this is ho= w > > it is laid out in memory > > } SDL_Palette; > > > In Ada I'm trying to do something like: > > > type SDL_Color is > > =A0 record > > =A0 =A0 r, g, b, unused : Uint8; > > =A0 end; > > type SDL_Color_Array is array(Positive range <>) of Color; > > type SDL_Palette is > > =A0 record > > =A0 =A0 ncolors : Integer; > > =A0 =A0 colors : SDL_Color_Array; > > =A0 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. > > =A0 =A0with Interfaces; =A0use Interfaces; > =A0 =A0with Interfaces.C; =A0use Interfaces.C; > =A0 =A0 =A0 ... > =A0 =A0type SDL_Color is record > =A0 =A0 =A0 R, G, B, Unused : Unsigned_8; > =A0 =A0end record; > =A0 =A0pragma Convention (C, SDL_Color); > =A0 =A0type SDL_Color_Array is array (Positive) of aliased SDL_Color; > =A0 =A0pragma Convention (C, SDL_Color_Array); > =A0 =A0type SDL_Palette is record > =A0 =A0 =A0 N_Colors : Int; > =A0 =A0 =A0 Colors =A0 : SDL_Color_Array; > =A0 =A0end record; > =A0 =A0pragma 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 to= o > large. You should do it as you would do in C, i.e. allocate some amount o= f > 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: > > =A0 =A0with System.Storage_Elements; =A0use System.Storage_Elements; > =A0 =A0 =A0 ... > =A0 =A0Raw_Memory : Storage_Array (1..1_024); -- Memory > =A0 =A0Palette =A0 =A0: SDL_Palette; > =A0 =A0for Palette'Address use Raw_Memory'Address; > begin > =A0 =A0Palette.N_Colors :=3D 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