From: Nick Bowler Date: Tue, 22 Mar 2011 00:11:55 +0000 (-0400) Subject: uobject: Add a helper function to load objects from packages. X-Git-Url: https://git.draconx.ca/gitweb/upkg.git/commitdiff_plain/559bcf15ef66d20d7e8527c690c0b4e5a66b6381?hp=bb5f99272ae78a2ddc07e0a54025baab39d84c49 uobject: Add a helper function to load objects from packages. --- diff --git a/src/uobject/uobject.c b/src/uobject/uobject.c index 581ac07..7b0a79e 100644 --- a/src/uobject/uobject.c +++ b/src/uobject/uobject.c @@ -23,6 +23,7 @@ #include #include +#include #include "upkg.h" #include "pack.h" @@ -216,6 +217,27 @@ int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx) return rc; } +GObject *u_object_new_from_package(struct upkg *upkg, unsigned long idx) +{ + const struct upkg_export *export; + const char *class, *package; + GObject *obj = NULL; + GType type; + + class = upkg_export_class(upkg, idx, &package); + + type = u_object_module_get_class(package, class); + if (type) { + obj = g_object_new(type, NULL); + if (u_object_deserialize(obj, upkg, idx) != 0) { + g_object_unref(obj); + return NULL; + } + } + + return obj; +} + static void u_object_init(UObject *o) { o->pkg = NULL; diff --git a/src/uobject/uobject.h b/src/uobject/uobject.h index 7319ee6..0d0d45c 100644 --- a/src/uobject/uobject.h +++ b/src/uobject/uobject.h @@ -54,4 +54,6 @@ GType u_object_get_type(void); int u_object_deserialize(GObject *obj, struct upkg *pkg, unsigned long idx); +GObject *u_object_new_from_package(struct upkg *pkg, unsigned long idx); + #endif diff --git a/src/upkg.c b/src/upkg.c index 5766e8b..c13a867 100644 --- a/src/upkg.c +++ b/src/upkg.c @@ -279,19 +279,12 @@ static int object_info(GTypeModule *pkg, unsigned long idx) if (verbose >= 1) { GParamSpec **props; GObject *obj; - GType type; unsigned n; - type = u_object_module_get_class(package, class); - if (!type) { - printf(" (unknown class)\n"); - return 0; - } - - obj = g_object_new(type, NULL); - - if (u_object_deserialize(obj, upkg, idx) != 0) { - g_object_unref(obj); + obj = u_object_new_from_package(upkg, idx); + if (!obj) { + fprintf(stderr, "%s: failed to load object.\n", + progname); return -1; }