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>
28 /* --- typedefs --- */
29 typedef struct _GObjectNotifyContext GObjectNotifyContext;
30 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
31 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
36 /* --- structures --- */
37 struct _GObjectNotifyContext
39 GQuark quark_notify_queue;
40 GObjectNotifyQueueDispatcher dispatcher;
41 GTrashStack *nqueue_trash;
43 struct _GObjectNotifyQueue
45 GObjectNotifyContext *context;
52 /* --- functions --- */
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);
71 nqueue = g_trash_stack_pop (&context->nqueue_trash);
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++;
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;
99 g_return_if_fail (nqueue->freeze_count > 0);
101 nqueue->freeze_count--;
102 if (nqueue->freeze_count)
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 */
109 for (slist = nqueue->pspecs; slist; slist = slist->next)
111 GParamSpec *pspec = slist->data;
114 /* dedup, make pspecs in the list unique */
116 if (pspecs[i] == pspec)
119 goto redo_dedup_check;
121 pspecs[n_pspecs++] = pspec;
123 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
126 context->dispatcher (object, n_pspecs, pspecs);
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;
142 g_object_notify_queue_add (GObject *object,
143 GObjectNotifyQueue *nqueue,
146 if (pspec->flags & G_PARAM_READABLE)
148 /* we do the deduping in _thaw */
149 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
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);
164 #endif /* __G_OBJECT_H__ */