Merge KatzeArray and KatzeList, we only ever use the array

This commit is contained in:
Christian Dywan 2009-03-24 01:55:46 +01:00
parent 15790565e8
commit a75a94c388
6 changed files with 119 additions and 404 deletions

View file

@ -1,5 +1,5 @@
/* /*
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 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -26,43 +26,66 @@
struct _KatzeArray struct _KatzeArray
{ {
KatzeList parent_instance; KatzeItem parent_instance;
GType type; GType type;
GList* items;
}; };
struct _KatzeArrayClass 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 static void
katze_array_finalize (GObject* object); katze_array_finalize (GObject* object);
static void static void
_katze_array_add_item (KatzeList* list, _katze_array_add_item (KatzeArray* array,
gpointer item) 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); 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); g_object_ref (item);
if (g_type_is_a (type, KATZE_TYPE_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 static void
_katze_array_remove_item (KatzeList* list, _katze_array_remove_item (KatzeArray* array,
gpointer item) gpointer item)
{ {
KATZE_LIST_CLASS (katze_array_parent_class)->remove_item (list, item); array->items = g_list_remove (array->items, item);
if (katze_array_is_a ((KatzeArray*)list, G_TYPE_OBJECT))
if (katze_array_is_a (array, G_TYPE_OBJECT))
{ {
if (KATZE_IS_ITEM (item)) if (KATZE_IS_ITEM (item))
katze_item_set_parent (item, NULL); katze_item_set_parent (item, NULL);
@ -70,24 +93,76 @@ _katze_array_remove_item (KatzeList* list,
} }
} }
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 static void
katze_array_class_init (KatzeArrayClass* class) katze_array_class_init (KatzeArrayClass* class)
{ {
GObjectClass* gobject_class; GObjectClass* gobject_class;
KatzeListClass* katzelist_class;
gobject_class = G_OBJECT_CLASS (class); gobject_class = G_OBJECT_CLASS (class);
gobject_class->finalize = katze_array_finalize; gobject_class->finalize = katze_array_finalize;
katzelist_class = KATZE_LIST_CLASS (class); signals[ADD_ITEM] = g_signal_new (
katzelist_class->add_item = _katze_array_add_item; "add-item",
katzelist_class->remove_item = _katze_array_remove_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 static void
katze_array_init (KatzeArray* array) katze_array_init (KatzeArray* array)
{ {
array->type = G_TYPE_NONE; array->type = G_TYPE_NONE;
array->items = NULL;
} }
static void static void
@ -99,11 +174,13 @@ katze_array_finalize (GObject* object)
array = KATZE_ARRAY (object); array = KATZE_ARRAY (object);
if (katze_array_is_a (array, G_TYPE_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++) 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); G_OBJECT_CLASS (katze_array_parent_class)->finalize (object);
} }
@ -118,12 +195,6 @@ katze_array_finalize (GObject* object)
* The array will keep a reference on each object until * The array will keep a reference on each object until
* it is removed from the array. * 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 * Return value: a new #KatzeArray
**/ **/
KatzeArray* KatzeArray*
@ -158,7 +229,7 @@ katze_array_is_a (KatzeArray* array,
/** /**
* katze_array_add_item: * katze_array_add_item:
* @array: a #KatzeArray * @array: a #KatzeArray
* @item: a #GObject * @item: an item
* *
* Adds an item to the array. * Adds an item to the array.
* *
@ -168,15 +239,15 @@ void
katze_array_add_item (KatzeArray* array, katze_array_add_item (KatzeArray* array,
gpointer item) 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: * katze_array_remove_item:
* @array: a #KatzeArray * @array: a #KatzeArray
* @item: a #GObject * @item: an item
* *
* Removes an item from the array. * Removes an item from the array.
* *
@ -186,9 +257,9 @@ void
katze_array_remove_item (KatzeArray* array, katze_array_remove_item (KatzeArray* array,
gpointer item) 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);
} }
/** /**
@ -204,25 +275,25 @@ gpointer
katze_array_get_nth_item (KatzeArray* array, katze_array_get_nth_item (KatzeArray* array,
guint n) 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: * katze_array_is_empty:
* @array: a #KatzeArray * @array: a #KatzeArray
* *
* Determines if @array is empty. * Determines whether @array is empty.
* *
* Return value: an item, or %NULL * Return value: an item, or %NULL
**/ **/
gboolean gboolean
katze_array_is_empty (KatzeArray* array) 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);
} }
/** /**
@ -238,9 +309,9 @@ gint
katze_array_get_item_index (KatzeArray* array, katze_array_get_item_index (KatzeArray* array,
gpointer item) 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);
} }
/** /**
@ -271,14 +342,14 @@ katze_array_find_token (KatzeArray* array,
if (!katze_array_is_a (array, G_TYPE_OBJECT)) if (!katze_array_is_a (array, G_TYPE_OBJECT))
return NULL; return NULL;
n = katze_list_get_length ((KatzeList*)array); n = g_list_length (array->items);
for (i = 0; i < n; i++) 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)) if (!g_type_is_a (G_OBJECT_TYPE (item), KATZE_TYPE_ITEM))
continue; continue;
found_token = katze_item_get_token ((KatzeItem*)item); found_token = katze_item_get_token ((KatzeItem*)item);
if (found_token && !strcmp (found_token, token)) if (!g_strcmp0 (found_token, token))
return item; return item;
} }
return NULL; return NULL;
@ -295,9 +366,9 @@ katze_array_find_token (KatzeArray* array,
guint guint
katze_array_get_length (KatzeArray* array) 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);
} }
/** /**
@ -309,7 +380,7 @@ katze_array_get_length (KatzeArray* array)
void void
katze_array_clear (KatzeArray* array) 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);
} }

View file

@ -1,5 +1,5 @@
/* /*
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 This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -12,7 +12,7 @@
#ifndef __KATZE_ARRAY_H__ #ifndef __KATZE_ARRAY_H__
#define __KATZE_ARRAY_H__ #define __KATZE_ARRAY_H__
#include "katze-list.h" #include <katze/katze-item.h>
G_BEGIN_DECLS G_BEGIN_DECLS

View file

@ -1,263 +0,0 @@
/*
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);
}

View file

@ -1,91 +0,0 @@
/*
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__ */

View file

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007 Christian Dywan <christian@twotoasts.de> Copyright (C) 2007-2009 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -17,7 +17,6 @@
#include "katze-throbber.h" #include "katze-throbber.h"
#include "katze-utils.h" #include "katze-utils.h"
#include "katze-item.h" #include "katze-item.h"
#include "katze-list.h"
#include "katze-array.h" #include "katze-array.h"
#include "katze-arrayaction.h" #include "katze-arrayaction.h"
#include "katze-separatoraction.h" #include "katze-separatoraction.h"

View file

@ -22,7 +22,6 @@ katze/katze-http-auth.c
katze/katze-throbber.c katze/katze-throbber.c
katze/katze-utils.c katze/katze-utils.c
katze/katze-item.c katze/katze-item.c
katze/katze-list.c
katze/katze-array.c katze/katze-array.c
katze/katze-arrayaction.c katze/katze-arrayaction.c
extensions/colorful-tabs.c extensions/colorful-tabs.c