开发者

Macro for iterating over a GList

开发者 https://www.devze.com 2022-12-09 11:48 出处:网络
I am using GLib\'s doubly linked list structure, GList. I would like to know if there is any standard macro for iterating over a GList. I couldn\'t find any such thing in the GLib documentation. As a

I am using GLib's doubly linked list structure, GList. I would like to know if there is any standard macro for iterating over a GList. I couldn't find any such thing in the GLib documentation. As a result I have made my own macro, but I'd rather use something standard if it exists.

To Illustrate the the issue: Usually I write a lot of code which looks like this:

GList *list, *elem;
MyType *item;

for(elem = list; elem; elem = elem->next) {
  item = elem->data;
  /* do something with item */
}

With a macro it can be reduced to

GList *list;
MyType *item;

GFOREACH(item, list) {
  /* do something with item */
}

Which is much less noisy.


Note: I realised that GLib supplies a foreach function for iterating ov开发者_JS百科er a list and calling a callback for each element, but often the indirection of a callback makes the code harder to read, especially if the callback is only used once.


Update: seeing as there is no standard macro, I'm putting the macro I am using here in case it is of any use to someone else. Corrections/improvements are welcome.

#define GFOREACH(item, list) for(GList *__glist = list; __glist && (item = __glist->data, true); __glist = __glist->next)


There is no such macro.

I usually use a for loop like in your example unless the operation spans more than, say, fifteen lines, in which case I usually find that an extra foreach function with a descriptive name is more readable than the alternative.

What you may not realize is that you don't necessarily have to write your own foreach function:

g_list_foreach(list, (GFunc)g_free, NULL);

frees each item in the list, an operation that I often use.

0

精彩评论

暂无评论...
验证码 换一张
取 消