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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, 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 * @next: The function to get the next item in the iterator
192 * @item: The function to be called for each item retrieved
193 * @resync: The function to call when a resync is needed.
194 * @free: The function to call when the iterator is freed
195 * @pushed: The iterator that is currently pushed with gst_iterator_push()
196 * @type: The type of the object that this iterator will return
197 * @lock: The lock protecting the data structure and the cookie.
198 * @cookie: The cookie; the value of the master_cookie when this iterator was
200 * @master_cookie: A pointer to the master cookie.
202 * #GstIterator base structure. The values of this structure are
203 * protected for subclasses, use the methods to use the #GstIterator.
205 struct _GstIterator {
207 GstIteratorCopyFunction copy;
208 GstIteratorNextFunction next;
209 GstIteratorItemFunction item;
210 GstIteratorResyncFunction resync;
211 GstIteratorFreeFunction free;
213 GstIterator *pushed; /* pushed iterator */
217 guint32 cookie; /* cookie of the iterator */
218 guint32 *master_cookie; /* pointer to guint32 holding the cookie when this
219 iterator was created */
223 gpointer _gst_reserved[GST_PADDING];
226 GType gst_iterator_get_type (void);
228 /* creating iterators */
229 GstIterator* gst_iterator_new (guint size,
232 guint32 *master_cookie,
233 GstIteratorCopyFunction copy,
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);
246 GstIterator* gst_iterator_new_single (GType type,
247 const GValue * object);
249 GstIterator* gst_iterator_copy (const GstIterator *it);
251 /* using iterators */
252 GstIteratorResult gst_iterator_next (GstIterator *it, GValue * elem);
253 void gst_iterator_resync (GstIterator *it);
254 void gst_iterator_free (GstIterator *it);
256 void gst_iterator_push (GstIterator *it, GstIterator *other);
258 /* higher-order functions that operate on iterators */
259 GstIterator* gst_iterator_filter (GstIterator *it, GCompareFunc func,
260 const GValue * user_data);
261 GstIteratorResult gst_iterator_fold (GstIterator *it,
262 GstIteratorFoldFunction func,
263 GValue *ret, gpointer user_data);
264 GstIteratorResult gst_iterator_foreach (GstIterator *it,
265 GstIteratorForeachFunction func, gpointer user_data);
266 gboolean gst_iterator_find_custom (GstIterator *it, GCompareFunc func,
267 GValue *elem, gpointer user_data);
271 #endif /* __GST_ITERATOR_H__ */