comp.lang.ada
 help / color / mirror / Atom feed
* C Integers -> Booleans: shortcut?
@ 2003-09-28 16:46 chris
  2003-09-28 17:15 ` Simon Wright
  0 siblings, 1 reply; 4+ messages in thread
From: chris @ 2003-09-28 16:46 UTC (permalink / raw)


Hi,

Can you (in general) import a function taking/returning a C integer and 
treat it like a boolean for import?  Let the compiler deal with the 
difference?

Like say,

function is_broke (something : sometype) return Boolean;


pragma Import (C, Is_Broke, "is_broke");

with is_broke actually,

int is_broke (sometype something);


instead of going through

function is_broke (something : sometype) return Boolean is
begin
    return c_is_broke (something) /= 0;
end is_broke;


Cheers,
Chris




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C Integers -> Booleans: shortcut?
  2003-09-28 16:46 C Integers -> Booleans: shortcut? chris
@ 2003-09-28 17:15 ` Simon Wright
  2003-09-28 18:17   ` chris
  2003-09-29 20:19   ` Randy Brukardt
  0 siblings, 2 replies; 4+ messages in thread
From: Simon Wright @ 2003-09-28 17:15 UTC (permalink / raw)


chris <spamoff.danx@ntlworld.com> writes:

> Can you (in general) import a function taking/returning a C integer
> and treat it like a boolean for import?  Let the compiler deal with
> the difference?

With GNAT you would say

  type C_Boolean is new Boolean;
  pragma Convention (C, C_Boolean);

with the meaning that 0 => False, anything else => True.

For other compilers I would think you'd be better off specifying
Interfaces.C.int and comparing to 0.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C Integers -> Booleans: shortcut?
  2003-09-28 17:15 ` Simon Wright
@ 2003-09-28 18:17   ` chris
  2003-09-29 20:19   ` Randy Brukardt
  1 sibling, 0 replies; 4+ messages in thread
From: chris @ 2003-09-28 18:17 UTC (permalink / raw)


Simon Wright wrote:

> With GNAT you would say
> 
>   type C_Boolean is new Boolean;
>   pragma Convention (C, C_Boolean);
> 
> with the meaning that 0 => False, anything else => True.

I thought GNAT would do that, but hoped other compilers would too since 
it's the convention in C for 0 to be treated like false and anything 
else as true.

> For other compilers I would think you'd be better off specifying
> Interfaces.C.int and comparing to 0.

Will do! Thanks!




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: C Integers -> Booleans: shortcut?
  2003-09-28 17:15 ` Simon Wright
  2003-09-28 18:17   ` chris
@ 2003-09-29 20:19   ` Randy Brukardt
  1 sibling, 0 replies; 4+ messages in thread
From: Randy Brukardt @ 2003-09-29 20:19 UTC (permalink / raw)


"Simon Wright" <simon@pushface.org> wrote in message
news:x7vpthkrg0h.fsf@smaug.pushface.org...
> chris <spamoff.danx@ntlworld.com> writes:
>
> > Can you (in general) import a function taking/returning a C integer
> > and treat it like a boolean for import?  Let the compiler deal with
> > the difference?
>
> With GNAT you would say
>
>   type C_Boolean is new Boolean;
>   pragma Convention (C, C_Boolean);
>
> with the meaning that 0 => False, anything else => True.
>
> For other compilers I would think you'd be better off specifying
> Interfaces.C.int and comparing to 0.

Janus/Ada automatically converts Boolean to the C equivalent for calls. We
used it in our early Windows bindings - made things quite a bit clearer.

But the point is that it isn't standard, so if you want your code to work on
other compilers, don't do that. Claw doesn't use that, for instance.

                   Randy.






^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-09-29 20:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-28 16:46 C Integers -> Booleans: shortcut? chris
2003-09-28 17:15 ` Simon Wright
2003-09-28 18:17   ` chris
2003-09-29 20:19   ` Randy Brukardt

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