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.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c9bb37a1fdd0d3e9 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Received: by 10.68.236.170 with SMTP id uv10mr401356pbc.4.1333144561605; Fri, 30 Mar 2012 14:56:01 -0700 (PDT) Path: z9ni20088pbe.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!hulme.interac.it!news.newsland.it!news.tornevall.net!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Passing C-style flags to C subprograms Date: Fri, 30 Mar 2012 14:55:48 -0700 Organization: TornevallNET - http://news.tornevall.net Message-ID: References: NNTP-Posting-Host: 33235f9adaa82885179464b9ea27dedb Mime-Version: 1.0 X-Trace: b6a6d755350aef9cdf0585b9259647ed X-Complaints-To: abuse@tornevall.net User-Agent: Mozilla/5.0 (X11; Linux i686; rv:11.0) Gecko/20120310 Thunderbird/11.0 X-Complaints-Language: Spoken language is english or swedish - NOT ITALIAN, FRENCH, GERMAN OR ANY OTHER LANGUAGE! In-Reply-To: X-UserIDNumber: 1738 X-Validate-Post: http://news.tornevall.net/validate.php?trace=b6a6d755350aef9cdf0585b9259647ed X-Complaints-Italiano: Non abbiamo padronanza della lingua italiana - se mandate una email scrivete solo in Inglese, grazie X-Posting-User: 0243687135df8c4b260dd4a9a93c79bd Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Date: 2012-03-30T14:55:48-07:00 List-Id: On 03/30/2012 11:18 AM, Natasha Kerensikova wrote: > > type These_Flags is record > Allow_Stuff : Boolean := False; > Do_Otherwise : Boolean := False; > Special_Feature : Boolean := False; > end record; > for These_Flags use record > Allow_Stuff at 0 range 0 .. 0; > Do_Otherwise at 0 range 1 .. 1; > Special_Feature at 0 range 2 .. Interfaces.C.int'Size; > end record; > for These_Flags'Size use Interfaces.C.int'Size; I don't see how this could work. You've specified Interfaces.C.Int'Size + 1 bits in These_Flags, and then specified that These_Flags is Interfaces.C.Int'Size bits in size. It's probably better to have 3 Boolean fields and a padding field that takes up the additional bits. That will work as long as you don't move to a big-endian machine. If you're worried about portability, the modular type approach is probably better. Brukardt has pointed out some pros and cons of both approaches. If you're really worried about portability, then you should make sure you pass the same kind to C that C expects. If the C functions take an int, then you should pass an Interfaces.C.Int to them. This doesn't prevent using either of the approaches, but does involve an unchecked conversion to or from Interfaces.C.Int hidden in the lowest level of your code. There are at least 2 parts to your problem: How you make this appear to the rest of your Ada code, and how you implement the low-level interface to C. There could also be a 3rd part that sits in between those, something more Ada-like than the low-level interface but less application-specific than the interface to this specific application. -- Jeff Carter "Friends don't let friends program in C++." Ludovic Brenta 114