From e888647a70094d5761f931e1a812764330ee5a2b Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 8 Jul 2005 16:25:39 +0000 Subject: 2005-07-08 Colin Walters * tools/Makefile.am: Kill of print-introspect in favor of using dbus-send --print-reply=literal. * tools/print-introspect.c: Deleted. * test/glib/test-service-glib.xml: * test/glib/test-service-glib.c (my_object_get_objs): New test for "ao". * test/glib/test-dbus-glib.c (echo_received_cb): Free echo data. (main): Test GetObjs. * glib/examples/statemachine/Makefile.am: * glib/examples/statemachine/sm-marshal.list: * glib/examples/statemachine/statemachine-client.c: * glib/examples/statemachine/statemachine-server.c: * glib/examples/statemachine/statemachine-server.xml: * glib/examples/statemachine/statemachine.c: * glib/examples/statemachine/statemachine.h: * glib/examples/statemachine/statemachine.xml: New example. * glib/examples/example-service.c (main): Move invocation of dbus_g_object_type_install_info earlier, to emphasize it should only be done once. * glib/examples/example-signal-emitter.c (main): Ditto. * glib/examples/Makefile.am (SUBDIRS): Include statemachine. * glib/dbus-gvalue.h (dbus_gtype_to_signature) (dbus_gvalue_marshal): Update prototypes. * glib/dbus-gvalue.c: Update all marshalling functions to take const GValue instead of GValue. (signature_iter_to_g_type_array): Return a GPtrArray for nonfixed types. (dbus_gvalue_to_signature): Update for dbus_gtype_to_signature change. (dbus_gtype_to_signature): Handle generic collecitons and maps. Return a newly-allocated string. (demarshal_proxy, demarshal_object_path, demarshal_object) (demarshal_strv, demarshal_ghashtable): Set error, don't assert if we get the wrong types from message. (get_type_demarshaller): New function, extracted from dbus_gvalue_demarshal. (demarshal_collection): New function, demarshals generic collection. (dbus_gvalue_demarshal): Just invoke result of get_type_demarshaller. Throw error if we don't have one. (marshal_garray_basic): Abort on OOM. (get_type_marshaller): New function, extracted from dbus_gvalue_marshal. (collection_marshal_iterator, marshal_collection): New functions; implements generic marshalling for an iteratable specialized collection. (dbus_gvalue_marshal): Just invoke result of get_type_marshaller. * glib/dbus-gvalue-utils.c (gvalue_from_ptrarray_value): Handle G_TYPE_STRING. (ptrarray_value_from_gvalue): Ditto. (ptrarray_append, ptrarray_free): New functions. (slist_constructor, slist_iterator, slist_copy_elt, slist_copy) (slist_append, slist_end_append, slist_free): New functions. (dbus_g_type_specialized_builtins_init): Add append fuctions for GPtrArray and GSList. Register GSList. (test_specialized_hash, _dbus_gvalue_utils_test): New functions. * glib/dbus-gtype-specialized.h (DBusGTypeSpecializedAppendContext): New. (dbus_g_type_specialized_collection_init_append) (dbus_g_type_specialized_collection_append) (dbus_g_type_specialized_collection_end_append): Prototype. (DBusGTypeSpecializedCollectionVtable): Add append_func and end_append_func. * glib/dbus-gtype-specialized.c (dbus_g_type_specialized_collection_init_append) (dbus_g_type_specialized_collection_append) (dbus_g_type_specialized_collection_end_append): New functions. (dbus_g_type_map_value_iterate): Take const GValue. (dbus_g_type_collection_value_iterate): Ditto. * glib/dbus-gtest.c (dbus_glib_internal_do_not_use_run_tests): Run _dbus_gvalue_utils_test. * glib/dbus-gtest.h: Prototype it. * glib/dbus-gproxy.c (dbus_g_proxy_manager_filter): Avoid using uninitialized owner_list. (dbus_g_proxy_begin_call_internal): Move return_if_fail to public API. (dbus_g_proxy_end_call_internal): Update to use error set from dbus_gvalue_demarshal instead of setting it here. (dbus_g_proxy_begin_call): Move return_if_fail here. * glib/dbus-gobject.c (write_interface): Update for dbus_gtype_to_signature returning new string. * configure.in: Add glib/examples/statemachine. --- glib/dbus-gvalue-utils.c | 261 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 252 insertions(+), 9 deletions(-) (limited to 'glib/dbus-gvalue-utils.c') diff --git a/glib/dbus-gvalue-utils.c b/glib/dbus-gvalue-utils.c index 9eea4bf0..b281f6ee 100644 --- a/glib/dbus-gvalue-utils.c +++ b/glib/dbus-gvalue-utils.c @@ -21,8 +21,10 @@ * */ +#include #include "dbus/dbus-glib.h" #include "dbus-gvalue-utils.h" +#include "dbus-gtest.h" #include #include #include @@ -572,6 +574,9 @@ gvalue_from_ptrarray_value (GValue *value, gpointer instance) { switch (g_type_fundamental (G_VALUE_TYPE (value))) { + case G_TYPE_STRING: + g_value_set_string (value, instance); + break; case G_TYPE_POINTER: g_value_set_pointer (value, instance); break; @@ -593,6 +598,9 @@ ptrarray_value_from_gvalue (const GValue *value) { switch (g_type_fundamental (G_VALUE_TYPE (value))) { + case G_TYPE_STRING: + return (gpointer) g_value_get_string (value); + break; case G_TYPE_POINTER: return g_value_get_pointer (value); break; @@ -658,12 +666,106 @@ ptrarray_copy (GType type, gpointer src) return new; } +static void +ptrarray_append (DBusGTypeSpecializedAppendContext *ctx, const GValue *value) +{ + GPtrArray *array; + + array = g_value_get_boxed (ctx->val); + + g_ptr_array_add (array, ptrarray_value_from_gvalue (value)); +} + static void ptrarray_free (GType type, gpointer val) { - GArray *array; + GPtrArray *array; array = val; - g_array_free (array, TRUE); + g_ptr_array_free (array, TRUE); +} + +static gpointer +slist_constructor (GType type) +{ + return NULL; +} + +static void +slist_iterator (GType list_type, + gpointer instance, + DBusGTypeSpecializedCollectionIterator iterator, + gpointer user_data) +{ + GSList *slist; + GType elt_gtype; + + slist = instance; + + elt_gtype = dbus_g_type_get_collection_specialization (list_type); + + while (slist != NULL) + { + GValue val = {0, }; + g_value_init (&val, elt_gtype); + gvalue_from_ptrarray_value (&val, slist->data); + iterator (&val, user_data); + } +} + +static void +slist_copy_elt (const GValue *val, gpointer user_data) +{ + GSList *dest = user_data; + GValue val_copy = {0, }; + + g_value_init (&val_copy, G_VALUE_TYPE (val)); + g_value_copy (val, &val_copy); + + g_slist_append (dest, ptrarray_value_from_gvalue (&val_copy)); +} + +static gpointer +slist_copy (GType type, gpointer src) +{ + GSList *new; + GValue slist_val = {0, }; + + g_value_init (&slist_val, type); + g_value_set_static_boxed (&slist_val, src); + + new = slist_constructor (type); + dbus_g_type_collection_value_iterate (&slist_val, slist_copy_elt, new); + + return new; +} + +static void +slist_append (DBusGTypeSpecializedAppendContext *ctx, const GValue *value) +{ + GSList *list; + + list = g_value_get_boxed (ctx->val); + list = g_slist_prepend (list, ptrarray_value_from_gvalue (value)); + g_value_set_static_boxed (ctx->val, list); +} + +static void +slist_end_append (DBusGTypeSpecializedAppendContext *ctx) +{ + GSList *list; + + list = g_value_get_boxed (ctx->val); + list = g_slist_reverse (list); + + g_value_set_static_boxed (ctx->val, list); +} + +static void +slist_free (GType type, gpointer val) +{ + GSList *list; + list = val; + g_slist_free (list); } void @@ -674,11 +776,10 @@ dbus_g_type_specialized_builtins_init (void) array_constructor, array_free, array_copy, - NULL, - NULL, - NULL }, array_fixed_accessor, + NULL, + NULL, NULL }; @@ -689,16 +790,29 @@ dbus_g_type_specialized_builtins_init (void) ptrarray_constructor, ptrarray_free, ptrarray_copy, - NULL, - NULL, - NULL }, NULL, - ptrarray_iterator + ptrarray_iterator, + ptrarray_append, + NULL, }; dbus_g_type_register_collection ("GPtrArray", &ptrarray_vtable, 0); + static const DBusGTypeSpecializedCollectionVtable slist_vtable = { + { + slist_constructor, + slist_free, + slist_copy, + }, + NULL, + slist_iterator, + slist_append, + slist_end_append, + }; + + dbus_g_type_register_collection ("GSList", &slist_vtable, 0); + static const DBusGTypeSpecializedMapVtable hashtable_vtable = { { hashtable_constructor, @@ -713,3 +827,132 @@ dbus_g_type_specialized_builtins_init (void) dbus_g_type_register_map ("GHashTable", &hashtable_vtable, 0); } + +#ifdef DBUS_BUILD_TESTS + +typedef struct +{ + gboolean seen_foo; + gboolean seen_baz; +} TestSpecializedHashData; + +static void +test_specialized_hash (const GValue *key, const GValue *val, gpointer user_data) +{ + TestSpecializedHashData *data = user_data; + + g_assert (G_VALUE_HOLDS_STRING (key)); + g_assert (G_VALUE_HOLDS_STRING (val)); + + if (!strcmp (g_value_get_string (key), "foo")) + { + data->seen_foo = TRUE; + g_assert (!strcmp (g_value_get_string (val), "bar")); + } + else if (!strcmp (g_value_get_string (key), "baz")) + { + data->seen_baz = TRUE; + g_assert (!strcmp (g_value_get_string (val), "moo")); + } + else + { + g_assert_not_reached (); + } +} + +gboolean +_dbus_gvalue_utils_test (const char *datadir) +{ + GType type; + + dbus_g_type_specialized_init (); + dbus_g_type_specialized_builtins_init (); + + type = dbus_g_type_get_collection ("GArray", G_TYPE_UINT); + g_assert (dbus_g_type_is_collection (type)); + g_assert (dbus_g_type_get_collection_specialization (type) == G_TYPE_UINT); + { + GArray *instance; + + instance = dbus_g_type_specialized_construct (type); + + g_assert (instance->len == 0); + + g_array_free (instance, TRUE); + } + + type = dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_STRING); + 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_STRING); + { + GHashTable *instance; + GValue val = { 0, }; + TestSpecializedHashData hashdata; + + instance = dbus_g_type_specialized_construct (type); + + g_assert (g_hash_table_size (instance) == 0); + g_hash_table_insert (instance, g_strdup ("foo"), g_strdup ("bar")); + g_hash_table_insert (instance, g_strdup ("baz"), g_strdup ("moo")); + g_assert (g_hash_table_size (instance) == 2); + + g_value_init (&val, type); + g_value_set_boxed_take_ownership (&val, instance); + hashdata.seen_foo = FALSE; + hashdata.seen_baz = FALSE; + dbus_g_type_map_value_iterate (&val, + test_specialized_hash, + &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); + { + GPtrArray *instance; + DBusGTypeSpecializedAppendContext ctx; + GValue val = {0, }; + GValue eltval = {0, }; + + instance = dbus_g_type_specialized_construct (type); + + g_assert (instance->len == 0); + + g_value_init (&val, type); + g_value_set_boxed_take_ownership (&val, instance); + + dbus_g_type_specialized_collection_init_append (&val, &ctx); + + g_value_init (&eltval, G_TYPE_STRING); + g_value_set_static_string (&eltval, "foo"); + dbus_g_type_specialized_collection_append (&ctx, &eltval); + + g_value_reset (&eltval); + g_value_set_static_string (&eltval, "bar"); + dbus_g_type_specialized_collection_append (&ctx, &eltval); + + g_value_reset (&eltval); + g_value_set_static_string (&eltval, "baz"); + dbus_g_type_specialized_collection_append (&ctx, &eltval); + + dbus_g_type_specialized_collection_end_append (&ctx); + + g_assert (instance->len == 3); + + g_assert (!strcmp ("foo", g_ptr_array_index (instance, 0))); + g_assert (!strcmp ("bar", g_ptr_array_index (instance, 1))); + g_assert (!strcmp ("baz", g_ptr_array_index (instance, 2))); + + g_value_unset (&val); + } + + return TRUE; +} + +#endif /* DBUS_BUILD_TESTS */ -- cgit