1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Iterator;
26 
27 private import glib.ConstructionException;
28 private import glib.ListG;
29 private import glib.Mutex;
30 private import gobject.ObjectG;
31 private import gobject.Value;
32 private import gstreamerc.gstreamer;
33 public  import gstreamerc.gstreamertypes;
34 
35 
36 /**
37  * A GstIterator is used to retrieve multiple objects from another object in
38  * a threadsafe way.
39  * 
40  * Various GStreamer objects provide access to their internal structures using
41  * an iterator.
42  * 
43  * Note that if calling a GstIterator function results in your code receiving
44  * a refcounted object (with, say, g_value_get_object()), the refcount for that
45  * object will not be increased. Your code is responsible for taking a reference
46  * if it wants to continue using it later.
47  * 
48  * The basic use pattern of an iterator is as follows:
49  * |[
50  * GstIterator *it = _get_iterator(object);
51  * GValue item = G_VALUE_INIT;
52  * done = FALSE;
53  * while (!done) {
54  * switch (gst_iterator_next (it, &item)) {
55  * case GST_ITERATOR_OK:
56  * ...get/use/change item here...
57  * g_value_reset (&item);
58  * break;
59  * case GST_ITERATOR_RESYNC:
60  * ...rollback changes to items...
61  * gst_iterator_resync (it);
62  * break;
63  * case GST_ITERATOR_ERROR:
64  * ...wrong parameters were given...
65  * done = TRUE;
66  * break;
67  * case GST_ITERATOR_DONE:
68  * done = TRUE;
69  * break;
70  * }
71  * }
72  * g_value_unset (&item);
73  * gst_iterator_free (it);
74  * ]|
75  */
76 public class Iterator
77 {
78 	/** the main Gtk struct */
79 	protected GstIterator* gstIterator;
80 
81 	/** Get the main Gtk struct */
82 	public GstIterator* getIteratorStruct()
83 	{
84 		return gstIterator;
85 	}
86 
87 	/** the main Gtk struct as a void* */
88 	protected void* getStruct()
89 	{
90 		return cast(void*)gstIterator;
91 	}
92 
93 	/**
94 	 * Sets our main struct and passes it to the parent class.
95 	 */
96 	public this (GstIterator* gstIterator)
97 	{
98 		this.gstIterator = gstIterator;
99 	}
100 
101 
102 	/** */
103 	public static GType getType()
104 	{
105 		return gst_iterator_get_type();
106 	}
107 
108 	/**
109 	 * Create a new iterator. This function is mainly used for objects
110 	 * implementing the next/resync/free function to iterate a data structure.
111 	 *
112 	 * For each item retrieved, the @item function is called with the lock
113 	 * held. The @free function is called when the iterator is freed.
114 	 *
115 	 * Params:
116 	 *     size = the size of the iterator structure
117 	 *     type = #GType of children
118 	 *     lock = pointer to a #GMutex.
119 	 *     masterCookie = pointer to a guint32 that is changed when the items in the
120 	 *         iterator changed.
121 	 *     copy = copy function
122 	 *     next = function to get next item
123 	 *     item = function to call on each item retrieved
124 	 *     resync = function to resync the iterator
125 	 *     free = function to free the iterator
126 	 *
127 	 * Return: the new #GstIterator.
128 	 *
129 	 *     MT safe.
130 	 *
131 	 * Throws: ConstructionException GTK+ fails to create the object.
132 	 */
133 	public this(uint size, GType type, Mutex lock, uint* masterCookie, GstIteratorCopyFunction copy, GstIteratorNextFunction next, GstIteratorItemFunction item, GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
134 	{
135 		auto p = gst_iterator_new(size, type, (lock is null) ? null : lock.getMutexStruct(), masterCookie, copy, next, item, resync, free);
136 		
137 		if(p is null)
138 		{
139 			throw new ConstructionException("null returned by new");
140 		}
141 		
142 		this(cast(GstIterator*) p);
143 	}
144 
145 	/**
146 	 * Create a new iterator designed for iterating @list.
147 	 *
148 	 * The list you iterate is usually part of a data structure @owner and is
149 	 * protected with @lock.
150 	 *
151 	 * The iterator will use @lock to retrieve the next item of the list and it
152 	 * will then call the @item function before releasing @lock again.
153 	 *
154 	 * When a concurrent update to the list is performed, usually by @owner while
155 	 * holding @lock, @master_cookie will be updated. The iterator implementation
156 	 * will notice the update of the cookie and will return %GST_ITERATOR_RESYNC to
157 	 * the user of the iterator in the next call to gst_iterator_next().
158 	 *
159 	 * Params:
160 	 *     type = #GType of elements
161 	 *     lock = pointer to a #GMutex protecting the list.
162 	 *     masterCookie = pointer to a guint32 that is incremented when the list
163 	 *         is changed.
164 	 *     list = pointer to the list
165 	 *     owner = object owning the list
166 	 *     item = function to call on each item retrieved
167 	 *
168 	 * Return: the new #GstIterator for @list.
169 	 *
170 	 *     MT safe.
171 	 *
172 	 * Throws: ConstructionException GTK+ fails to create the object.
173 	 */
174 	public this(GType type, Mutex lock, uint* masterCookie, ref ListG list, ObjectG owner, GstIteratorItemFunction item)
175 	{
176 		GList* outlist = list.getListGStruct();
177 		
178 		auto p = gst_iterator_new_list(type, (lock is null) ? null : lock.getMutexStruct(), masterCookie, &outlist, (owner is null) ? null : owner.getObjectGStruct(), item);
179 		
180 		if(p is null)
181 		{
182 			throw new ConstructionException("null returned by new_list");
183 		}
184 		
185 		list = new ListG(outlist);
186 		
187 		this(cast(GstIterator*) p);
188 	}
189 
190 	/**
191 	 * This #GstIterator is a convenient iterator for the common
192 	 * case where a #GstIterator needs to be returned but only
193 	 * a single object has to be considered. This happens often
194 	 * for the #GstPadIterIntLinkFunction.
195 	 *
196 	 * Params:
197 	 *     type = #GType of the passed object
198 	 *     object = object that this iterator should return
199 	 *
200 	 * Return: the new #GstIterator for @object.
201 	 *
202 	 * Throws: ConstructionException GTK+ fails to create the object.
203 	 */
204 	public this(GType type, Value object)
205 	{
206 		auto p = gst_iterator_new_single(type, (object is null) ? null : object.getValueStruct());
207 		
208 		if(p is null)
209 		{
210 			throw new ConstructionException("null returned by new_single");
211 		}
212 		
213 		this(cast(GstIterator*) p);
214 	}
215 
216 	/**
217 	 * Copy the iterator and its state.
218 	 *
219 	 * Return: a new copy of @it.
220 	 */
221 	public Iterator copy()
222 	{
223 		auto p = gst_iterator_copy(gstIterator);
224 		
225 		if(p is null)
226 		{
227 			return null;
228 		}
229 		
230 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
231 	}
232 
233 	/**
234 	 * Create a new iterator from an existing iterator. The new iterator
235 	 * will only return those elements that match the given compare function @func.
236 	 * The first parameter that is passed to @func is the #GValue of the current
237 	 * iterator element and the second parameter is @user_data. @func should
238 	 * return 0 for elements that should be included in the filtered iterator.
239 	 *
240 	 * When this iterator is freed, @it will also be freed.
241 	 *
242 	 * Params:
243 	 *     func = the compare function to select elements
244 	 *     userData = user data passed to the compare function
245 	 *
246 	 * Return: a new #GstIterator.
247 	 *
248 	 *     MT safe.
249 	 */
250 	public Iterator filter(GCompareFunc func, Value userData)
251 	{
252 		auto p = gst_iterator_filter(gstIterator, func, (userData is null) ? null : userData.getValueStruct());
253 		
254 		if(p is null)
255 		{
256 			return null;
257 		}
258 		
259 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p);
260 	}
261 
262 	/**
263 	 * Find the first element in @it that matches the compare function @func.
264 	 * @func should return 0 when the element is found. The first parameter
265 	 * to @func will be the current element of the iterator and the
266 	 * second parameter will be @user_data.
267 	 * The result will be stored in @elem if a result is found.
268 	 *
269 	 * The iterator will not be freed.
270 	 *
271 	 * This function will return %FALSE if an error happened to the iterator
272 	 * or if the element wasn't found.
273 	 *
274 	 * Params:
275 	 *     func = the compare function to use
276 	 *     elem = pointer to a #GValue where to store the result
277 	 *     userData = user data passed to the compare function
278 	 *
279 	 * Return: Returns %TRUE if the element was found, else %FALSE.
280 	 *
281 	 *     MT safe.
282 	 */
283 	public bool findCustom(GCompareFunc func, out Value elem, void* userData)
284 	{
285 		GValue* outelem = gMalloc!GValue();
286 		
287 		auto p = gst_iterator_find_custom(gstIterator, func, outelem, userData) != 0;
288 		
289 		elem = ObjectG.getDObject!(Value)(outelem);
290 		
291 		return p;
292 	}
293 
294 	/**
295 	 * Folds @func over the elements of @iter. That is to say, @func will be called
296 	 * as @func (object, @ret, @user_data) for each object in @it. The normal use
297 	 * of this procedure is to accumulate the results of operating on the objects in
298 	 * @ret.
299 	 *
300 	 * This procedure can be used (and is used internally) to implement the
301 	 * gst_iterator_foreach() and gst_iterator_find_custom() operations.
302 	 *
303 	 * The fold will proceed as long as @func returns %TRUE. When the iterator has no
304 	 * more arguments, %GST_ITERATOR_DONE will be returned. If @func returns %FALSE,
305 	 * the fold will stop, and %GST_ITERATOR_OK will be returned. Errors or resyncs
306 	 * will cause fold to return %GST_ITERATOR_ERROR or %GST_ITERATOR_RESYNC as
307 	 * appropriate.
308 	 *
309 	 * The iterator will not be freed.
310 	 *
311 	 * Params:
312 	 *     func = the fold function
313 	 *     ret = the seed value passed to the fold function
314 	 *     userData = user data passed to the fold function
315 	 *
316 	 * Return: A #GstIteratorResult, as described above.
317 	 *
318 	 *     MT safe.
319 	 */
320 	public GstIteratorResult fold(GstIteratorFoldFunction func, Value ret, void* userData)
321 	{
322 		return gst_iterator_fold(gstIterator, func, (ret is null) ? null : ret.getValueStruct(), userData);
323 	}
324 
325 	/**
326 	 * Iterate over all element of @it and call the given function @func for
327 	 * each element.
328 	 *
329 	 * Params:
330 	 *     func = the function to call for each element.
331 	 *     userData = user data passed to the function
332 	 *
333 	 * Return: the result call to gst_iterator_fold(). The iterator will not be
334 	 *     freed.
335 	 *
336 	 *     MT safe.
337 	 */
338 	public GstIteratorResult foreac(GstIteratorForeachFunction func, void* userData)
339 	{
340 		return gst_iterator_foreach(gstIterator, func, userData);
341 	}
342 
343 	/**
344 	 * Free the iterator.
345 	 *
346 	 * MT safe.
347 	 */
348 	public void free()
349 	{
350 		gst_iterator_free(gstIterator);
351 	}
352 
353 	/**
354 	 * Get the next item from the iterator in @elem.
355 	 *
356 	 * Only when this function returns %GST_ITERATOR_OK, @elem will contain a valid
357 	 * value. @elem must have been initialized to the type of the iterator or
358 	 * initialized to zeroes with g_value_unset(). The caller is responsible for
359 	 * unsetting or resetting @elem with g_value_unset() or g_value_reset()
360 	 * after usage.
361 	 *
362 	 * When this function returns %GST_ITERATOR_DONE, no more elements can be
363 	 * retrieved from @it.
364 	 *
365 	 * A return value of %GST_ITERATOR_RESYNC indicates that the element list was
366 	 * concurrently updated. The user of @it should call gst_iterator_resync() to
367 	 * get the newly updated list.
368 	 *
369 	 * A return value of %GST_ITERATOR_ERROR indicates an unrecoverable fatal error.
370 	 *
371 	 * Params:
372 	 *     elem = pointer to hold next element
373 	 *
374 	 * Return: The result of the iteration. Unset @elem after usage.
375 	 *
376 	 *     MT safe.
377 	 */
378 	public GstIteratorResult next(out Value elem)
379 	{
380 		GValue* outelem = gMalloc!GValue();
381 		
382 		auto p = gst_iterator_next(gstIterator, outelem);
383 		
384 		elem = ObjectG.getDObject!(Value)(outelem);
385 		
386 		return p;
387 	}
388 
389 	/**
390 	 * Pushes @other iterator onto @it. All calls performed on @it are
391 	 * forwarded to @other. If @other returns %GST_ITERATOR_DONE, it is
392 	 * popped again and calls are handled by @it again.
393 	 *
394 	 * This function is mainly used by objects implementing the iterator
395 	 * next function to recurse into substructures.
396 	 *
397 	 * When gst_iterator_resync() is called on @it, @other will automatically be
398 	 * popped.
399 	 *
400 	 * MT safe.
401 	 *
402 	 * Params:
403 	 *     other = The #GstIterator to push
404 	 */
405 	public void push(Iterator other)
406 	{
407 		gst_iterator_push(gstIterator, (other is null) ? null : other.getIteratorStruct());
408 	}
409 
410 	/**
411 	 * Resync the iterator. this function is mostly called
412 	 * after gst_iterator_next() returned %GST_ITERATOR_RESYNC.
413 	 *
414 	 * When an iterator was pushed on @it, it will automatically be popped again
415 	 * with this function.
416 	 *
417 	 * MT safe.
418 	 */
419 	public void resync()
420 	{
421 		gst_iterator_resync(gstIterator);
422 	}
423 }