CSpe |
back to project summary |
void g_hash_table_foreach(GHashTable *hash_table, GHFunc func, gpointer user_data);func will be called as func(key, value, user_data), so if you want to pass more arguments you usually end up creating a structure :
void f(int x, int y, // function we want called gpointer key, gpointer value); typedef struct { int x; int y; } t_dummy_struct; void wrapper_function(gpointer key, gpointer value, gpointer data) { t_dummy_struct *s = data; f(s->x, s->y, key, value); } void iterate(GHashTable *hash_table, int x, int y) { t_dummy_struct s; s.x = x; s.y = y; g_hash_table_foreach(hash_table, wrapper_function, &s); } |
void f(int x, int y, gpointer key, gpointer value); void iterate(GHashTable *hash_table, int x, int y) { void (*func)(gpointer key, gpointer value); // Note: a GHFunc also takes a gpointer user_data but we dont't use it here CSPE_4_2(func, // lvalue to store result in void, f, int, int, gpointer, gpointer, // type of f() x, y); // specialize x and y args g_hash_table_foreach(hash_table, (GHFunc) func, NULL); CSPE_FREE(func); } |
value_destroy_func: a function to free the memory allocated for the value used when removing the entry from the GHashTable
#include <cspe-1.0/cspe.h> t_pool * pool_create(void); void pool_destroy(t_pool*); void pool_dealloc(t_pool *pool, int id); void object_init(object *obj) { void (*value_destroy)(int id); obj->pool = pool_create(); CSPE_2_1(value_destroy, // lvalue to store result in void, pool_dealloc, t_pool*, int, // type of pool_dealloc() obj->pool); // specialize pool argument obj->hash_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, value_destroy); obj->value_destroy = value_destroy; // we need to free it later } void object_destroy(object *obj) { g_hash_table_destroy(obj->hash_table); CSPE_FREE(obj->value_destroy); pool_destroy(obj->pool); } |