/*
- Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
+ Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
struct _KatzeArray
{
- KatzeList parent_instance;
+ KatzeItem parent_instance;
GType type;
+ GList* items;
};
struct _KatzeArrayClass
{
- KatzeListClass parent_class;
+ KatzeItemClass parent_class;
+
+ /* Signals */
+ void
+ (*add_item) (KatzeArray* array,
+ gpointer item);
+ void
+ (*remove_item) (KatzeArray* array,
+ gpointer item);
+ void
+ (*clear) (KatzeArray* array);
};
-G_DEFINE_TYPE (KatzeArray, katze_array, KATZE_TYPE_LIST)
+G_DEFINE_TYPE (KatzeArray, katze_array, KATZE_TYPE_ITEM);
+
+enum {
+ ADD_ITEM,
+ REMOVE_ITEM,
+ CLEAR,
+
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL];
static void
katze_array_finalize (GObject* object);
static void
-_katze_array_add_item (KatzeList* list,
- gpointer item)
+_katze_array_add_item (KatzeArray* array,
+ gpointer item)
{
- if (katze_array_is_a ((KatzeArray*)list, G_TYPE_OBJECT))
+ if (katze_array_is_a (array, G_TYPE_OBJECT))
{
GType type = G_OBJECT_TYPE (item);
- g_return_if_fail (katze_array_is_a ((KatzeArray*)list, type));
+ g_return_if_fail (katze_array_is_a (array, type));
g_object_ref (item);
if (g_type_is_a (type, KATZE_TYPE_ITEM))
- katze_item_set_parent (item, list);
+ katze_item_set_parent (item, array);
}
- KATZE_LIST_CLASS (katze_array_parent_class)->add_item (list, item);
+
+ array->items = g_list_append (array->items, item);
}
static void
-_katze_array_remove_item (KatzeList* list,
+_katze_array_remove_item (KatzeArray* array,
gpointer item)
{
- KATZE_LIST_CLASS (katze_array_parent_class)->remove_item (list, item);
- if (katze_array_is_a ((KatzeArray*)list, G_TYPE_OBJECT))
+ array->items = g_list_remove (array->items, item);
+
+ if (katze_array_is_a (array, G_TYPE_OBJECT))
{
if (KATZE_IS_ITEM (item))
katze_item_set_parent (item, NULL);
}
}
+static void
+_katze_array_clear (KatzeArray* array)
+{
+ guint n;
+ guint i;
+ GObject* item;
+
+ n = g_list_length (array->items);
+ for (i = 0; i < n; i++)
+ {
+ if ((item = g_list_nth_data (array->items, i)))
+ katze_array_remove_item (array, item);
+ }
+ g_list_free (array->items);
+ array->items = NULL;
+}
+
static void
katze_array_class_init (KatzeArrayClass* class)
{
GObjectClass* gobject_class;
- KatzeListClass* katzelist_class;
gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = katze_array_finalize;
- katzelist_class = KATZE_LIST_CLASS (class);
- katzelist_class->add_item = _katze_array_add_item;
- katzelist_class->remove_item = _katze_array_remove_item;
+ signals[ADD_ITEM] = g_signal_new (
+ "add-item",
+ G_TYPE_FROM_CLASS (class),
+ (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
+ G_STRUCT_OFFSET (KatzeArrayClass, add_item),
+ 0,
+ NULL,
+ g_cclosure_marshal_VOID__POINTER,
+ G_TYPE_NONE, 1,
+ G_TYPE_POINTER);
+
+ signals[REMOVE_ITEM] = g_signal_new (
+ "remove-item",
+ G_TYPE_FROM_CLASS (class),
+ (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
+ G_STRUCT_OFFSET (KatzeArrayClass, remove_item),
+ 0,
+ NULL,
+ g_cclosure_marshal_VOID__POINTER,
+ G_TYPE_NONE, 1,
+ G_TYPE_POINTER);
+
+ signals[CLEAR] = g_signal_new (
+ "clear",
+ G_TYPE_FROM_CLASS (class),
+ (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
+ G_STRUCT_OFFSET (KatzeArrayClass, clear),
+ 0,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ gobject_class = G_OBJECT_CLASS (class);
+ gobject_class->finalize = katze_array_finalize;
+
+ class->add_item = _katze_array_add_item;
+ class->remove_item = _katze_array_remove_item;
+ class->clear = _katze_array_clear;
}
static void
katze_array_init (KatzeArray* array)
{
array->type = G_TYPE_NONE;
+ array->items = NULL;
}
static void
array = KATZE_ARRAY (object);
if (katze_array_is_a (array, G_TYPE_OBJECT))
{
- n = katze_list_get_length ((KatzeList*)array);
+ n = g_list_length (array->items);
for (i = 0; i < n; i++)
- g_object_unref (katze_list_get_nth_item ((KatzeList*)array, i));
+ g_object_unref (g_list_nth_data (array->items, i));
}
+ g_list_free (array->items);
+
G_OBJECT_CLASS (katze_array_parent_class)->finalize (object);
}
* The array will keep a reference on each object until
* it is removed from the array.
*
- * If @type is *not* a #GObject type, #KatzeArray behaves
- * pretty much like #KatzeList.
- *
- * Note: Since 0.1.2 you may use #KatzeList accessors to
- * work with #KatzeArray if you want to.
- *
* Return value: a new #KatzeArray
**/
KatzeArray*
/**
* katze_array_add_item:
* @array: a #KatzeArray
- * @item: a #GObject
+ * @item: an item
*
* Adds an item to the array.
*
katze_array_add_item (KatzeArray* array,
gpointer item)
{
- /* g_return_if_fail (KATZE_IS_ARRAY (array)); */
+ g_return_if_fail (KATZE_IS_ARRAY (array));
- katze_list_add_item (KATZE_LIST (array), item);
+ g_signal_emit (array, signals[ADD_ITEM], 0, item);
}
/**
* katze_array_remove_item:
* @array: a #KatzeArray
- * @item: a #GObject
+ * @item: an item
*
* Removes an item from the array.
*
katze_array_remove_item (KatzeArray* array,
gpointer item)
{
- /* g_return_if_fail (KATZE_IS_ARRAY (array)); */
+ g_return_if_fail (KATZE_IS_ARRAY (array));
- katze_list_remove_item (KATZE_LIST (array), item);
+ g_signal_emit (array, signals[REMOVE_ITEM], 0, item);
}
/**
katze_array_get_nth_item (KatzeArray* array,
guint n)
{
- /* g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL); */
+ g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
- return katze_list_get_nth_item (KATZE_LIST (array), n);
+ return g_list_nth_data (array->items, n);
}
/**
* katze_array_is_empty:
* @array: a #KatzeArray
*
- * Determines if @array is empty.
+ * Determines whether @array is empty.
*
* Return value: an item, or %NULL
**/
gboolean
katze_array_is_empty (KatzeArray* array)
{
- /* g_return_val_if_fail (KATZE_IS_ARRAY (array), TRUE); */
+ g_return_val_if_fail (KATZE_IS_ARRAY (array), TRUE);
- return katze_list_is_empty (KATZE_LIST (array));
+ return !g_list_nth_data (array->items, 0);
}
/**
katze_array_get_item_index (KatzeArray* array,
gpointer item)
{
- /* g_return_val_if_fail (KATZE_IS_ARRAY (array), -1); */
+ g_return_val_if_fail (KATZE_IS_ARRAY (array), -1);
- return katze_list_get_item_index (KATZE_LIST (array), item);
+ return g_list_index (array->items, item);
}
/**
if (!katze_array_is_a (array, G_TYPE_OBJECT))
return NULL;
- n = katze_list_get_length ((KatzeList*)array);
+ n = g_list_length (array->items);
for (i = 0; i < n; i++)
{
- item = katze_list_get_nth_item ((KatzeList*)array, i);
+ item = g_list_nth_data (array->items, i);
if (!g_type_is_a (G_OBJECT_TYPE (item), KATZE_TYPE_ITEM))
continue;
found_token = katze_item_get_token ((KatzeItem*)item);
- if (found_token && !strcmp (found_token, token))
+ if (!g_strcmp0 (found_token, token))
return item;
}
return NULL;
guint
katze_array_get_length (KatzeArray* array)
{
- /* g_return_val_if_fail (KATZE_IS_ARRAY (array), 0); */
+ g_return_val_if_fail (KATZE_IS_ARRAY (array), 0);
- return katze_list_get_length (KATZE_LIST (array));
+ return g_list_length (array->items);
}
/**
void
katze_array_clear (KatzeArray* array)
{
- /* g_return_if_fail (KATZE_IS_ARRAY (array)); */
+ g_return_if_fail (KATZE_IS_ARRAY (array));
- katze_list_clear (KATZE_LIST (array));
+ g_signal_emit (array, signals[CLEAR], 0);
}
+++ /dev/null
-/*
- Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- See the file COPYING for the full license text.
-*/
-
-#include "katze-list.h"
-
-#include "katze-utils.h"
-
-#include <glib/gi18n.h>
-#include <string.h>
-
-/**
- * SECTION:katze-list
- * @short_description: A verbose and versatile item container
- * @see_also: #KatzeItem
- *
- * #KatzeList is a verbose and versatile container for items.
- */
-
-G_DEFINE_TYPE (KatzeList, katze_list, KATZE_TYPE_ITEM)
-
-enum {
- ADD_ITEM,
- REMOVE_ITEM,
- CLEAR,
-
- LAST_SIGNAL
-};
-
-static guint signals[LAST_SIGNAL];
-
-static void
-katze_list_finalize (GObject* object);
-
-static void
-_katze_list_add_item (KatzeList* list,
- gpointer item)
-{
- list->items = g_list_append (list->items, item);
-}
-
-static void
-_katze_list_remove_item (KatzeList* list,
- gpointer item)
-{
- list->items = g_list_remove (list->items, item);
-}
-
-static void
-_katze_list_clear (KatzeList* list)
-{
- guint n;
- guint i;
- GObject* item;
-
- n = g_list_length (list->items);
- for (i = 0; i < n; i++)
- {
- if ((item = g_list_nth_data (list->items, i)))
- katze_list_remove_item (list, item);
- }
- g_list_free (list->items);
- list->items = NULL;
-}
-
-static void
-katze_list_class_init (KatzeListClass* class)
-{
- GObjectClass* gobject_class;
-
- signals[ADD_ITEM] = g_signal_new (
- "add-item",
- G_TYPE_FROM_CLASS (class),
- (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
- G_STRUCT_OFFSET (KatzeListClass, add_item),
- 0,
- NULL,
- g_cclosure_marshal_VOID__POINTER,
- G_TYPE_NONE, 1,
- G_TYPE_POINTER);
-
- signals[REMOVE_ITEM] = g_signal_new (
- "remove-item",
- G_TYPE_FROM_CLASS (class),
- (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
- G_STRUCT_OFFSET (KatzeListClass, remove_item),
- 0,
- NULL,
- g_cclosure_marshal_VOID__POINTER,
- G_TYPE_NONE, 1,
- G_TYPE_POINTER);
-
- signals[CLEAR] = g_signal_new (
- "clear",
- G_TYPE_FROM_CLASS (class),
- (GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION),
- G_STRUCT_OFFSET (KatzeListClass, clear),
- 0,
- NULL,
- g_cclosure_marshal_VOID__VOID,
- G_TYPE_NONE, 0);
-
-
- gobject_class = G_OBJECT_CLASS (class);
- gobject_class->finalize = katze_list_finalize;
-
- class->add_item = _katze_list_add_item;
- class->remove_item = _katze_list_remove_item;
- class->clear = _katze_list_clear;
-}
-
-static void
-katze_list_init (KatzeList* list)
-{
- list->items = NULL;
-}
-
-static void
-katze_list_finalize (GObject* object)
-{
- KatzeList* list;
-
- list = KATZE_LIST (object);
- g_list_free (list->items);
-
- G_OBJECT_CLASS (katze_list_parent_class)->finalize (object);
-}
-
-/**
- * katze_list_new:
- *
- * Creates a new #KatzeList.
- *
- * Return value: a new #KatzeList
- **/
-KatzeList*
-katze_list_new (void)
-{
- KatzeList* list = g_object_new (KATZE_TYPE_LIST, NULL);
-
- return list;
-}
-
-/**
- * katze_list_add_item:
- * @list: a #KatzeList
- * @item: a #GObject
- *
- * Adds an item to the list.
- **/
-void
-katze_list_add_item (KatzeList* list,
- gpointer item)
-{
- g_return_if_fail (KATZE_IS_LIST (list));
-
- g_signal_emit (list, signals[ADD_ITEM], 0, item);
-}
-
-/**
- * katze_list_add_item:
- * @list: a #KatzeList
- * @item: a #GObject
- *
- * Removes an item from the list.
- **/
-void
-katze_list_remove_item (KatzeList* list,
- gpointer item)
-{
- g_return_if_fail (KATZE_IS_LIST (list));
-
- g_signal_emit (list, signals[REMOVE_ITEM], 0, item);
-}
-
-/**
- * katze_list_get_nth_item:
- * @list: a #KatzeList
- * @n: an index in the list
- *
- * Retrieves the item in @list at the position @n.
- *
- * Return value: an item, or %NULL
- **/
-gpointer
-katze_list_get_nth_item (KatzeList* list,
- guint n)
-{
- g_return_val_if_fail (KATZE_IS_LIST (list), NULL);
-
- return g_list_nth_data (list->items, n);
-}
-
-/**
- * katze_list_is_empty:
- * @list: a #KatzeList
- *
- * Determines if @list is empty.
- *
- * Return value: an item, or %NULL
- **/
-gboolean
-katze_list_is_empty (KatzeList* list)
-{
- g_return_val_if_fail (KATZE_IS_LIST (list), TRUE);
-
- return !g_list_nth_data (list->items, 0);
-}
-
-/**
- * katze_list_get_item_position:
- * @list: a #KatzeList
- * @item: an item in the list
- *
- * Retrieves the index of the item in @list.
- *
- * Return value: an item, or -1
- **/
-gint
-katze_list_get_item_index (KatzeList* list,
- gpointer item)
-{
- g_return_val_if_fail (KATZE_IS_LIST (list), -1);
-
- return g_list_index (list->items, item);
-}
-
-/**
- * katze_list_get_length:
- * @list: a #KatzeList
- *
- * Retrieves the number of items in @list.
- *
- * Return value: the length of the list
- **/
-guint
-katze_list_get_length (KatzeList* list)
-{
- g_return_val_if_fail (KATZE_IS_LIST (list), 0);
-
- return g_list_length (list->items);
-}
-
-/**
- * katze_list_clear:
- * @list: a #KatzeList
- *
- * Deletes all items currently contained in @list.
- **/
-void
-katze_list_clear (KatzeList* list)
-{
- g_return_if_fail (KATZE_IS_LIST (list));
-
- g_signal_emit (list, signals[CLEAR], 0);
-}
+++ /dev/null
-/*
- Copyright (C) 2008 Christian Dywan <christian@twotoasts.de>
-
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 2.1 of the License, or (at your option) any later version.
-
- See the file COPYING for the full license text.
-*/
-
-#ifndef __KATZE_LIST_H__
-#define __KATZE_LIST_H__
-
-#include "katze-item.h"
-
-G_BEGIN_DECLS
-
-#define KATZE_TYPE_LIST \
- (katze_list_get_type ())
-#define KATZE_LIST(obj) \
- (G_TYPE_CHECK_INSTANCE_CAST ((obj), KATZE_TYPE_LIST, KatzeList))
-#define KATZE_LIST_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_CAST ((klass), KATZE_TYPE_LIST, KatzeListClass))
-#define KATZE_IS_LIST(obj) \
- (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KATZE_TYPE_LIST))
-#define KATZE_IS_LIST_CLASS(klass) \
- (G_TYPE_CHECK_CLASS_TYPE ((klass), KATZE_TYPE_LIST))
-#define KATZE_LIST_GET_CLASS(obj) \
- (G_TYPE_INSTANCE_GET_CLASS ((obj), KATZE_TYPE_LIST, KatzeListClass))
-
-typedef struct _KatzeList KatzeList;
-typedef struct _KatzeListClass KatzeListClass;
-
-struct _KatzeList
-{
- KatzeItem parent_instance;
-
- GList* items;
-};
-
-struct _KatzeListClass
-{
- KatzeItemClass parent_class;
-
- /* Signals */
- void
- (*add_item) (KatzeList* list,
- gpointer item);
- void
- (*remove_item) (KatzeList* list,
- gpointer item);
- void
- (*clear) (KatzeList* list);
-
-};
-
-GType
-katze_list_get_type (void);
-
-KatzeList*
-katze_list_new (void);
-
-void
-katze_list_add_item (KatzeList* list,
- gpointer item);
-
-void
-katze_list_remove_item (KatzeList* list,
- gpointer item);
-
-gpointer
-katze_list_get_nth_item (KatzeList* list,
- guint n);
-
-gboolean
-katze_list_is_empty (KatzeList* list);
-
-gint
-katze_list_get_item_index (KatzeList* list,
- gpointer item);
-
-guint
-katze_list_get_length (KatzeList* list);
-
-void
-katze_list_clear (KatzeList* list);
-
-G_END_DECLS
-
-#endif /* __KATZE_LIST_H__ */