[glib-controller] Add GPtrArrayController class



commit 0b160de6f0348ded9bcfcc5e7df207c2a2f32aa2
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Fri Apr 30 12:11:50 2010 +0100

    Add GPtrArrayController class
    
    A simple copy of GArrayController for GPtrArray.

 Makefile.am                           |    2 +
 glib-controller/gcontrollertypes.h    |   12 ++
 glib-controller/glib-controller.h     |    1 +
 glib-controller/gptrarraycontroller.c |  188 +++++++++++++++++++++++++++++++++
 glib-controller/gptrarraycontroller.h |   49 +++++++++
 5 files changed, 252 insertions(+), 0 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 9ef1fd2..525e828 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -39,6 +39,7 @@ source_c = \
 	glib-controller/gcontrollerreference.c	\
 	glib-controller/gcontroller.c 		\
 	glib-controller/ghashcontroller.c	\
+	glib-controller/gptrarraycontroller.c	\
 	$(NULL)
 
 source_public_h = \
@@ -47,6 +48,7 @@ source_public_h = \
 	glib-controller/gcontrollertypes.h	\
 	glib-controller/gcontroller.h		\
 	glib-controller/ghashcontroller.h	\
+	glib-controller/gptrarraycontroller.h	\
 	$(NULL)
 
 source_h = \
diff --git a/glib-controller/gcontrollertypes.h b/glib-controller/gcontrollertypes.h
index 446dd0d..cd663e8 100644
--- a/glib-controller/gcontrollertypes.h
+++ b/glib-controller/gcontrollertypes.h
@@ -45,6 +45,18 @@ typedef struct _GControllerReference    GControllerReference;
  */
 typedef struct _GArrayController        GArrayController;
 
+#define G_TYPE_PTR_ARRAY_CONTROLLER     (g_ptr_array_controller_get_type ())
+#define G_PTR_ARRAY_CONTROLLER(obj)     (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_PTR_ARRAY_CONTROLLER, GPtrArrayController))
+#define G_IS_PTR_ARRAY_CONTROLLER(obj)  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_PTR_ARRAY_CONTROLLER))
+
+/**
+ * GPtrArrayController:
+ *
+ * The <structname>GPtrArrayController</structname> structure contains
+ * only private data and should be accessed using the provided API
+ */
+typedef struct _GPtrArrayController     GPtrArrayController;
+
 #define G_TYPE_HASH_CONTROLLER          (g_hash_controller_get_type ())
 #define G_HASH_CONTROLLER(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), G_TYPE_HASH_CONTROLLER, GHashController))
 #define G_IS_HASH_CONTROLLER(obj)       (G_TYPE_CHECK_INSTANCE_TYPE ((obj), G_TYPE_HASH_CONTROLLER))
diff --git a/glib-controller/glib-controller.h b/glib-controller/glib-controller.h
index 5325a94..566277e 100644
--- a/glib-controller/glib-controller.h
+++ b/glib-controller/glib-controller.h
@@ -9,6 +9,7 @@
 
 #include "garraycontroller.h"
 #include "ghashcontroller.h"
+#include "gptrarraycontroller.h"
 
 #include "gcontrollerversion.h"
 #include "gcontrollerenumtypes.h"
diff --git a/glib-controller/gptrarraycontroller.c b/glib-controller/gptrarraycontroller.c
new file mode 100644
index 0000000..14e1be7
--- /dev/null
+++ b/glib-controller/gptrarraycontroller.c
@@ -0,0 +1,188 @@
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "gptrarraycontroller.h"
+
+/**
+ * SECTION:g-array-controller
+ * @Title: GPtrArrayController
+ * @Short_Description: A controller for GLib arrays
+ * @See_Also: #GController, #GPtrArray
+ *
+ * The #GPtrArrayController object is a #GController implementation aimed
+ * at controlling #GPtrArray data structures
+ */
+
+struct _GPtrArrayControllerPrivate
+{
+  GPtrArray *array;
+};
+
+enum
+{
+  PROP_0,
+
+  PROP_ARRAY
+};
+
+G_DEFINE_TYPE (GPtrArrayController, g_ptr_array_controller, G_TYPE_CONTROLLER);
+
+static GType
+get_index_type (GController *controller)
+{
+  /* GPtrArray indexes are unsigned integers */
+  return G_TYPE_UINT;
+}
+
+static void
+g_ptr_array_controller_set_property (GObject      *gobject,
+                                     guint         prop_id,
+                                     const GValue *value,
+                                     GParamSpec   *pspec)
+{
+  GPtrArrayController *self = G_PTR_ARRAY_CONTROLLER (gobject);
+
+  switch (prop_id)
+    {
+    case PROP_ARRAY:
+      g_ptr_array_controller_set_array (self, g_value_get_boxed (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+g_ptr_array_controller_get_property (GObject    *gobject,
+                                     guint       prop_id,
+                                     GValue     *value,
+                                     GParamSpec *pspec)
+{
+  GPtrArrayControllerPrivate *priv = G_PTR_ARRAY_CONTROLLER (gobject)->priv;
+
+  switch (prop_id)
+    {
+    case PROP_ARRAY:
+      g_value_set_boxed (value, priv->array);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+g_ptr_array_controller_finalize (GObject *gobject)
+{
+  GPtrArrayControllerPrivate *priv = G_PTR_ARRAY_CONTROLLER (gobject)->priv;
+
+  if (priv->array)
+    g_ptr_array_unref (priv->array);
+
+  G_OBJECT_CLASS (g_ptr_array_controller_parent_class)->finalize (gobject);
+}
+
+static void
+g_ptr_array_controller_class_init (GPtrArrayControllerClass *klass)
+{
+  GControllerClass *controller_class = G_CONTROLLER_CLASS (klass);
+  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+  GParamSpec *pspec;
+
+  controller_class->get_index_type = get_index_type;
+
+  gobject_class->set_property = g_ptr_array_controller_set_property;
+  gobject_class->get_property = g_ptr_array_controller_get_property;
+  gobject_class->finalize = g_ptr_array_controller_finalize;
+
+  /**
+   * GPtrArrayController:array:
+   *
+   * The #GPtrArray to be controlled by a #GPtrArrayController instance
+   */
+  pspec = g_param_spec_boxed ("array",
+                              "Array",
+                              "The GPtrArray to be controlled",
+                              G_TYPE_PTR_ARRAY,
+                              G_PARAM_READWRITE |
+                              G_PARAM_CONSTRUCT |
+                              G_PARAM_STATIC_STRINGS);
+  g_object_class_install_property (gobject_class, PROP_ARRAY, pspec);
+
+  g_type_class_add_private (klass, sizeof (GPtrArrayControllerPrivate));
+}
+
+static void
+g_ptr_array_controller_init (GPtrArrayController *self)
+{
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+                                            G_TYPE_PTR_ARRAY_CONTROLLER,
+                                            GPtrArrayControllerPrivate);
+}
+
+/**
+ * g_ptr_array_controller_new:
+ * @array: (allow-none): a #GPtrArray or %NULL
+ *
+ * Creates a new #GPtrArrayController controlling the @array
+ *
+ * Return value: the newly created #GPtrArrayController
+ */
+GController *
+g_ptr_array_controller_new (GPtrArray *array)
+{
+  return g_object_new (G_TYPE_PTR_ARRAY_CONTROLLER,
+                       "array", array,
+                       NULL);
+}
+
+/**
+ * g_ptr_array_controller_set_array:
+ * @controller: a #GPtrArrayController
+ * @array: (allow-none): a #GPtrArray or %NULL
+ *
+ * Sets the #GPtrArray to be controlled by @controller
+ *
+ * The #GPtrArrayController will take a reference on the passed #GPtrArray
+ * which will be released when the #GPtrArrayController is disposed or
+ * when unsetting the controlled array by passing %NULL to this
+ * function
+ */
+void
+g_ptr_array_controller_set_array (GPtrArrayController *controller,
+                                  GPtrArray           *array)
+{
+  g_return_if_fail (G_IS_PTR_ARRAY_CONTROLLER (controller));
+
+  if (controller->priv->array == array)
+    return;
+
+  if (controller->priv->array != NULL)
+    g_ptr_array_unref (controller->priv->array);
+
+  controller->priv->array = array;
+  if (controller->priv->array != NULL)
+    g_ptr_array_ref (controller->priv->array);
+
+  g_object_notify (G_OBJECT (controller), "array");
+}
+
+/**
+ * g_ptr_array_controller_get_array:
+ * @controller: a #GPtrArrayController
+ *
+ * Retrieves the #GPtrArray controlled by @controller
+ *
+ * Return value: (transfer none): a pointer to a #GPtrArray, or %NULL
+ */
+GPtrArray *
+g_ptr_array_controller_get_array (GPtrArrayController *controller)
+{
+  g_return_val_if_fail (G_IS_PTR_ARRAY_CONTROLLER (controller), NULL);
+
+  return controller->priv->array;
+}
diff --git a/glib-controller/gptrarraycontroller.h b/glib-controller/gptrarraycontroller.h
new file mode 100644
index 0000000..ee1a716
--- /dev/null
+++ b/glib-controller/gptrarraycontroller.h
@@ -0,0 +1,49 @@
+#if !defined(__GLIB_CONTROLLER_H_INSIDE__) && !defined(GLIB_CONTROLLER_COMPILATION)
+#error "Only <glib-controller.h> can be included directly."
+#endif
+
+#ifndef __G_PTR_ARRAY_CONTROLLER_H__
+#define __G_PTR_ARRAY_CONTROLLER_H__
+
+#include <glib-controller/gcontroller.h>
+
+G_BEGIN_DECLS
+
+#define G_PTR_ARRAY_CONTROLLER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), G_TYPE_PTR_ARRAY_CONTROLLER, GPtrArrayControllerClass))
+#define G_IS_PTR_ARRAY_CONTROLLER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), G_TYPE_PTR_ARRAY_CONTROLLER))
+#define G_PTR_ARRAY_CONTROLLER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), G_TYPE_PTR_ARRAY_CONTROLLER, GPtrArrayControllerClass))
+
+typedef struct _GPtrArrayControllerPrivate      GPtrArrayControllerPrivate;
+typedef struct _GPtrArrayControllerClass        GPtrArrayControllerClass;
+
+struct _GPtrArrayController
+{
+  /*< private >*/
+  GController parent_instance;
+
+  GPtrArrayControllerPrivate *priv;
+};
+
+/**
+ * GPtrArrayControllerClass:
+ *
+ * The <structname>GPtrArrayControllerClass</structname> structure contains
+ * only private data.
+ */
+struct _GPtrArrayControllerClass
+{
+  /*< private >*/
+  GControllerClass parent_class;
+};
+
+GType g_ptr_array_controller_get_type (void) G_GNUC_CONST;
+
+GController *g_ptr_array_controller_new       (GPtrArray           *array);
+
+void         g_ptr_array_controller_set_array (GPtrArrayController *controller,
+                                               GPtrArray           *array);
+GPtrArray *  g_ptr_array_controller_get_array (GPtrArrayController *controller);
+
+G_END_DECLS
+
+#endif /* __G_PTR_ARRAY_CONTROLLER_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]