LibTpz and C++
20. October 2006
I am able to write that in the moment because I am hacking on the glom po import/export feature and g++ is just that damn slow that I takes ages to build glom on my Pentium M 1.73 (which is usually not really slow and has great beryl effects now).
Anyway, I have some api suggestions for libtpz (for ThreePointZero). There also is a Project at gna for it now:
Interface TpzIter
{
next();
prev();
get();
equal();
}
Interface TpzList
{
prepend();
append();
remove();
foreach();
copy();
get_length();
Iter* begin(); // first item
Iter* end(); // after last item
}
I have some initial code here but it needs some polishment to be added to svn. I still would like to derive GInterfaces but I doubt that this is possible in C. The design is very close to what the C++ STL provides.
Some might say now, what the advantage over GList* is so here are two points:
– GList* is used for the whole list and for an individual item – that is confusing
– [Edit: Removed]*
So there is another more important point: You can hide your list implementation. In fact, you do not have to use a “list” at all, you could have a tree, an array or any other data structure that you can wrap on that interface. This way, you can also add optimations that are not possible with GList*.
Of course there is a perfomance impact but I think that is not that major that we should not try it. Maybe someone wants to create benchmarks later.
Another thing: Dear Lazyweb, how can I configure beryl to maximize a window like metacity when I double-click on the title bar?
*[EDIT]
This point was not very good but it is still here:
– Writing loops is more intuitive, instead of
/*GList* node = list;
while (node)
{
/* do something */
node = g_list_next(node);
}*/
[EDIT] Some pointed out (correctly) that it is better to write
GList* node;
for (node = list; node != NULL; node = node->next)
{
/* do something */
}
you can use
TpzIter* iter;
for (iter = tpz_list_begin(list); !tpz_iter_equal(iter, tpz_list_end(list)); tpz_iter_next(iter))
{
/* do something */
}
20. October 2006 at 21:49
you shouldn’t ever, ever iterate on a G(S)List using the while() loop: it’s unnatural, leading to bugs and leaks and it’s overall more complicated.
you should use the for() loop:
GList *iter;
for (iter = list; iter != NULL; iter = iter->next)
do_something_with_data (iter->data);
in C, virtualization aside, is completely equivalent to your C++ example, readability-wise.