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 <gobject/gobject.h>
27 /* --- typedefs --- */
28 typedef struct _GObjectNotifyContext GObjectNotifyContext;
29 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
30 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
35 /* --- structures --- */
36 struct _GObjectNotifyContext
38 GQuark quark_notify_queue;
39 GObjectNotifyQueueDispatcher dispatcher;
40 GTrashStack *nqueue_trash;
42 struct _GObjectNotifyQueue
44 GObjectNotifyContext *context;
51 /* --- functions --- */
53 g_object_notify_queue_free (gpointer data)
55 GObjectNotifyQueue *nqueue = data;
57 g_slist_free (nqueue->pspecs);
58 g_trash_stack_push (&nqueue->context->nqueue_trash, nqueue);
61 static inline GObjectNotifyQueue*
62 g_object_notify_queue_freeze (GObject *object,
63 GObjectNotifyContext *context)
65 GObjectNotifyQueue *nqueue;
67 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
70 nqueue = g_trash_stack_pop (&context->nqueue_trash);
75 nqueue = g_new (GObjectNotifyQueue, 16);
76 for (i = 0; i < 15; i++)
77 g_trash_stack_push (&context->nqueue_trash, nqueue++);
79 memset (nqueue, 0, sizeof (*nqueue));
80 nqueue->context = context;
81 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
82 nqueue, g_object_notify_queue_free);
84 nqueue->freeze_count++;
90 g_object_notify_queue_thaw (GObject *object,
91 GObjectNotifyQueue *nqueue)
93 GObjectNotifyContext *context = nqueue->context;
94 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
98 g_return_if_fail (nqueue->freeze_count > 0);
100 nqueue->freeze_count--;
101 if (nqueue->freeze_count)
103 g_return_if_fail (object->ref_count > 0);
105 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
106 for (slist = nqueue->pspecs; slist; slist = slist->next)
108 GParamSpec *pspec = slist->data;
111 /* dedup, make pspecs in the list unique */
113 if (pspecs[i] == pspec)
116 goto redo_dedup_check;
118 pspecs[n_pspecs++] = pspec;
120 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
123 context->dispatcher (object, n_pspecs, pspecs);
128 g_object_notify_queue_clear (GObject *object,
129 GObjectNotifyQueue *nqueue)
131 g_return_if_fail (nqueue->freeze_count > 0);
133 g_slist_free (nqueue->pspecs);
134 nqueue->pspecs = NULL;
135 nqueue->n_pspecs = 0;
139 g_object_notify_queue_add (GObject *object,
140 GObjectNotifyQueue *nqueue,
143 if (pspec->flags & G_PARAM_READABLE)
145 /* we do the deduping in _thaw */
146 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
151 static inline GObjectNotifyQueue*
152 g_object_notify_queue_from_object (GObject *object,
153 GObjectNotifyContext *context)
155 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
161 #endif /* __G_OBJECT_H__ */