Re: Pass more widgets as gpointer.



You can use a few tricks to help manage these one-shot constructs...

One that I used quite a while ago, was a wrapper around a simple array that stores array size and free 
functions for each member, so a simple "free" function can rip through and clear everything out.  It's good 
as long as you're not trying to store too many items (it is a little wasteful).


gpointer oneshot_new(gint n) {
    gpointer data = g_malloc(sizeof(GtkWidget *)*((n)*2+1));
    data[0] = GINT_TO_POINTER(n);
    return data;
}

void oneshot_free(gpointer data) {
    gint i, count = GPOINTER_TO_INT(data[0]);
    for (i = 1; i < count*2; i+=2) {
        if (data[i] && data[i+1]) data[i+1](data[i]);
    }
    g_free(data);
}

#define oneshot_set(arr,n,data,unref) arr[n*2+1]=data,arr[n*2+2]=unref
#define oneshot_set_with_unref(arr,n,data) oneshot_set(arr,n,data,g_unref)
#define oneshot_set_with_free(arr,n,data) oneshot_set(arr,n,data,g_free)
#define oneshot_get(arr,n) arr[n*2+1]

#define oneshot_set_widget(arr,n,data) oneshot_set_with_unref(arr,n,data)
#define oneshot_get_widget(arr,n) ((GtkWidget*)(oneshot_get(arr,n)))
// can be helpful to add a type assertion into the oneshot_set_widget definition, etc.


Well, you get the idea.  I'm sure I've messed up the type casting and probably a few other bits and pieces in 
there somewhere, I've been working mostly in HLL's just lately, and haven't tested that at all.  But it's one 
more idea to keep in your bag of tricks.


Fredderic


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