Re: gtk-demo uses private fields



On 9 Nov 2001, Murray Cumming wrote:

> On Fri, 2001-11-09 at 20:47, Darin Adler wrote:
> > on 11/9/01 11:54 AM, Murray Cumming at murrayc t-online de wrote:
> > 
> > > I don't understand. Please elaborate.
> > 
> > If you define something like this:
> > 
> >     struct x {
> >         int b : 1;
> >     };
> > 
> > Then b will contain either 0 or -1 (at least with gcc), not 0 or 1 as you
> > probably want. So this:
> > 
> >     struct x {
> >         gboolean b : 1;
> >     };
> > 
> > Is a bad idea, and can lead to annoying bugs. Thus in glib we use:
> > 
> >     struct x {
> >         guint b : 1;
> >     };
> > 
> > Eel has eel_boolean_bit as a typedef for guint, but the glib folks found
> > that ugly and decided not to add something like that.
> > 
> > An alternative would be to have gboolean be guint instead of int, but it's
> > far too late in the backward-compatibility day for that to be changed, I
> > imagine.
> 
> Thanks a lot for that. I guess that it's not a problem that these guints
> are set from gbooleans? This must be very common.

a gboolean type variable should really _only_ contain the values
TRUE or FALSE, if not you probably choosed your type wrong.
an exception is function arguments, e.g.:

#define	MY_FOO_FLAG   (1 << 5)
guint my_foo_value = 0xfffff;
evaluate_boolean (my_foo_value & MY_FOO_FLAG);

here, you'll get trouble if you do:

void evaluate_boolean (gboolean boolval)
{
  if (boolval == TRUE) /* ... */ ;
  else /* ... */ ;
}

so for glib/gtk etc., all functions that take a boolean and aren't just
done with:
  if (!boolval) /* ... */ ;
should upon entry do something like:
  boolval = boolval ? TRUE : FALSE;
or:
  boolval = boolval != FALSE;

so as to not put the burden on the caller, we've had subtle bugs
with this, which is why flag checking macros also contain a 0
comparison now:

#define GTK_WIDGET_NO_WINDOW(wid)         ((GTK_WIDGET_FLAGS (wid) & GTK_NO_WINDOW) != 0)

> -- 
> Murray Cumming
> murrayc usa net
> www.murrayc.com
> 

---
ciaoTJ




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]