Add unit tests for some more methods
[glib.git] / gobject / gobjectnotifyqueue.c
blob56422c2460f23c86cd7c7173591ad3f778e0b234
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.
20 #ifndef __G_OBJECT_NOTIFY_QUEUE_H__
21 #define __G_OBJECT_NOTIFY_QUEUE_H__
23 #include <string.h> /* memset */
25 #include <glib-object.h>
27 G_BEGIN_DECLS
30 /* --- typedefs --- */
31 typedef struct _GObjectNotifyContext GObjectNotifyContext;
32 typedef struct _GObjectNotifyQueue GObjectNotifyQueue;
33 typedef void (*GObjectNotifyQueueDispatcher) (GObject *object,
34 guint n_pspecs,
35 GParamSpec **pspecs);
38 /* --- structures --- */
39 struct _GObjectNotifyContext
41 GQuark quark_notify_queue;
42 GObjectNotifyQueueDispatcher dispatcher;
43 GTrashStack *_nqueue_trash; /* unused */
45 struct _GObjectNotifyQueue
47 GObjectNotifyContext *context;
48 GSList *pspecs;
49 guint16 n_pspecs;
50 guint16 freeze_count;
53 /* --- functions --- */
54 static void
55 g_object_notify_queue_free (gpointer data)
57 GObjectNotifyQueue *nqueue = data;
59 g_slist_free (nqueue->pspecs);
60 g_slice_free (GObjectNotifyQueue, nqueue);
63 static inline GObjectNotifyQueue*
64 g_object_notify_queue_freeze (GObject *object,
65 GObjectNotifyContext *context)
67 GObjectNotifyQueue *nqueue;
69 nqueue = g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
70 if (!nqueue)
72 nqueue = g_slice_new0 (GObjectNotifyQueue);
73 nqueue->context = context;
74 g_datalist_id_set_data_full (&object->qdata, context->quark_notify_queue,
75 nqueue, g_object_notify_queue_free);
78 g_return_val_if_fail (nqueue->freeze_count < 65535, nqueue);
79 nqueue->freeze_count++;
81 return nqueue;
84 static inline void
85 g_object_notify_queue_thaw (GObject *object,
86 GObjectNotifyQueue *nqueue)
88 GObjectNotifyContext *context = nqueue->context;
89 GParamSpec *pspecs_mem[16], **pspecs, **free_me = NULL;
90 GSList *slist;
91 guint n_pspecs = 0;
93 g_return_if_fail (nqueue->freeze_count > 0);
95 nqueue->freeze_count--;
96 if (nqueue->freeze_count)
97 return;
98 g_return_if_fail (object->ref_count > 0);
100 pspecs = nqueue->n_pspecs > 16 ? free_me = g_new (GParamSpec*, nqueue->n_pspecs) : pspecs_mem;
101 /* set first entry to NULL since it's checked unconditionally */
102 pspecs[0] = NULL;
103 for (slist = nqueue->pspecs; slist; slist = slist->next)
105 GParamSpec *pspec = slist->data;
106 guint i = 0;
108 /* dedup, make pspecs in the list unique */
109 redo_dedup_check:
110 if (pspecs[i] == pspec)
111 continue;
112 if (++i < n_pspecs)
113 goto redo_dedup_check;
115 pspecs[n_pspecs++] = pspec;
117 g_datalist_id_set_data (&object->qdata, context->quark_notify_queue, NULL);
119 if (n_pspecs)
120 context->dispatcher (object, n_pspecs, pspecs);
121 g_free (free_me);
124 static inline void
125 g_object_notify_queue_clear (GObject *object,
126 GObjectNotifyQueue *nqueue)
128 g_return_if_fail (nqueue->freeze_count > 0);
130 g_slist_free (nqueue->pspecs);
131 nqueue->pspecs = NULL;
132 nqueue->n_pspecs = 0;
135 static inline void
136 g_object_notify_queue_add (GObject *object,
137 GObjectNotifyQueue *nqueue,
138 GParamSpec *pspec)
140 if (pspec->flags & G_PARAM_READABLE)
142 GParamSpec *redirect;
144 g_return_if_fail (nqueue->n_pspecs < 65535);
146 redirect = g_param_spec_get_redirect_target (pspec);
147 if (redirect)
148 pspec = redirect;
150 /* we do the deduping in _thaw */
151 nqueue->pspecs = g_slist_prepend (nqueue->pspecs, pspec);
152 nqueue->n_pspecs++;
156 static inline GObjectNotifyQueue*
157 g_object_notify_queue_from_object (GObject *object,
158 GObjectNotifyContext *context)
160 return g_datalist_id_get_data (&object->qdata, context->quark_notify_queue);
164 G_END_DECLS
166 #endif /* __G_OBJECT_NOTIFY_QUEUE_H__ */