* cspi/cspi-lowlevel.h cspi/spi-impl.h cspi/spi-listener.h
[platform/upstream/at-spi2-core.git] / libspi / util.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001 Sun Microsystems Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <glib/gmessages.h>
24 #include <glib/gslist.h>
25
26 #include "spi-private.h"
27
28 typedef struct {
29         GList **list;
30         GList  *iterator;
31 } Iteration;
32
33 static GSList *working_list = NULL; /* of Iteration */
34
35 /*
36  *   deletes an element from the list - in a re-entrant
37  * safe fashion; advances the element pointer to the next
38  * element.
39  */
40 void
41 spi_re_entrant_list_delete_link (GList * const *element_ptr)
42 {
43   GSList    *l;
44   GList     *next;
45   GList     *element;
46   gboolean   first_item;
47
48   g_return_if_fail (element_ptr != NULL);
49
50   element = *element_ptr;
51   g_return_if_fail (element != NULL);
52
53   next = element->next;
54   first_item = (element->prev == NULL);
55
56   g_list_remove_link (NULL, element);
57
58   for (l = working_list; l; l = l->next)
59     {
60        Iteration *i = l->data;
61
62        if (i->iterator == element)
63          {
64            i->iterator = next;
65          }
66
67        if (first_item && *(i->list) == element)
68          {
69            *(i->list) = next;
70          }
71     }
72
73   g_list_free_1 (element);
74 }
75
76 void
77 spi_re_entrant_list_foreach (GList         **list,
78                              SpiReEntrantFn  func,
79                              gpointer        user_data)
80 {
81         Iteration i;
82
83         if (!list)
84           {
85             return;
86           }
87
88         i.list = list;
89         i.iterator = *list;
90
91         working_list = g_slist_prepend (working_list, &i);
92
93         while (i.iterator) {
94                 GList *l = i.iterator;
95
96                 func (&i.iterator, user_data);
97
98                 if (i.iterator == l)
99                         i.iterator = i.iterator->next;
100         }
101
102         working_list = g_slist_remove (working_list, &i);
103 }