From 891435f53bf3b8f52e1161fc986f127d3343bf62 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 04 Oct 2017 17:41:12 -0400 Subject: src/config.c: Run callbacks on value setting And just use "enum", "union", and "struct" keywords and namespaces instead of defining types. Otherwise, at least one of these types would have to be forward declared. --- diff --git a/src/config.c b/src/config.c index 701dc35..97f8429 100644 --- a/src/config.c +++ b/src/config.c @@ -26,14 +26,24 @@ #include "config.h" -typedef enum { +enum type { TYPE_BOOLEAN, -} Type; +}; -typedef struct { - Type type; - GList *callbacks; -} ConfigItem; +union callback { + void (*boolean_cb)(const gchar *, gboolean, gpointer); +}; + +struct callbacks { + union callback cb; + gpointer user_data; + struct callbacks *next; +}; + +struct item { + enum type type; + struct callbacks *callbacks; +}; static void split_name(const gchar *name, gchar **group, gchar **key) @@ -45,9 +55,11 @@ split_name(const gchar *name, gchar **group, gchar **key) } static void -add_type_and_callbacks(MqConfig *config, const gchar *name, Type type) +set_type_or_run_callbacks(MqConfig *config, const gchar *name, gpointer value, + enum type type) { - ConfigItem *item; + struct item *item; + struct callbacks *cbs; if (!config->types_and_cbs_set) { item = g_malloc(sizeof(*item)); @@ -55,6 +67,17 @@ add_type_and_callbacks(MqConfig *config, const gchar *name, Type type) item->callbacks = NULL; g_hash_table_insert(config->types_and_cbs, g_strdup(name), item); + } else { + item = g_hash_table_lookup(config->types_and_cbs, name); + for (cbs = item->callbacks; cbs; cbs = cbs->next) { + switch (item->type) { + case TYPE_BOOLEAN: + cbs->cb.boolean_cb(name, + *((gboolean *) value), + cbs->user_data); + break; + } + } } } @@ -68,7 +91,7 @@ set_boolean(MqConfig *config, const gchar *name, gboolean value) g_key_file_set_boolean(config->key_file, group, name, value); - add_type_and_callbacks(config, name, TYPE_BOOLEAN); + set_type_or_run_callbacks(config, name, &value, TYPE_BOOLEAN); } static void @@ -115,7 +138,7 @@ mq_config_save(MqConfig *config) void mq_config_set(MqConfig *config, const gchar *name, const gchar *value) { - ConfigItem *item; + struct item *item; item = g_hash_table_lookup(config->types_and_cbs, name); g_assert(item); -- cgit v0.9.1