summaryrefslogtreecommitdiff
path: root/libdevkit-power
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2008-08-28 17:28:26 +0100
committerRichard Hughes <richard@hughsie.com>2008-08-28 17:28:26 +0100
commit38a050b01f1362aebbbfa9b303b5c99cb938d5bf (patch)
treed6945ce8d4df5f7a56ce1dbf95dbd62b53f39419 /libdevkit-power
parent7fdcc278dc80aa0bb07c834e0d8de5787852ecb2 (diff)
switch from using GPtrArray to EggObjList as this provides us with auto array cleanup for different objects
Diffstat (limited to 'libdevkit-power')
-rw-r--r--libdevkit-power/Makefile.am2
-rw-r--r--libdevkit-power/dkp-client-device.c33
-rw-r--r--libdevkit-power/dkp-client-device.h5
-rw-r--r--libdevkit-power/egg-obj-list.c252
-rw-r--r--libdevkit-power/egg-obj-list.h71
5 files changed, 349 insertions, 14 deletions
diff --git a/libdevkit-power/Makefile.am b/libdevkit-power/Makefile.am
index 6d776d2..8067744 100644
--- a/libdevkit-power/Makefile.am
+++ b/libdevkit-power/Makefile.am
@@ -14,6 +14,8 @@ libdevkit_power_la_SOURCES = \
egg-debug.h \
egg-string.c \
egg-string.h \
+ egg-obj-list.c \
+ egg-obj-list.h \
dkp-client.c \
dkp-client.h \
dkp-client-device.c \
diff --git a/libdevkit-power/dkp-client-device.c b/libdevkit-power/dkp-client-device.c
index 2374e37..535cd86 100644
--- a/libdevkit-power/dkp-client-device.c
+++ b/libdevkit-power/dkp-client-device.c
@@ -27,6 +27,8 @@
#include <dbus/dbus-glib.h>
#include "egg-debug.h"
+#include "egg-obj-list.h"
+
#include "dkp-client-device.h"
#include "dkp-object.h"
#include "dkp-stats-obj.h"
@@ -199,7 +201,7 @@ static gboolean
dkp_client_device_print_history (const DkpClientDevice *device, const gchar *type)
{
guint i;
- GPtrArray *array;
+ EggObjList *array;
DkpHistoryObj *obj;
gboolean ret = FALSE;
@@ -211,11 +213,10 @@ dkp_client_device_print_history (const DkpClientDevice *device, const gchar *typ
/* pretty print */
g_print (" Statistics (%s):\n", type);
for (i=0; i<array->len; i++) {
- obj = (DkpHistoryObj *) g_ptr_array_index (array, i);
+ obj = (DkpHistoryObj *) egg_obj_list_index (array, i);
g_print (" %i\t%.3f\t%s\n", obj->time, obj->value, dkp_device_state_to_text (obj->state));
}
- g_ptr_array_foreach (array, (GFunc) dkp_history_obj_free, NULL);
- g_ptr_array_free (array, TRUE);
+ g_object_unref (array);
ret = TRUE;
out:
return ret;
@@ -268,7 +269,7 @@ out:
*
* Returns an array of %DkpHistoryObj's
**/
-GPtrArray *
+EggObjList *
dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type, guint timespec)
{
GError *error = NULL;
@@ -278,7 +279,7 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
GValue *gv;
guint i;
DkpHistoryObj *obj;
- GPtrArray *array = NULL;
+ EggObjList *array = NULL;
gboolean ret;
g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
@@ -309,7 +310,10 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
goto out;
/* convert */
- array = g_ptr_array_sized_new (gvalue_ptr_array->len);
+ array = egg_obj_list_new ();
+ egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_history_obj_copy);
+ egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_history_obj_free);
+
for (i=0; i<gvalue_ptr_array->len; i++) {
gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
obj = dkp_history_obj_new ();
@@ -325,7 +329,8 @@ dkp_client_device_get_history (const DkpClientDevice *device, const gchar *type,
gv = g_value_array_get_nth (gva, 2);
obj->state = dkp_device_state_from_text (g_value_get_string (gv));
g_value_unset (gv);
- g_ptr_array_add (array, obj);
+ egg_obj_list_add (array, obj);
+ dkp_history_obj_free (obj);
g_value_array_free (gva);
}
@@ -340,7 +345,7 @@ out:
*
* Returns an array of %DkpStatsObj's
**/
-GPtrArray *
+EggObjList *
dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *type)
{
GError *error = NULL;
@@ -350,7 +355,7 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
GValue *gv;
guint i;
DkpStatsObj *obj;
- GPtrArray *array = NULL;
+ EggObjList *array = NULL;
gboolean ret;
g_return_val_if_fail (DKP_IS_CLIENT_DEVICE (device), FALSE);
@@ -379,7 +384,10 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
goto out;
/* convert */
- array = g_ptr_array_sized_new (gvalue_ptr_array->len);
+ array = egg_obj_list_new ();
+ egg_obj_list_set_copy (array, (EggObjListCopyFunc) dkp_stats_obj_copy);
+ egg_obj_list_set_free (array, (EggObjListFreeFunc) dkp_stats_obj_free);
+
for (i=0; i<gvalue_ptr_array->len; i++) {
gva = (GValueArray *) g_ptr_array_index (gvalue_ptr_array, i);
obj = dkp_stats_obj_new ();
@@ -392,7 +400,8 @@ dkp_client_device_get_statistics (const DkpClientDevice *device, const gchar *ty
obj->accuracy = g_value_get_double (gv);
g_value_unset (gv);
/* 2 */
- g_ptr_array_add (array, obj);
+ egg_obj_list_add (array, obj);
+ dkp_stats_obj_free (obj);
g_value_array_free (gva);
}
diff --git a/libdevkit-power/dkp-client-device.h b/libdevkit-power/dkp-client-device.h
index 3dceebc..9903fc2 100644
--- a/libdevkit-power/dkp-client-device.h
+++ b/libdevkit-power/dkp-client-device.h
@@ -25,6 +25,7 @@
#include <glib-object.h>
#include <dkp-enum.h>
#include <dkp-object.h>
+#include "egg-obj-list.h"
G_BEGIN_DECLS
@@ -62,10 +63,10 @@ gboolean dkp_client_device_set_object_path (DkpClientDevice *device,
gboolean dkp_client_device_print (const DkpClientDevice *device);
gboolean dkp_client_device_refresh (DkpClientDevice *device);
-GPtrArray *dkp_client_device_get_history (const DkpClientDevice *device,
+EggObjList *dkp_client_device_get_history (const DkpClientDevice *device,
const gchar *type,
guint timespec);
-GPtrArray *dkp_client_device_get_statistics (const DkpClientDevice *device,
+EggObjList *dkp_client_device_get_statistics (const DkpClientDevice *device,
const gchar *type);
G_END_DECLS
diff --git a/libdevkit-power/egg-obj-list.c b/libdevkit-power/egg-obj-list.c
new file mode 100644
index 0000000..cd12df2
--- /dev/null
+++ b/libdevkit-power/egg-obj-list.c
@@ -0,0 +1,252 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <glib/gi18n.h>
+#include <glib.h>
+
+#include "egg-obj-list.h"
+
+#define EGG_OBJ_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_OBJ_LIST, EggObjListPrivate))
+
+struct EggObjListPrivate
+{
+ EggObjListNewFunc func_new;
+ EggObjListCopyFunc func_copy;
+ EggObjListFreeFunc func_free;
+ GPtrArray *array;
+};
+
+G_DEFINE_TYPE (EggObjList, egg_obj_list, G_TYPE_OBJECT)
+
+/**
+ * egg_obj_list_set_new:
+ * @list: a valid #EggObjList instance
+ * @func: typedef'd function
+ *
+ * Adds a creation func
+ **/
+void
+egg_obj_list_set_new (EggObjList *list, EggObjListNewFunc func)
+{
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ list->priv->func_new = func;
+}
+
+/**
+ * egg_obj_list_set_copy:
+ * @list: a valid #EggObjList instance
+ * @func: typedef'd function
+ *
+ * Adds a copy func
+ **/
+void
+egg_obj_list_set_copy (EggObjList *list, EggObjListCopyFunc func)
+{
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ list->priv->func_copy = func;
+}
+
+/**
+ * egg_obj_list_set_free:
+ * @list: a valid #EggObjList instance
+ * @func: typedef'd function
+ *
+ * Adds a free func
+ **/
+void
+egg_obj_list_set_free (EggObjList *list, EggObjListFreeFunc func)
+{
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ list->priv->func_free = func;
+}
+
+/**
+ * egg_obj_list_clear:
+ * @list: a valid #EggObjList instance
+ *
+ * Clears the package list
+ **/
+void
+egg_obj_list_clear (EggObjList *list)
+{
+ guint i;
+ gpointer obj;
+ GPtrArray *array;
+ EggObjListFreeFunc func_free;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+
+ array = list->priv->array;
+ func_free = list->priv->func_free;
+ for (i=0; i<array->len; i++) {
+ obj = g_ptr_array_index (array, i);
+ if (func_free != NULL)
+ func_free (obj);
+ g_ptr_array_remove (array, obj);
+ }
+ list->len = 0;
+}
+
+/**
+ * egg_obj_list_add:
+ * @list: a valid #EggObjList instance
+ * @obj: a valid #gpointer object
+ *
+ * Adds a copy of the object to the list
+ **/
+void
+egg_obj_list_add (EggObjList *list, const gpointer obj)
+{
+ gpointer obj_new;
+
+ g_return_if_fail (EGG_IS_OBJ_LIST (list));
+ g_return_if_fail (obj != NULL);
+ g_return_if_fail (list->priv->func_copy != NULL);
+
+ /* TODO: are we already in the list? */
+ obj_new = list->priv->func_copy (obj);
+ g_ptr_array_add (list->priv->array, obj_new);
+ list->len = list->priv->array->len;
+}
+
+/**
+ * egg_obj_list_index:
+ * @list: a valid #EggObjList instance
+ * @id: A #EggPackageId of the item to match
+ *
+ * Gets an object from the list
+ **/
+const gpointer
+egg_obj_list_index (EggObjList *list, guint i)
+{
+ gpointer obj;
+
+ g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);
+
+ obj = g_ptr_array_index (list->priv->array, i);
+ return obj;
+}
+
+/**
+ * egg_obj_list_finalize:
+ * @object: a valid #EggObjList instance
+ **/
+static void
+egg_obj_list_finalize (GObject *object)
+{
+ EggObjListFreeFunc func_free;
+ gpointer obj;
+ guint i;
+ EggObjList *list;
+ GPtrArray *array;
+ g_return_if_fail (EGG_IS_OBJ_LIST (object));
+ list = EGG_OBJ_LIST (object);
+
+ array = list->priv->array;
+ func_free = list->priv->func_free;
+ for (i=0; i<array->len; i++) {
+ obj = g_ptr_array_index (array, i);
+ if (func_free != NULL)
+ func_free (obj);
+ }
+ g_ptr_array_free (array, TRUE);
+
+ G_OBJECT_CLASS (egg_obj_list_parent_class)->finalize (object);
+}
+
+/**
+ * egg_obj_list_class_init:
+ * @klass: a valid #EggObjListClass instance
+ **/
+static void
+egg_obj_list_class_init (EggObjListClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ object_class->finalize = egg_obj_list_finalize;
+ g_type_class_add_private (klass, sizeof (EggObjListPrivate));
+}
+
+/**
+ * egg_obj_list_init:
+ * @list: a valid #EggObjList instance
+ *
+ * Initializes the obj_list class.
+ **/
+static void
+egg_obj_list_init (EggObjList *list)
+{
+ list->priv = EGG_OBJ_LIST_GET_PRIVATE (list);
+ list->priv->func_new = NULL;
+ list->priv->func_copy = NULL;
+ list->priv->func_free = NULL;
+ list->priv->array = g_ptr_array_new ();
+ list->len = list->priv->array->len;
+}
+
+/**
+ * egg_obj_list_new:
+ *
+ * Return value: A new list class instance.
+ **/
+EggObjList *
+egg_obj_list_new (void)
+{
+ EggObjList *list;
+ list = g_object_new (EGG_TYPE_OBJ_LIST, NULL);
+ return EGG_OBJ_LIST (list);
+}
+
+/***************************************************************************
+ *** MAKE CHECK TESTS ***
+ ***************************************************************************/
+#ifdef EGG_BUILD_TESTS
+#include <libselftest.h>
+
+void
+libst_obj_list (LibSelfTest *test)
+{
+ EggObjList *list;
+ gchar *text;
+ gint value;
+
+ if (libst_start (test, "EggObjList", CLASS_AUTO) == FALSE) {
+ return;
+ }
+
+ /************************************************************/
+ libst_title (test, "get an instance");
+ list = egg_obj_list_new ();
+ if (list != NULL) {
+ libst_success (test, NULL);
+ } else {
+ libst_failed (test, NULL);
+ }
+
+ g_object_unref (list);
+
+ libst_end (test);
+}
+#endif
+
diff --git a/libdevkit-power/egg-obj-list.h b/libdevkit-power/egg-obj-list.h
new file mode 100644
index 0000000..d936b57
--- /dev/null
+++ b/libdevkit-power/egg-obj-list.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __EGG_OBJ_LIST_H
+#define __EGG_OBJ_LIST_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define EGG_TYPE_OBJ_LIST (egg_obj_list_get_type ())
+#define EGG_OBJ_LIST(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EGG_TYPE_OBJ_LIST, EggObjList))
+#define EGG_OBJ_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EGG_TYPE_OBJ_LIST, EggObjListClass))
+#define EGG_IS_OBJ_LIST(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EGG_TYPE_OBJ_LIST))
+#define EGG_IS_OBJ_LIST_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EGG_TYPE_OBJ_LIST))
+#define EGG_OBJ_LIST_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EGG_TYPE_OBJ_LIST, EggObjListClass))
+
+typedef struct EggObjListPrivate EggObjListPrivate;
+
+typedef struct
+{
+ GObject parent;
+ EggObjListPrivate *priv;
+ guint len;
+} EggObjList;
+
+typedef struct
+{
+ GObjectClass parent_class;
+} EggObjListClass;
+
+typedef gpointer (*EggObjListNewFunc) (void);
+typedef gpointer (*EggObjListCopyFunc) (const gpointer data);
+typedef void (*EggObjListFreeFunc) (gpointer data);
+
+GType egg_obj_list_get_type (void) G_GNUC_CONST;
+EggObjList *egg_obj_list_new (void);
+
+void egg_obj_list_set_new (EggObjList *list,
+ EggObjListNewFunc func);
+void egg_obj_list_set_copy (EggObjList *list,
+ EggObjListCopyFunc func);
+void egg_obj_list_set_free (EggObjList *list,
+ EggObjListFreeFunc func);
+void egg_obj_list_clear (EggObjList *list);
+void egg_obj_list_add (EggObjList *list,
+ const gpointer data);
+const gpointer egg_obj_list_index (EggObjList *list,
+ guint index);
+
+G_END_DECLS
+
+#endif /* __EGG_OBJ_LIST_H */