[gimp] app: Add GimpIdTable utility class



commit 9fefa22efe70e484fc7c92708ed8efe023e4d219
Author: Martin Nordholts <martinn src gnome org>
Date:   Wed May 4 07:24:23 2011 +0200

    app: Add GimpIdTable utility class
    
    Not written in Vala... ;)

 app/core/Makefile.am         |    2 +
 app/core/core-types.h        |    1 +
 app/core/gimpidtable.c       |  224 ++++++++++++++++++++++++++++++++++++++++
 app/core/gimpidtable.h       |   68 ++++++++++++
 app/tests/.gitignore         |    1 +
 app/tests/Makefile.am        |    1 +
 app/tests/test-gimpidtable.c |  232 ++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 529 insertions(+), 0 deletions(-)
---
diff --git a/app/core/Makefile.am b/app/core/Makefile.am
index 3a8ee12..b1d620b 100644
--- a/app/core/Makefile.am
+++ b/app/core/Makefile.am
@@ -205,6 +205,8 @@ libappcore_a_sources = \
 	gimpguide.h				\
 	gimpguideundo.c				\
 	gimpguideundo.h				\
+	gimpidtable.c				\
+	gimpidtable.h				\
 	gimpimage.c				\
 	gimpimage.h				\
 	gimpimage-arrange.c			\
diff --git a/app/core/core-types.h b/app/core/core-types.h
index a7fd2eb..cb8e141 100644
--- a/app/core/core-types.h
+++ b/app/core/core-types.h
@@ -153,6 +153,7 @@ typedef struct _GimpUndoAccumulator   GimpUndoAccumulator;
 typedef struct _GimpBuffer          GimpBuffer;
 typedef struct _GimpEnvironTable    GimpEnvironTable;
 typedef struct _GimpGuide           GimpGuide;
+typedef struct _GimpIdTable         GimpIdTable;
 typedef struct _GimpImageMap        GimpImageMap;
 typedef struct _GimpImageMapConfig  GimpImageMapConfig;
 typedef struct _GimpImagefile       GimpImagefile;
diff --git a/app/core/gimpidtable.c b/app/core/gimpidtable.c
new file mode 100644
index 0000000..ab3175d
--- /dev/null
+++ b/app/core/gimpidtable.c
@@ -0,0 +1,224 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpidtable.c
+ * Copyright (C) 2011 Martin Nordholts <martinn src gnome org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "core-types.h"
+
+#include "gimpidtable.h"
+#include "gimp-utils.h"
+
+
+#define GIMP_ID_TABLE_START_ID 1
+#define GIMP_ID_TABLE_END_ID   G_MAXINT
+
+
+struct _GimpIdTablePriv
+{
+  GHashTable *id_table;
+  gint        next_id;
+};
+
+
+static void    gimp_id_table_finalize    (GObject    *object);
+static gint64  gimp_id_table_get_memsize (GimpObject *object,
+                                          gint64     *gui_size);
+
+
+G_DEFINE_TYPE (GimpIdTable, gimp_id_table, GIMP_TYPE_OBJECT)
+
+#define parent_class gimp_id_table_parent_class
+
+
+static void
+gimp_id_table_class_init (GimpIdTableClass *klass)
+{
+  GObjectClass     *object_class        = G_OBJECT_CLASS (klass);
+  GimpObjectClass  *gimp_object_class   = GIMP_OBJECT_CLASS (klass);
+  GimpIdTableClass *gimp_id_table_class = GIMP_ID_TABLE_CLASS (klass);
+
+  object_class->finalize         = gimp_id_table_finalize;
+
+  gimp_object_class->get_memsize = gimp_id_table_get_memsize;
+
+  g_type_class_add_private (gimp_id_table_class,
+                            sizeof (GimpIdTablePriv));
+}
+
+static void
+gimp_id_table_init (GimpIdTable *id_table)
+{
+  id_table->priv = G_TYPE_INSTANCE_GET_PRIVATE (id_table,
+                                                GIMP_TYPE_ID_TABLE,
+                                                GimpIdTablePriv);
+
+  id_table->priv->id_table = g_hash_table_new (g_direct_hash, NULL);
+  id_table->priv->next_id  = GIMP_ID_TABLE_START_ID;
+}
+
+static void
+gimp_id_table_finalize (GObject *object)
+{
+  GimpIdTable *id_table = GIMP_ID_TABLE (object);
+
+  if (id_table->priv->id_table)
+    {
+      g_hash_table_unref (id_table->priv->id_table);
+      id_table->priv->id_table = NULL;
+    }
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static gint64
+gimp_id_table_get_memsize (GimpObject *object,
+                           gint64     *gui_size)
+{
+  GimpIdTable *id_table = GIMP_ID_TABLE (object);
+  gint64       memsize  = 0;
+
+  memsize += gimp_g_hash_table_get_memsize (id_table->priv->id_table, 0);
+
+  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
+                                                                  gui_size);
+}
+
+/**
+ * gimp_id_table_new:
+ *
+ * Returns: A new #GimpIdTable.
+ **/
+GimpIdTable *
+gimp_id_table_new (void)
+{
+  return g_object_new (GIMP_TYPE_ID_TABLE, NULL);
+}
+
+/**
+ * gimp_id_table_insert:
+ * @id_table: A #GimpIdTable
+ * @data: Data to insert and assign an id to
+ *
+ * Insert data in the id table. The data will get an, in this table,
+ * unused ID assigned to it that can be used to later lookup the data.
+ *
+ * Returns: The assigned ID.
+ **/
+gint
+gimp_id_table_insert (GimpIdTable *id_table, void *data)
+{
+  gint new_id;
+
+  g_return_val_if_fail (GIMP_IS_ID_TABLE (id_table), 0);
+
+  do
+    {
+      new_id = id_table->priv->next_id++;
+
+      if (id_table->priv->next_id == GIMP_ID_TABLE_END_ID)
+        id_table->priv->next_id = GIMP_ID_TABLE_START_ID;
+    }
+  while (gimp_id_table_lookup (id_table, new_id));
+
+  return gimp_id_table_insert_with_id (id_table, new_id, data);
+}
+
+/**
+ * gimp_id_table_insert_with_id:
+ * @id_table: An #GimpIdTable
+ * @id: The ID to use. Must be greater than 0.
+ * @data: The data to associate with the id
+ *
+ * Insert data in the id table with a specific ID. If data already
+ * exsts with the given ID, this function fails.
+ *
+ * Returns: The used ID if successful, -1 if it was already in use.
+ **/
+gint
+gimp_id_table_insert_with_id (GimpIdTable *id_table, gint id, void *data)
+{
+  g_return_val_if_fail (GIMP_IS_ID_TABLE (id_table), 0);
+  g_return_val_if_fail (id > 0, 0);
+
+  if (gimp_id_table_lookup (id_table, id))
+    return -1;
+
+  g_hash_table_insert (id_table->priv->id_table, GINT_TO_POINTER (id), data);
+
+  return id;
+}
+
+/**
+ * gimp_id_table_replace:
+ * @id_table: An #GimpIdTable
+ * @id: The ID to use. Must be greater than 0.
+ * @data: The data to insert/replace
+ *
+ * Replaces (if an item with the given ID exists) or inserts a new
+ * entry in the id table.
+ **/
+void
+gimp_id_table_replace (GimpIdTable *id_table, gint id, void *data)
+{
+  g_return_if_fail (GIMP_IS_ID_TABLE (id_table));
+  g_return_if_fail (id > 0);
+
+  g_hash_table_replace (id_table->priv->id_table, GINT_TO_POINTER (id), data);
+}
+
+/**
+ * gimp_id_table_lookup:
+ * @id_table: An #GimpIdTable
+ * @id: The ID of the data to lookup
+ *
+ * Lookup data based on ID.
+ *
+ * Returns: The data, or NULL if no data with the given ID was found.
+ **/
+void *
+gimp_id_table_lookup (GimpIdTable *id_table, gint id)
+{
+  g_return_val_if_fail (GIMP_IS_ID_TABLE (id_table), NULL);
+
+  return g_hash_table_lookup (id_table->priv->id_table, GINT_TO_POINTER (id));
+}
+
+
+/**
+ * gimp_id_table_remove:
+ * @id_table: An #GimpIdTable
+ * @id: The ID of the data to remove.
+ *
+ * Remove the data from the table with the given ID.
+ *
+ * Returns: %TRUE if data with the ID existed and was successfully
+ *          removed, %FALSE otherwise.
+ **/
+gboolean
+gimp_id_table_remove (GimpIdTable *id_table, gint id)
+{
+  g_return_val_if_fail (GIMP_IS_ID_TABLE (id_table), FALSE);
+
+  g_return_val_if_fail (id_table != NULL, FALSE);
+
+  return g_hash_table_remove (id_table->priv->id_table, GINT_TO_POINTER (id));
+}
diff --git a/app/core/gimpidtable.h b/app/core/gimpidtable.h
new file mode 100644
index 0000000..e5cec24
--- /dev/null
+++ b/app/core/gimpidtable.h
@@ -0,0 +1,68 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpidtable.h
+ * Copyright (C) 2011 Martin Nordholts <martinn src gnome org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_ID_TABLE_H__
+#define __GIMP_ID_TABLE_H__
+
+
+#include "gimpobject.h"
+
+
+#define GIMP_TYPE_ID_TABLE            (gimp_id_table_get_type ())
+#define GIMP_ID_TABLE(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_ID_TABLE, GimpIdTable))
+#define GIMP_ID_TABLE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_ID_TABLE, GimpIdTableClass))
+#define GIMP_IS_ID_TABLE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_ID_TABLE))
+#define GIMP_IS_ID_TABLE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_ID_TABLE))
+#define GIMP_ID_TABLE_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_ID_TABLE, GimpIdTableClass))
+
+
+typedef struct _GimpIdTableClass  GimpIdTableClass;
+typedef struct _GimpIdTablePriv   GimpIdTablePriv;
+
+struct _GimpIdTable
+{
+  GimpObject       parent_instance;
+
+  GimpIdTablePriv *priv;
+};
+
+struct _GimpIdTableClass
+{
+  GimpObjectClass  parent_class;
+};
+
+
+GType          gimp_id_table_get_type       (void) G_GNUC_CONST;
+GimpIdTable *  gimp_id_table_new            (void);
+gint           gimp_id_table_insert         (GimpIdTable *id_table,
+                                             void        *data);
+gint           gimp_id_table_insert_with_id (GimpIdTable *id_table,
+                                             gint         id,
+                                             void        *data);
+void           gimp_id_table_replace        (GimpIdTable *id_table,
+                                             gint         id,
+                                             void        *data);
+void         * gimp_id_table_lookup         (GimpIdTable *id_table,
+                                             gint         id);
+gboolean       gimp_id_table_remove         (GimpIdTable *id_table,
+                                             gint         id);
+
+
+#endif  /*  __GIMP_ID_TABLE_H__  */
diff --git a/app/tests/.gitignore b/app/tests/.gitignore
index 51e7346..0a4e9b2 100644
--- a/app/tests/.gitignore
+++ b/app/tests/.gitignore
@@ -5,6 +5,7 @@ Makefile
 Makefile.in
 libgimpapptestutils.a
 test-core*
+test-gimpidtable*
 test-layer-grouping*
 test-save-and-export*
 test-session-management*
diff --git a/app/tests/Makefile.am b/app/tests/Makefile.am
index 82675df..13b49d2 100644
--- a/app/tests/Makefile.am
+++ b/app/tests/Makefile.am
@@ -20,6 +20,7 @@ endif
 
 TESTS = \
 	test-core		\
+	test-gimpidtable	\
 	test-save-and-export	\
 	test-session-management	\
 	test-tools		\
diff --git a/app/tests/test-gimpidtable.c b/app/tests/test-gimpidtable.c
new file mode 100644
index 0000000..79e0640
--- /dev/null
+++ b/app/tests/test-gimpidtable.c
@@ -0,0 +1,232 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 2009 Martin Nordholts <martinn src gnome org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <glib-object.h>
+
+#include "core/core-types.h"
+
+#include "core/gimpidtable.h"
+
+//#include "tests.h"
+
+//#include "gimp-app-test-utils.h"
+
+
+#define ADD_TEST(function) \
+  g_test_add ("/gimpidtable/" #function, \
+              GimpTestFixture, \
+              NULL, \
+              gimp_test_id_table_setup, \
+              function, \
+              gimp_test_id_table_teardown);
+
+
+typedef struct
+{
+  GimpIdTable *id_table;
+} GimpTestFixture;
+
+
+static void *data1 = (void *) 0x00000001;
+static void *data2 = (void *) 0x00000002;
+
+
+static void
+gimp_test_id_table_setup (GimpTestFixture *fixture,
+                          gconstpointer    data)
+{
+  fixture->id_table = gimp_id_table_new ();
+}
+
+static void
+gimp_test_id_table_teardown (GimpTestFixture *fixture,
+                             gconstpointer    data)
+{
+  g_object_unref (fixture->id_table);
+  fixture->id_table = NULL;
+}
+
+/**
+ * insert_and_lookup:
+ *
+ * Test that insert and lookup works.
+ **/
+static void
+insert_and_lookup (GimpTestFixture *f,
+                   gconstpointer    data)
+{
+  gint  ret_id   = gimp_id_table_insert (f->id_table, data1);
+  void *ret_data = gimp_id_table_lookup (f->id_table, ret_id);
+
+  g_assert (ret_data == data1);
+}
+
+/**
+ * insert_twice:
+ *
+ * Test that two consecutive inserts generates different IDs.
+ **/
+static void
+insert_twice (GimpTestFixture *f,
+              gconstpointer    data)
+{
+  gint  ret_id    = gimp_id_table_insert (f->id_table, data1);
+  void *ret_data  = gimp_id_table_lookup (f->id_table, ret_id);
+  gint  ret_id2   = gimp_id_table_insert (f->id_table, data2);
+  void *ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);
+
+  g_assert (ret_id    != ret_id2);
+  g_assert (ret_data  == data1);
+  g_assert (ret_data2 == data2);
+}
+
+/**
+ * insert_with_id:
+ *
+ * Test that it is possible to insert data with a specific ID.
+ **/
+static void
+insert_with_id (GimpTestFixture *f,
+                gconstpointer    data)
+{
+  const int id = 10;
+
+  int   ret_id   = gimp_id_table_insert_with_id (f->id_table, id, data1);
+  void *ret_data = gimp_id_table_lookup (f->id_table, id);
+
+  g_assert (ret_id   == id);
+  g_assert (ret_data == data1);
+}
+
+/**
+ * insert_with_id_existing:
+ *
+ * Test that it is not possible to insert data with a specific ID if
+ * that ID already is inserted.
+ **/
+static void
+insert_with_id_existing (GimpTestFixture *f,
+                         gconstpointer    data)
+{
+  const int id = 10;
+
+  int   ret_id    = gimp_id_table_insert_with_id (f->id_table, id, data1);
+  void *ret_data  = gimp_id_table_lookup (f->id_table, ret_id);
+  int   ret_id2   = gimp_id_table_insert_with_id (f->id_table, id, data2);
+  void *ret_data2 = gimp_id_table_lookup (f->id_table, ret_id2);
+
+  g_assert (id        == ret_id);
+  g_assert (ret_id2   == -1);
+  g_assert (ret_data  == data1);
+  g_assert (ret_data2 == NULL);
+}
+
+/**
+ * replace:
+ *
+ * Test that it is possible to replace data with a given ID with
+ * different data.
+ **/
+static void
+replace (GimpTestFixture *f,
+         gconstpointer    data)
+{
+  int ret_id = gimp_id_table_insert (f->id_table, data1);
+  void *ret_data;
+
+  gimp_id_table_replace (f->id_table, ret_id, data2);
+  ret_data = gimp_id_table_lookup (f->id_table, ret_id);
+
+  g_assert (ret_data  == data2);
+}
+
+/**
+ * replace_as_insert:
+ *
+ * Test that replace works like insert when there is no data to
+ * replace.
+ **/
+static void
+replace_as_insert (GimpTestFixture *f,
+                   gconstpointer    data)
+{
+  const int id = 10;
+
+  void *ret_data;
+
+  gimp_id_table_replace (f->id_table, id, data1);
+  ret_data = gimp_id_table_lookup (f->id_table, id);
+
+  g_assert (ret_data  == data1);
+}
+
+/**
+ * remove:
+ *
+ * Test that it is possible to remove data identified by the ID:
+ **/
+static void
+remove (GimpTestFixture *f,
+        gconstpointer    data)
+{
+  gint     ret_id            = gimp_id_table_insert (f->id_table, data1);
+  void    *ret_data          = gimp_id_table_lookup (f->id_table, ret_id);
+  gboolean remove_successful = gimp_id_table_remove (f->id_table, ret_id);
+  void    *ret_data2         = gimp_id_table_lookup (f->id_table, ret_id);
+  
+  g_assert (remove_successful);
+  g_assert (ret_data == data1);
+  g_assert (ret_data2 == NULL);
+}
+
+/**
+ * remove_non_existing:
+ *
+ * Tests that things work properly when trying to remove data with an
+ * ID that doesn't exist.
+ **/
+static void
+remove_non_existing (GimpTestFixture *f,
+                     gconstpointer    data)
+{
+  const int id = 10;
+
+  gboolean remove_successful = gimp_id_table_remove (f->id_table, id);
+  void    *ret_data          = gimp_id_table_lookup (f->id_table, id);
+  
+  g_assert (! remove_successful);
+  g_assert (ret_data == NULL);
+}
+
+int main(int argc, char **argv)
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  ADD_TEST (insert_and_lookup);
+  ADD_TEST (insert_twice);
+  ADD_TEST (insert_with_id);
+  ADD_TEST (insert_with_id_existing);
+  ADD_TEST (replace);
+  ADD_TEST (replace_as_insert);
+  ADD_TEST (remove);
+  ADD_TEST (remove_non_existing);
+
+  return g_test_run ();
+}



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