G_DEFINE_ARRAY, G_DEFINE_LIST



The following mail is about using the chance of breaking API with
glib-3.0 for improving GArray and GList.

GArray in its current form is really broken:

        * Many of its functions are implemented as macro need its
        element type as argument. This looks strange and it is hard to
        remember which of the functions actually are macros and need the
        element type as argument.

        * Macros like g_array_append() mimic functions but only accept
        real variables as argument, but no immediate values:
        
                g_array_append_val (a, x * 2); /* invalid */
        
        * GArray doesn't know how to release its elements.
        
GList has similar problems:

        * It doesn't know the type of its elements, which makes
        re-factoring of of GList code quite a pain. It's really easy to
        change the element type of some list at one place and to forget
        it at some other place.
        
        * Since GList doesn't know its element type, elements have to be
        released manually which forces you to repeat this code sequences
        over and over again:
        
                g_list_foreach (l, (GFunc) g_free, NULL);
                g_list_free (l);
                
        Or alternatively:
        
                while (l) {
                  g_free (l->data);
                  l = g_list_delete_link (l, l);
                }
                

So the idea I had today was to introduce some macros in the spirit of
G_DEFINE_TYPE for replacing the current mess.

For instance G_DEFINE_ARRAY(GIntArray, g_int_array, int) could be used
to declare some array of integer values:

        GIntArray* g_int_array_new       (void);
        GIntArray* g_int_array_sized_new (guint      reserved_size);
        void       g_int_array_add       (GIntArray *array,
                                          int        value);
        
To support memory management some variant of the macro would exist:

        G_DEFINE_ARRAY_EXTENDED
          (GStringArray, g_string_array, char*, g_strdup, g_free);

This version of the macro would instruct glib to use g_strdup() and
g_free() for copying and freeing array elements.

Actually there also is a boxed type for character arrays, so what about
this (despite the old libglib vs. libgobject issue):

        G_DEFINE_ARRAY_WITH_TYPE
          (GStringArray, g_string_array, char*, G_TYPE_STRING);
        
Now g_boxed_copy() and g_boxed_free() could be used to copy and free
array elements.

For GList we'd have similar macros.

Discussion opened.

Ciao,
Mathias
-- 
Mathias Hasselmann <mathias hasselmann gmx de>
Openismus GmbH: http://www.openismus.com/
Personal Site: http://taschenorakel.de/

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil



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