2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2011 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5 * gstiterator.h: Header for GstIterator
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #ifndef __GST_ITERATOR_H__
24 #define __GST_ITERATOR_H__
26 #include <glib-object.h> /* for GValue in the fold */
27 #include <gst/gstconfig.h>
31 #define GST_TYPE_ITERATOR (gst_iterator_get_type ())
35 * @GST_ITERATOR_DONE: No more items in the iterator
36 * @GST_ITERATOR_OK: An item was retrieved
37 * @GST_ITERATOR_RESYNC: Datastructure changed while iterating
38 * @GST_ITERATOR_ERROR: An error happened
40 * The result of gst_iterator_next().
43 GST_ITERATOR_DONE = 0,
45 GST_ITERATOR_RESYNC = 2,
46 GST_ITERATOR_ERROR = 3
49 typedef struct _GstIterator GstIterator;
53 * @GST_ITERATOR_ITEM_SKIP: Skip this item
54 * @GST_ITERATOR_ITEM_PASS: Return item
55 * @GST_ITERATOR_ITEM_END: Stop after this item.
57 * The result of a #GstIteratorItemFunction.
60 GST_ITERATOR_ITEM_SKIP = 0,
61 GST_ITERATOR_ITEM_PASS = 1,
62 GST_ITERATOR_ITEM_END = 2
66 * GstIteratorCopyFunction:
67 * @it: The original iterator
68 * @copy: The copied iterator
70 * This function will be called when creating a copy of @it and should
71 * create a copy of all custom iterator fields or increase their
74 typedef void (*GstIteratorCopyFunction) (const GstIterator *it, GstIterator *copy);
77 * GstIteratorItemFunction:
79 * @item: the item being retrieved.
81 * The function that will be called after the next item of the iterator
82 * has been retrieved. This function can be used to skip items or stop
85 * The function will be called with the iterator lock held.
87 * Returns: the result of the operation.
89 typedef GstIteratorItem (*GstIteratorItemFunction) (GstIterator *it, const GValue * item);
92 * GstIteratorNextFunction:
94 * @result: a pointer to hold the next item
96 * The function that will be called when the next element of the iterator
97 * should be retrieved.
99 * Implementors of a #GstIterator should implement this
100 * function and pass it to the constructor of the custom iterator.
101 * The function will be called with the iterator lock held.
103 * Returns: the result of the operation.
105 typedef GstIteratorResult (*GstIteratorNextFunction) (GstIterator *it, GValue *result);
107 * GstIteratorResyncFunction:
110 * This function will be called whenever a concurrent update happened
111 * to the iterated datastructure. The implementor of the iterator should
112 * restart the iterator from the beginning and clean up any state it might
115 * Implementors of a #GstIterator should implement this
116 * function and pass it to the constructor of the custom iterator.
117 * The function will be called with the iterator lock held.
119 typedef void (*GstIteratorResyncFunction) (GstIterator *it);
121 * GstIteratorFreeFunction:
124 * This function will be called when the iterator is freed.
126 * Implementors of a #GstIterator should implement this
127 * function and pass it to the constructor of the custom iterator.
128 * The function will be called with the iterator lock held.
130 typedef void (*GstIteratorFreeFunction) (GstIterator *it);
133 * GstIteratorForeachFunction:
135 * @user_data: User data
137 * A function that is called by gst_iterator_foreach() for every element.
139 typedef void (*GstIteratorForeachFunction) (const GValue * item, gpointer user_data);
142 * GstIteratorFoldFunction:
143 * @item: the item to fold
144 * @ret: a #GValue collecting the result
145 * @user_data: data passed to gst_iterator_fold()
147 * A function to be passed to gst_iterator_fold().
149 * Returns: %TRUE if the fold should continue, %FALSE if it should stop.
151 typedef gboolean (*GstIteratorFoldFunction) (const GValue * item, GValue * ret, gpointer user_data);
155 * @it: the #GstIterator value
157 * Macro to cast to a #GstIterator
159 #define GST_ITERATOR(it) ((GstIterator*)(it))
162 * @it: the #GstIterator to get the lock of
164 * Macro to get the lock protecting the datastructure being iterated.
166 #define GST_ITERATOR_LOCK(it) (GST_ITERATOR(it)->lock)
168 * GST_ITERATOR_COOKIE:
169 * @it: the #GstIterator to get the cookie of
171 * Macro to get the cookie of a #GstIterator. The cookie of the
172 * iterator is the value of the master cookie when the iterator
174 * Whenever the iterator is iterated, the value is compared to the
175 * value of the master cookie. If they are different, a concurrent
176 * modification happened to the iterator and a resync is needed.
178 #define GST_ITERATOR_COOKIE(it) (GST_ITERATOR(it)->cookie)
180 * GST_ITERATOR_ORIG_COOKIE:
181 * @it: the #GstIterator to get the master cookie of
183 * Macro to get a pointer to where the master cookie is stored. The
184 * master cookie protects the structure being iterated and gets updated
185 * whenever the datastructure changes.
187 #define GST_ITERATOR_ORIG_COOKIE(it) (GST_ITERATOR(it)->master_cookie)
191 * @copy: The function to copy the iterator
192 * @next: The function to get the next item in the iterator
193 * @item: The function to be called for each item retrieved
194 * @resync: The function to call when a resync is needed.
195 * @free: The function to call when the iterator is freed
196 * @pushed: The iterator that is currently pushed with gst_iterator_push()
197 * @type: The type of the object that this iterator will return
198 * @lock: The lock protecting the data structure and the cookie.
199 * @cookie: The cookie; the value of the master_cookie when this iterator was
201 * @master_cookie: A pointer to the master cookie.
202 * @size: the size of the iterator
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 GstIteratorCopyFunction copy;
210 GstIteratorNextFunction next;
211 GstIteratorItemFunction item;
212 GstIteratorResyncFunction resync;
213 GstIteratorFreeFunction free;
215 GstIterator *pushed; /* pushed iterator */
219 guint32 cookie; /* cookie of the iterator */
220 guint32 *master_cookie; /* pointer to guint32 holding the cookie when this
221 iterator was created */
225 gpointer _gst_reserved[GST_PADDING];
229 GType gst_iterator_get_type (void);
231 /* creating iterators */
234 GstIterator* gst_iterator_new (guint size,
237 guint32 *master_cookie,
238 GstIteratorCopyFunction copy,
239 GstIteratorNextFunction next,
240 GstIteratorItemFunction item,
241 GstIteratorResyncFunction resync,
242 GstIteratorFreeFunction free) G_GNUC_MALLOC;
244 GstIterator* gst_iterator_new_list (GType type,
246 guint32 *master_cookie,
249 GstIteratorItemFunction item) G_GNUC_MALLOC;
251 GstIterator* gst_iterator_new_single (GType type,
252 const GValue * object) G_GNUC_MALLOC;
254 GstIterator* gst_iterator_copy (const GstIterator *it) G_GNUC_MALLOC;
256 /* using iterators */
259 GstIteratorResult gst_iterator_next (GstIterator *it, GValue * elem);
262 void gst_iterator_resync (GstIterator *it);
265 void gst_iterator_free (GstIterator *it);
268 void gst_iterator_push (GstIterator *it, GstIterator *other);
270 /* higher-order functions that operate on iterators */
273 GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func,
274 const GValue * user_data) G_GNUC_MALLOC;
276 GstIteratorResult gst_iterator_fold (GstIterator *it,
277 GstIteratorFoldFunction func,
278 GValue *ret, gpointer user_data);
280 GstIteratorResult gst_iterator_foreach (GstIterator *it,
281 GstIteratorForeachFunction func, gpointer user_data);
283 gboolean gst_iterator_find_custom (GstIterator *it, GCompareFunc func,
284 GValue *elem, gpointer user_data);
286 #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
287 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstIterator, gst_iterator_free)
292 #endif /* __GST_ITERATOR_H__ */