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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC 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-7-bit Path: g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!newsfeed.freenet.ag!news.netcologne.de!ramfeed1.netcologne.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Interfacing C type, unconstrained array with record Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Sat, 16 Oct 2010 20:36:13 +0200 Message-ID: NNTP-Posting-Date: 16 Oct 2010 20:36:10 CEST NNTP-Posting-Host: 1484c460.newsspool2.arcor-online.net X-Trace: DXC=EkV:O7_>J[RYkFXOIPA9EHlD;3YcR4Fo<]lROoRQ8kFm2fcZ<8LT`c`FfcT X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:15544 Date: 2010-10-16T20:36:10+02:00 List-Id: 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); 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). -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de