diff options
author | Colin Walters <walters@verbum.org> | 2005-07-09 01:46:51 +0000 |
---|---|---|
committer | Colin Walters <walters@verbum.org> | 2005-07-09 01:46:51 +0000 |
commit | 0ccef79d7e9d1b19cdb10b3a147b64b41686905d (patch) | |
tree | 78df63d544586877804afe2cdef873f20771ce0d /glib/dbus-gvalue-utils.c | |
parent | d8e7405eb35974518b0b579e2e2789fd065cb477 (diff) |
2005-07-08 Colin Walters <walters@verbum.org>
* test/glib/test-service-glib.xml:
* test/glib/test-service-glib.c:
* test/glib/test-dbus-glib.c: Test a{sv}.
* glib/examples/statemachine/statemachine.c:
* glib/examples/statemachine/statemachine-server.c:
* glib/examples/statemachine/statemachine-client.c: Fix some bugs,
add progress bar, etc.
* glib/dbus-gvalue.c (register_array, register_dict): Delete; not
needed anymore due to generic array/map marshalling.
(dbus_g_value_types_init): Don't register basic arrays or the
string/string hash.
(dbus_gtype_from_signature_iter): Don't try to recurse into
variants.
(dbus_gtype_to_signature): Check collection/map before type
metadata.
(demarshal_garray_basic): Renamed to demarshal_collection_array.
(demarshal_ghashtable): Renamed to demarshal_map; fix to use new
generic map creation/append functions instead of hash table
specifically.
(get_type_demarshaller): Handle maps.
(demarshal_collection): Dispatch on collection type to either
demarshal_collection_ptrarray or demarshal_collection_array.
(get_type_marshaller): Handle maps.
(marshal_collection): Dispatch collection type to either
marshal_collection_ptrarray or marshal_collection_array.
(_dbus_gvalue_test): New test.
* glib/dbus-gvalue-utils.c (unset_and_free_g_value): New function.
(hash_free_from_gtype): Use it to free GValues.
(hashtable_append): New function.
(ptrarray_append): Fix prototype.
(slist_append): Ditto.
(_dbus_gvalue_utils_test): Extend tests.
* glib/dbus-gtype-specialized.c
(dbus_g_type_specialized_init_append): Renamed from
dbus_g_type_specialized_collection_init_append. Remove const from
value, since we steal it.
(dbus_g_type_specialized_map_append): New function.
* glib/dbus-gtype-specialized.h: Update prototypes.
Add DBusGTypeSpecializedMapAppendFunc.
* glib/dbus-gtest.c (dbus_glib_internal_do_not_use_run_tests): Run
_dbus_gvalue_test.
* glib/dbus-gtest.h: Prototype it.
Diffstat (limited to 'glib/dbus-gvalue-utils.c')
-rw-r--r-- | glib/dbus-gvalue-utils.c | 116 |
1 files changed, 111 insertions, 5 deletions
diff --git a/glib/dbus-gvalue-utils.c b/glib/dbus-gvalue-utils.c index b281f6ee..b17eee16 100644 --- a/glib/dbus-gvalue-utils.c +++ b/glib/dbus-gvalue-utils.c @@ -226,6 +226,15 @@ hash_func_from_gtype (GType gtype, GHashFunc *func) } } +static void +unset_and_free_g_value (gpointer val) +{ + GValue *value = val; + + g_value_unset (value); + g_free (value); +} + static gboolean hash_free_from_gtype (GType gtype, GDestroyNotify *func) { @@ -244,7 +253,7 @@ hash_free_from_gtype (GType gtype, GDestroyNotify *func) default: if (gtype == G_TYPE_VALUE) { - *func = (GDestroyNotify) g_value_unset; + *func = unset_and_free_g_value; return TRUE; } return FALSE; @@ -441,6 +450,17 @@ dbus_g_hash_table_insert_steal_values (GHashTable *table, g_hash_table_insert (table, key, val); } +static void +hashtable_append (DBusGTypeSpecializedAppendContext *ctx, + GValue *key, + GValue *val) +{ + GHashTable *table; + + table = g_value_get_boxed (ctx->val); + dbus_g_hash_table_insert_steal_values (table, key, val); +} + static gpointer hashtable_constructor (GType type) { @@ -667,7 +687,7 @@ ptrarray_copy (GType type, gpointer src) } static void -ptrarray_append (DBusGTypeSpecializedAppendContext *ctx, const GValue *value) +ptrarray_append (DBusGTypeSpecializedAppendContext *ctx, GValue *value) { GPtrArray *array; @@ -740,7 +760,7 @@ slist_copy (GType type, gpointer src) } static void -slist_append (DBusGTypeSpecializedAppendContext *ctx, const GValue *value) +slist_append (DBusGTypeSpecializedAppendContext *ctx, GValue *value) { GSList *list; @@ -822,7 +842,8 @@ dbus_g_type_specialized_builtins_init (void) NULL, NULL }, - hashtable_iterator + hashtable_iterator, + hashtable_append }; dbus_g_type_register_map ("GHashTable", &hashtable_vtable, 0); @@ -860,6 +881,35 @@ test_specialized_hash (const GValue *key, const GValue *val, gpointer user_data) } } +static void +test_specialized_hash_2 (const GValue *key, const GValue *val, gpointer user_data) +{ + TestSpecializedHashData *data = user_data; + const GValue *realval; + + g_assert (G_VALUE_HOLDS_STRING (key)); + g_assert (G_VALUE_TYPE (val) == G_TYPE_VALUE); + + realval = g_value_get_boxed (val); + + if (!strcmp (g_value_get_string (key), "foo")) + { + data->seen_foo = TRUE; + g_assert (G_VALUE_HOLDS_UINT (realval)); + g_assert (g_value_get_uint (realval) == 20); + } + else if (!strcmp (g_value_get_string (key), "baz")) + { + data->seen_baz = TRUE; + g_assert (G_VALUE_HOLDS_STRING (realval)); + g_assert (!strcmp ("bar", g_value_get_string (realval))); + } + else + { + g_assert_not_reached (); + } +} + gboolean _dbus_gvalue_utils_test (const char *datadir) { @@ -911,6 +961,62 @@ _dbus_gvalue_utils_test (const char *datadir) g_value_unset (&val); } + type = dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE); + g_assert (dbus_g_type_is_map (type)); + g_assert (dbus_g_type_get_map_key_specialization (type) == G_TYPE_STRING); + g_assert (dbus_g_type_get_map_value_specialization (type) == G_TYPE_VALUE); + { + GHashTable *instance; + GValue val = { 0, }; + TestSpecializedHashData hashdata; + DBusGTypeSpecializedAppendContext ctx; + GValue *eltval; + + instance = dbus_g_type_specialized_construct (type); + g_value_init (&val, type); + g_value_set_boxed_take_ownership (&val, instance); + + dbus_g_type_specialized_init_append (&val, &ctx); + + { + GValue keyval = { 0, }; + GValue valval = { 0, }; + g_value_init (&keyval, G_TYPE_STRING); + g_value_set_string (&keyval, "foo"); + + g_value_init (&valval, G_TYPE_VALUE); + eltval = g_new0 (GValue, 1); + g_value_init (eltval, G_TYPE_UINT); + g_value_set_uint (eltval, 20); + g_value_set_boxed_take_ownership (&valval, eltval); + dbus_g_type_specialized_map_append (&ctx, &keyval, &valval); + } + + { + GValue keyval = { 0, }; + GValue valval = { 0, }; + g_value_init (&keyval, G_TYPE_STRING); + g_value_set_string (&keyval, "baz"); + g_value_init (&valval, G_TYPE_VALUE); + eltval = g_new0 (GValue, 1); + g_value_init (eltval, G_TYPE_STRING); + g_value_set_string (eltval, "bar"); + g_value_set_boxed_take_ownership (&valval, eltval); + dbus_g_type_specialized_map_append (&ctx, &keyval, &valval); + } + + hashdata.seen_foo = FALSE; + hashdata.seen_baz = FALSE; + dbus_g_type_map_value_iterate (&val, + test_specialized_hash_2, + &hashdata); + + g_assert (hashdata.seen_foo); + g_assert (hashdata.seen_baz); + + g_value_unset (&val); + } + type = dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRING); g_assert (dbus_g_type_is_collection (type)); g_assert (dbus_g_type_get_collection_specialization (type) == G_TYPE_STRING); @@ -927,7 +1033,7 @@ _dbus_gvalue_utils_test (const char *datadir) g_value_init (&val, type); g_value_set_boxed_take_ownership (&val, instance); - dbus_g_type_specialized_collection_init_append (&val, &ctx); + dbus_g_type_specialized_init_append (&val, &ctx); g_value_init (&eltval, G_TYPE_STRING); g_value_set_static_string (&eltval, "foo"); |