Documentation fixes.
[glib.git] / gobject / gobjectnotifyqueue.c
blobf2c0873244768f30404ccb4ef20c0fea0d90b9b1
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #ifndef __G_NOTIFY_H__
20 #define __G_NOTIFY_H__
22 #include <string.h> /* memset */
23 #include <glib-object.h>
25 G_BEGIN_DECLS
28 /* --- typedefs --- */
29 typedef struct _GObjectNotifyContext GObjectNotifyContext;
30 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
31 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
32 guint n_pspecs,
33 GParamSpec **pspecs);
36 /* --- structures --- */
37 struct _GObjectNotifyContext
39 GQuark quark_notify_queue;
40 GObjectNotifyQueueDispatcher dispatcher;
41 GTrashStack *nqueue_trash;
43 struct _GObjectNotifyQueue
45 GObjectNotifyContext *context;
46 GSList *pspecs;
47 guint n_pspecs;
48 guint freeze_count;
52 /* --- functions --- */
53 static void
54 g_object_notify_queue_free (gpointer data)
56 GObjectNotifyQueue *nqueue = data;
58 g_slist_free (nqueue->pspecs);
59 g_trash_stack_push (&nqueue->context->nqueue_trash, nqueue);
62 static inline GObjectNotifyQueue*
63 g_object_notify_queue_freeze (GObject *object,
64 GObjectNotifyContext *context)
66 GObjectNotifyQueue *nqueue;
68 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
69 if (!nqueue)
71 nqueue = g_trash_stack_pop (&context->nqueue_trash);
72 if (!nqueue)
74 guint i;
76 nqueue = g_new (GObjectNotifyQueue, 16);
77 for (i = 0; i < 15; i++)
78 g_trash_stack_push (&context->nqueue_trash, nqueue++);
80 memset (nqueue, 0, sizeof (*nqueue));
81 nqueue->context = context;
82 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
83 nqueue, g_object_notify_queue_free);
85 nqueue->freeze_count++;
87 return nqueue;
90 static inline void
91 g_object_notify_queue_thaw (GObject *object,
92 GObjectNotifyQueue *nqueue)
94 GObjectNotifyContext *context = nqueue->context;
95 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
96 GSList *slist;
97 guint n_pspecs = 0;
99 g_return_if_fail (nqueue->freeze_count > 0);
101 nqueue->freeze_count--;
102 if (nqueue->freeze_count)
103 return;
104 g_return_if_fail (object->ref_count > 0);
106 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
107 /* set first entry to NULL since it's checked unconditionally */
108 pspecs[0] = NULL;
109 for (slist = nqueue->pspecs; slist; slist = slist->next)
111 GParamSpec *pspec = slist->data;
112 gint i = 0;
114 /* dedup, make pspecs in the list unique */
115 redo_dedup_check:
116 if (pspecs[i] == pspec)
117 continue;
118 if (++i < n_pspecs)
119 goto redo_dedup_check;
121 pspecs[n_pspecs++] = pspec;
123 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
125 if (n_pspecs)
126 context->dispatcher (object, n_pspecs, pspecs);
127 g_free (free_me);
130 static inline void
131 g_object_notify_queue_clear (GObject *object,
132 GObjectNotifyQueue *nqueue)
134 g_return_if_fail (nqueue->freeze_count > 0);
136 g_slist_free (nqueue->pspecs);
137 nqueue->pspecs = NULL;
138 nqueue->n_pspecs = 0;
141 static inline void
142 g_object_notify_queue_add (GObject *object,
143 GObjectNotifyQueue *nqueue,
144 GParamSpec *pspec)
146 if (pspec->flags & G_PARAM_READABLE)
148 /* we do the deduping in _thaw */
149 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
150 nqueue->n_pspecs++;
154 static inline GObjectNotifyQueue*
155 g_object_notify_queue_from_object (GObject *object,
156 GObjectNotifyContext *context)
158 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
162 G_END_DECLS
164 #endif /* __G_OBJECT_H__ */