From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Passing C-style flags to C subprograms
Date: Fri, 30 Mar 2012 14:55:48 -0700
Date: 2012-03-30T14:55:48-07:00 [thread overview]
Message-ID: <jl5a6j$t2g$1@tornado.tornevall.net> (raw)
In-Reply-To: <slrnjnbu7o.1lme.lithiumcat@sigil.instinctive.eu>
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
prev parent reply other threads:[~2012-03-30 21:56 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-03-30 18:18 Passing C-style flags to C subprograms Natasha Kerensikova
2012-03-30 19:57 ` Randy Brukardt
2012-04-02 10:40 ` Natasha Kerensikova
2012-04-03 2:14 ` Randy Brukardt
2012-04-03 1:44 ` BrianG
2012-03-30 21:55 ` Jeffrey Carter [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox