libgeda: Remove some exit() calls and assertions.
[geda-gaf/peter-b.git] / libgeda / src / geda_list.c
blobc14432726046ed16760db2bcd0efcad49d04388c
1 /* gEDA - GPL Electronic Design Automation
2 * libgeda - gEDA's library
3 * Copyright (C) 1998-2000 Ales Hvezda
4 * Copyright (C) 2007-2010 Peter Clifton
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /*! \file geda_list.c
22 * \brief list derived from GList with GObject properties
24 * This GedaList with the GObject properties can use the signaling
25 * mechanisms of GObject now.
28 #include <config.h>
30 #include <glib-object.h>
32 #include "geda_list.h"
34 #ifdef HAVE_LIBDMALLOC
35 #include <dmalloc.h>
36 #endif
39 enum {
40 CHANGED,
41 LAST_SIGNAL
44 static guint geda_list_signals[ LAST_SIGNAL ] = { 0 };
45 static GObjectClass *geda_list_parent_class = NULL;
48 /*! \brief GType instance initialiser for GedaList
50 * \par Function Description
51 * GType instance initialiser for GedaList.
53 * \param [in] instance The GedaList we are initialising.
54 * \param [in] g_class The class of the type the instance is created for.
56 static void geda_list_instance_init( GTypeInstance *instance, gpointer g_class )
58 GedaList *list = (GedaList *)instance;
60 /* Strictly un-necessary, as the memory is zero'd after allocation */
61 list->glist = NULL;
65 /*! \brief GObject finalise handler
67 * \par Function Description
68 * Just before the GedaList GObject is finalized, free our
69 * allocated data, and then chain up to the parent's finalize handler.
71 * \param [in] object The GObject being finalized.
73 static void geda_list_finalize( GObject *object )
75 GedaList *list = GEDA_LIST( object );
76 g_list_free( list->glist );
78 G_OBJECT_CLASS( geda_list_parent_class )->finalize( object );
82 /*! \brief GType class initialiser for GedaList
84 * \par Function Description
85 * GType class initialiser for GedaList. We override our parents
86 * virtual class methods as needed and register our GObject signals.
88 * \param [in] g_class The GedaList we are initialising
89 * \param [in] g_class_data (unused)
91 static void geda_list_class_init( gpointer g_class, gpointer g_class_data )
93 GedaListClass *klass = GEDA_LIST_CLASS( g_class );
94 GObjectClass *gobject_class = G_OBJECT_CLASS( klass );
95 geda_list_parent_class = g_type_class_peek_parent( klass );
97 gobject_class->finalize = geda_list_finalize;
99 geda_list_signals[ CHANGED ] =
100 g_signal_new ("changed",
101 G_OBJECT_CLASS_TYPE( gobject_class ),
102 0 /*signal_flags */,
103 0 /*class_offset */,
104 NULL, /* accumulator */
105 NULL, /* accu_data */
106 g_cclosure_marshal_VOID__VOID,
107 G_TYPE_NONE,
108 0 /* n_params */
113 /*! \brief Function to retrieve GedaList's GType identifier.
115 * \par Function Description
116 * Function to retrieve GedaList's GType identifier.
117 * Upon first call, this registers the GedaList in the GType system.
118 * Subsequently it returns the saved value from its first execution.
120 * \return the GType identifier associated with GedaList.
122 GType geda_list_get_type(void)
124 static GType type = 0;
125 if (type == 0) {
126 static const GTypeInfo info = {
127 sizeof (GedaListClass),
128 NULL, /* base_init */
129 NULL, /* base_finalize */
130 geda_list_class_init, /* class_init */
131 NULL, /* class_finalize */
132 NULL, /* class_data */
133 sizeof (GedaList),
134 0, /* n_preallocs */
135 geda_list_instance_init /* instance_init */
137 type = g_type_register_static (G_TYPE_OBJECT, "GedaList", &info, 0);
139 return type;
143 /*! \brief Returns a pointer to a new GedaList object.
145 * \par Function Description
146 * Returns a pointer to a new GedaList object.
148 * \return pointer to the new GedaList object.
150 GedaList *geda_list_new( void ) {
151 return g_object_new( GEDA_TYPE_LIST, NULL );
155 /*! \brief Adds the given item to the GedaList
157 * \par Function Description
158 * Adds the given item to the GedaList
160 * \param [in] list Pointer to the GedaList
161 * \param [in] item item to add to the GedaList.
163 void geda_list_add( GedaList *list, gpointer item )
165 list->glist = g_list_append(list->glist, item );
166 g_signal_emit( list, geda_list_signals[ CHANGED ], 0 );
170 /*! \brief Adds the given glist of items to the GedaList
172 * \par Function Description
173 * Adds the given glist of items to the GedaList
174 * A copy is made, so the original GList is not modified.
176 * \param [in] list Pointer to the GedaList
177 * \param [in] items GList of items to add to the GedaList.
179 void geda_list_add_glist( GedaList *list, GList *items )
181 GList *glist_copy = g_list_copy( items );
182 list->glist = g_list_concat(list->glist, glist_copy );
183 g_signal_emit( list, geda_list_signals[ CHANGED ], 0 );
187 /*! \brief Removes the given item from the GedaList
189 * \par Function Description
190 * Removes the given item from the GedaList.
191 * It's ok to call this function with an item which
192 * is not necessarily in the list.
194 * \param [in] list Pointer to the GedaList
195 * \param [in] item to remove from the GedaList.
197 void geda_list_remove( GedaList *list, gpointer item )
199 if (g_list_find(list->glist, item) == NULL)
200 return;
202 list->glist = g_list_remove(list->glist, item);
203 g_signal_emit( list, geda_list_signals[ CHANGED ], 0 );
207 /*! \brief Removes all the items in the given GedaList.
209 * \par Function Description
210 * Removes all items in the given GedaList.
212 * \param [in] list Pointer to the GedaList
214 void geda_list_remove_all( GedaList *list )
216 g_list_free(list->glist);
217 list->glist = NULL;
218 g_signal_emit( list, geda_list_signals[ CHANGED ], 0 );