g_(s)list_data()



Folks,
	We've all noticed that it is kinda weird to have g_list_next()
as an accessor macro, but use list->data as the data accessor.  But
we've never put g_list_data() into the code, because no one felt it
important enough.
	However, I was thinking about it again today, and while 

#define g_list_data(list) ((list) ? ((GList *)(list))->data) : NULL)

is relatively useless

#define g_list_data(data_type, list) \
       ((list) ? ((data_type)(((GList *)(list))->data)) : NULL)

is damned useful.  Everyone right now has code

while (list)
{
    Foo *foo = (Foo *)list->data;
    do_something(foo->obj);
    list = g_list_next(list);
}

or the uglier 

while (list)
{
    do_something(((Foo *)list->data)->obj);
    list = g_list_next(list);
}

with g_list_data(data_type, list) you get

while (list)
{
    Foo *foo = g_list_data(Foo *, list);
    do_something(foo->obj);
    list = g_list_next(list);
}

	Which is very nice to read.  Face it, the cast will be
explicitly typed in 90% of cases (1% really want a gpointer, and the
other 9% are GTK_FOO(list->data) calls).


What do folks think?  This can, of course, work for GSList too (and any
other ->data structs I've forgotten).

Joel

-- 

"In the arms of the angel, fly away from here,
 From this dark, cold hotel room and the endlessness that you fear.
 You are pulled from the wreckage of your silent reverie.
 In the arms of the angel, may you find some comfort here."

			http://www.jlbec.org/
			jlbec evilplan org




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