2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * gstiterator.h: Header for GstIterator
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #ifndef __GST_ITERATOR_H__
23 #define __GST_ITERATOR_H__
25 #include <glib-object.h> /* for GValue in the fold */
26 #include <gst/gstconfig.h>
30 #define GST_TYPE_ITERATOR (gst_iterator_get_type ())
34 * @GST_ITERATOR_DONE: No more items in the iterator
35 * @GST_ITERATOR_OK: An item was retrieved
36 * @GST_ITERATOR_RESYNC: Datastructure changed while iterating
37 * @GST_ITERATOR_ERROR: An error happened
39 * The result of gst_iterator_next().
42 GST_ITERATOR_DONE = 0,
44 GST_ITERATOR_RESYNC = 2,
45 GST_ITERATOR_ERROR = 3
48 typedef struct _GstIterator GstIterator;
52 * @GST_ITERATOR_ITEM_SKIP: Skip this item
53 * @GST_ITERATOR_ITEM_PASS: Return item
54 * @GST_ITERATOR_ITEM_END: Stop after this item.
56 * The result of a #GstIteratorItemFunction.
59 GST_ITERATOR_ITEM_SKIP = 0,
60 GST_ITERATOR_ITEM_PASS = 1,
61 GST_ITERATOR_ITEM_END = 2
65 * GstIteratorDisposeFunction:
66 * @owner: the owner of the iterator
68 * The function that will be called when a #GList iterator is freed. The
69 * owner of the #GList iterator can then clean up its resources.
71 typedef void (*GstIteratorDisposeFunction) (gpointer owner);
74 * GstIteratorNextFunction:
76 * @result: a pointer to hold the next item
78 * The function that will be called when the next element of the iterator
79 * should be retrieved.
81 * Implementors of a #GstIterator should implement this
82 * function and pass it to the constructor of the custom iterator.
83 * The function will be called with the iterator lock held.
85 * Returns: the result of the operation.
87 typedef GstIteratorResult (*GstIteratorNextFunction) (GstIterator *it, gpointer *result);
89 * GstIteratorItemFunction:
91 * @item: the item being retrieved.
93 * The function that will be called after the next item of the iterator
94 * has been retrieved. This function will typically increase the refcount
95 * of the item or make a copy.
97 * Implementors of a #GstIterator should implement this
98 * function and pass it to the constructor of the custom iterator.
99 * The function will be called with the iterator lock held.
101 * Returns: the result of the operation.
103 typedef GstIteratorItem (*GstIteratorItemFunction) (GstIterator *it, gpointer item);
105 * GstIteratorResyncFunction:
108 * This function will be called whenever a concurrent update happened
109 * to the iterated datastructure. The implementor of the iterator should
110 * restart the iterator from the beginning and clean up any state it might
113 * Implementors of a #GstIterator should implement this
114 * function and pass it to the constructor of the custom iterator.
115 * The function will be called with the iterator lock held.
117 typedef void (*GstIteratorResyncFunction) (GstIterator *it);
119 * GstIteratorFreeFunction:
122 * This function will be called when the iterator is freed.
124 * Implementors of a #GstIterator should implement this
125 * function and pass it to the constructor of the custom iterator.
126 * The function will be called with the iterator lock held.
128 typedef void (*GstIteratorFreeFunction) (GstIterator *it);
131 * GstIteratorFoldFunction:
132 * @item: the item to fold
133 * @ret: a #GValue collecting the result
134 * @user_data: data passed to gst_iterator_fold()
136 * A function to be passed to gst_iterator_fold().
138 * Returns: TRUE if the fold should continue, FALSE if it should stop.
140 typedef gboolean (*GstIteratorFoldFunction) (gpointer item, GValue *ret, gpointer user_data);
144 * @object: The object to copy
146 * A function to create a copy of some object or
147 * increase its reference count.
149 * Returns: a copy of the object or the same object with increased reference count
153 typedef gpointer (*GstCopyFunction) (gpointer object);
157 * @it: the #GstIterator value
159 * Macro to cast to a #GstIterator
161 #define GST_ITERATOR(it) ((GstIterator*)(it))
164 * @it: the #GstIterator to get the lock of
166 * Macro to get the lock protecting the datastructure being iterated.
168 #define GST_ITERATOR_LOCK(it) (GST_ITERATOR(it)->lock)
170 * GST_ITERATOR_COOKIE:
171 * @it: the #GstIterator to get the cookie of
173 * Macro to get the cookie of a #GstIterator. The cookie of the
174 * iterator is the value of the master cookie when the iterator
176 * Whenever the iterator is iterated, the value is compared to the
177 * value of the master cookie. If they are different, a concurrent
178 * modification happened to the iterator and a resync is needed.
180 #define GST_ITERATOR_COOKIE(it) (GST_ITERATOR(it)->cookie)
182 * GST_ITERATOR_ORIG_COOKIE:
183 * @it: the #GstIterator to get the master cookie of
185 * Macro to get a pointer to where the master cookie is stored. The
186 * master cookie protects the structure being iterated and gets updated
187 * whenever the datastructure changes.
189 #define GST_ITERATOR_ORIG_COOKIE(it) (GST_ITERATOR(it)->master_cookie)
193 * @next: The function to get the next item in the iterator
194 * @item: The function to be called for each item retrieved
195 * @resync: The function to call when a resync is needed.
196 * @free: The function to call when the iterator is freed
197 * @pushed: The iterator that is currently pushed with gst_iterator_push()
198 * @type: The type of the object that this iterator will return
199 * @lock: The lock protecting the data structure and the cookie.
200 * @cookie: The cookie; the value of the master_cookie when this iterator was
202 * @master_cookie: A pointer to the master cookie.
204 * #GstIterator base structure. The values of this structure are
205 * protected for subclasses, use the methods to use the #GstIterator.
207 struct _GstIterator {
209 GstIteratorNextFunction next;
210 GstIteratorItemFunction item;
211 GstIteratorResyncFunction resync;
212 GstIteratorFreeFunction free;
214 GstIterator *pushed; /* pushed iterator */
218 guint32 cookie; /* cookie of the iterator */
219 guint32 *master_cookie; /* pointer to guint32 holding the cookie when this
220 iterator was created */
224 gpointer _gst_reserved[GST_PADDING-1];
227 GType gst_iterator_get_type (void);
229 /* creating iterators */
230 GstIterator* gst_iterator_new (guint size,
233 guint32 *master_cookie,
234 GstIteratorNextFunction next,
235 GstIteratorItemFunction item,
236 GstIteratorResyncFunction resync,
237 GstIteratorFreeFunction free);
239 GstIterator* gst_iterator_new_list (GType type,
241 guint32 *master_cookie,
244 GstIteratorItemFunction item,
245 GstIteratorDisposeFunction free);
247 GstIterator* gst_iterator_new_single (GType type,
249 GstCopyFunction copy,
252 /* using iterators */
253 GstIteratorResult gst_iterator_next (GstIterator *it, gpointer *elem);
254 void gst_iterator_resync (GstIterator *it);
255 void gst_iterator_free (GstIterator *it);
257 void gst_iterator_push (GstIterator *it, GstIterator *other);
259 /* higher-order functions that operate on iterators */
260 GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func,
262 GstIteratorResult gst_iterator_fold (GstIterator *it,
263 GstIteratorFoldFunction func,
264 GValue *ret, gpointer user_data);
265 GstIteratorResult gst_iterator_foreach (GstIterator *it,
266 GFunc func, gpointer user_data);
267 gpointer gst_iterator_find_custom (GstIterator *it, GCompareFunc func,
272 #endif /* __GST_ITERATOR_H__ */