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  * |[<!-- language="C" -->
50  * GstIterator *it = _get_iterator(object);
51  * GValue item = G_VALUE_INIT;
52  * done = FALSE;
53  * while (!done) {
54  * switch (gst_iterator_next (it, &amp;item)) {
55  * case GST_ITERATOR_OK:
56  * ...get/use/change item here...
57  * g_value_reset (&amp;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 (&amp;item);
73  * gst_iterator_free (it);
74  * ]|
75  */
76 public class Iterator
77 {
78 	/** the main Gtk struct */
79 	protected GstIterator* gstIterator;
80 	protected bool ownedRef;
81 
82 	/** Get the main Gtk struct */
83 	public GstIterator* getIteratorStruct()
84 	{
85 		return gstIterator;
86 	}
87 
88 	/** the main Gtk struct as a void* */
89 	protected void* getStruct()
90 	{
91 		return cast(void*)gstIterator;
92 	}
93 
94 	/**
95 	 * Sets our main struct and passes it to the parent class.
96 	 */
97 	public this (GstIterator* gstIterator, bool ownedRef = false)
98 	{
99 		this.gstIterator = gstIterator;
100 		this.ownedRef = ownedRef;
101 	}
102 
103 
104 	/** */
105 	public static GType getType()
106 	{
107 		return gst_iterator_get_type();
108 	}
109 
110 	/**
111 	 * Create a new iterator. This function is mainly used for objects
112 	 * implementing the next/resync/free function to iterate a data structure.
113 	 *
114 	 * For each item retrieved, the @item function is called with the lock
115 	 * held. The @free function is called when the iterator is freed.
116 	 *
117 	 * Params:
118 	 *     size = the size of the iterator structure
119 	 *     type = #GType of children
120 	 *     lock = pointer to a #GMutex.
121 	 *     masterCookie = pointer to a guint32 that is changed when the items in the
122 	 *         iterator changed.
123 	 *     copy = copy function
124 	 *     next = function to get next item
125 	 *     item = function to call on each item retrieved
126 	 *     resync = function to resync the iterator
127 	 *     free = function to free the iterator
128 	 *
129 	 * Return: the new #GstIterator.
130 	 *
131 	 *     MT safe.
132 	 *
133 	 * Throws: ConstructionException GTK+ fails to create the object.
134 	 */
135 	public this(uint size, GType type, Mutex lock, uint* masterCookie, GstIteratorCopyFunction copy, GstIteratorNextFunction next, GstIteratorItemFunction item, GstIteratorResyncFunction resync, GstIteratorFreeFunction free)
136 	{
137 		auto p = gst_iterator_new(size, type, (lock is null) ? null : lock.getMutexStruct(), masterCookie, copy, next, item, resync, free);
138 		
139 		if(p is null)
140 		{
141 			throw new ConstructionException("null returned by new");
142 		}
143 		
144 		this(cast(GstIterator*) p);
145 	}
146 
147 	/**
148 	 * Create a new iterator designed for iterating @list.
149 	 *
150 	 * The list you iterate is usually part of a data structure @owner and is
151 	 * protected with @lock.
152 	 *
153 	 * The iterator will use @lock to retrieve the next item of the list and it
154 	 * will then call the @item function before releasing @lock again.
155 	 *
156 	 * When a concurrent update to the list is performed, usually by @owner while
157 	 * holding @lock, @master_cookie will be updated. The iterator implementation
158 	 * will notice the update of the cookie and will return %GST_ITERATOR_RESYNC to
159 	 * the user of the iterator in the next call to gst_iterator_next().
160 	 *
161 	 * Params:
162 	 *     type = #GType of elements
163 	 *     lock = pointer to a #GMutex protecting the list.
164 	 *     masterCookie = pointer to a guint32 that is incremented when the list
165 	 *         is changed.
166 	 *     list = pointer to the list
167 	 *     owner = object owning the list
168 	 *     item = function to call on each item retrieved
169 	 *
170 	 * Return: the new #GstIterator for @list.
171 	 *
172 	 *     MT safe.
173 	 *
174 	 * Throws: ConstructionException GTK+ fails to create the object.
175 	 */
176 	public this(GType type, Mutex lock, uint* masterCookie, ref ListG list, ObjectG owner, GstIteratorItemFunction item)
177 	{
178 		GList* outlist = list.getListGStruct();
179 		
180 		auto p = gst_iterator_new_list(type, (lock is null) ? null : lock.getMutexStruct(), masterCookie, &outlist, (owner is null) ? null : owner.getObjectGStruct(), item);
181 		
182 		if(p is null)
183 		{
184 			throw new ConstructionException("null returned by new_list");
185 		}
186 		
187 		list = new ListG(outlist);
188 		
189 		this(cast(GstIterator*) p);
190 	}
191 
192 	/**
193 	 * This #GstIterator is a convenient iterator for the common
194 	 * case where a #GstIterator needs to be returned but only
195 	 * a single object has to be considered. This happens often
196 	 * for the #GstPadIterIntLinkFunction.
197 	 *
198 	 * Params:
199 	 *     type = #GType of the passed object
200 	 *     object = object that this iterator should return
201 	 *
202 	 * Return: the new #GstIterator for @object.
203 	 *
204 	 * Throws: ConstructionException GTK+ fails to create the object.
205 	 */
206 	public this(GType type, Value object)
207 	{
208 		auto p = gst_iterator_new_single(type, (object is null) ? null : object.getValueStruct());
209 		
210 		if(p is null)
211 		{
212 			throw new ConstructionException("null returned by new_single");
213 		}
214 		
215 		this(cast(GstIterator*) p);
216 	}
217 
218 	/**
219 	 * Copy the iterator and its state.
220 	 *
221 	 * Return: a new copy of @it.
222 	 */
223 	public Iterator copy()
224 	{
225 		auto p = gst_iterator_copy(gstIterator);
226 		
227 		if(p is null)
228 		{
229 			return null;
230 		}
231 		
232 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p, true);
233 	}
234 
235 	/**
236 	 * Create a new iterator from an existing iterator. The new iterator
237 	 * will only return those elements that match the given compare function @func.
238 	 * The first parameter that is passed to @func is the #GValue of the current
239 	 * iterator element and the second parameter is @user_data. @func should
240 	 * return 0 for elements that should be included in the filtered iterator.
241 	 *
242 	 * When this iterator is freed, @it will also be freed.
243 	 *
244 	 * Params:
245 	 *     func = the compare function to select elements
246 	 *     userData = user data passed to the compare function
247 	 *
248 	 * Return: a new #GstIterator.
249 	 *
250 	 *     MT safe.
251 	 */
252 	public Iterator filter(GCompareFunc func, Value userData)
253 	{
254 		auto p = gst_iterator_filter(gstIterator, func, (userData is null) ? null : userData.getValueStruct());
255 		
256 		if(p is null)
257 		{
258 			return null;
259 		}
260 		
261 		return ObjectG.getDObject!(Iterator)(cast(GstIterator*) p, true);
262 	}
263 
264 	/**
265 	 * Find the first element in @it that matches the compare function @func.
266 	 * @func should return 0 when the element is found. The first parameter
267 	 * to @func will be the current element of the iterator and the
268 	 * second parameter will be @user_data.
269 	 * The result will be stored in @elem if a result is found.
270 	 *
271 	 * The iterator will not be freed.
272 	 *
273 	 * This function will return %FALSE if an error happened to the iterator
274 	 * or if the element wasn't found.
275 	 *
276 	 * Params:
277 	 *     func = the compare function to use
278 	 *     elem = pointer to a #GValue where to store the result
279 	 *     userData = user data passed to the compare function
280 	 *
281 	 * Return: Returns %TRUE if the element was found, else %FALSE.
282 	 *
283 	 *     MT safe.
284 	 */
285 	public bool findCustom(GCompareFunc func, out Value elem, void* userData)
286 	{
287 		GValue* outelem = gMalloc!GValue();
288 		
289 		auto p = gst_iterator_find_custom(gstIterator, func, outelem, userData) != 0;
290 		
291 		elem = ObjectG.getDObject!(Value)(outelem, true);
292 		
293 		return p;
294 	}
295 
296 	/**
297 	 * Folds @func over the elements of @iter. That is to say, @func will be called
298 	 * as @func (object, @ret, @user_data) for each object in @it. The normal use
299 	 * of this procedure is to accumulate the results of operating on the objects in
300 	 * @ret.
301 	 *
302 	 * This procedure can be used (and is used internally) to implement the
303 	 * gst_iterator_foreach() and gst_iterator_find_custom() operations.
304 	 *
305 	 * The fold will proceed as long as @func returns %TRUE. When the iterator has no
306 	 * more arguments, %GST_ITERATOR_DONE will be returned. If @func returns %FALSE,
307 	 * the fold will stop, and %GST_ITERATOR_OK will be returned. Errors or resyncs
308 	 * will cause fold to return %GST_ITERATOR_ERROR or %GST_ITERATOR_RESYNC as
309 	 * appropriate.
310 	 *
311 	 * The iterator will not be freed.
312 	 *
313 	 * Params:
314 	 *     func = the fold function
315 	 *     ret = the seed value passed to the fold function
316 	 *     userData = user data passed to the fold function
317 	 *
318 	 * Return: A #GstIteratorResult, as described above.
319 	 *
320 	 *     MT safe.
321 	 */
322 	public GstIteratorResult fold(GstIteratorFoldFunction func, Value ret, void* userData)
323 	{
324 		return gst_iterator_fold(gstIterator, func, (ret is null) ? null : ret.getValueStruct(), userData);
325 	}
326 
327 	/**
328 	 * Iterate over all element of @it and call the given function @func for
329 	 * each element.
330 	 *
331 	 * Params:
332 	 *     func = the function to call for each element.
333 	 *     userData = user data passed to the function
334 	 *
335 	 * Return: the result call to gst_iterator_fold(). The iterator will not be
336 	 *     freed.
337 	 *
338 	 *     MT safe.
339 	 */
340 	public GstIteratorResult foreac(GstIteratorForeachFunction func, void* userData)
341 	{
342 		return gst_iterator_foreach(gstIterator, func, userData);
343 	}
344 
345 	/**
346 	 * Free the iterator.
347 	 *
348 	 * MT safe.
349 	 */
350 	public void free()
351 	{
352 		gst_iterator_free(gstIterator);
353 	}
354 
355 	/**
356 	 * Get the next item from the iterator in @elem.
357 	 *
358 	 * Only when this function returns %GST_ITERATOR_OK, @elem will contain a valid
359 	 * value. @elem must have been initialized to the type of the iterator or
360 	 * initialized to zeroes with g_value_unset(). The caller is responsible for
361 	 * unsetting or resetting @elem with g_value_unset() or g_value_reset()
362 	 * after usage.
363 	 *
364 	 * When this function returns %GST_ITERATOR_DONE, no more elements can be
365 	 * retrieved from @it.
366 	 *
367 	 * A return value of %GST_ITERATOR_RESYNC indicates that the element list was
368 	 * concurrently updated. The user of @it should call gst_iterator_resync() to
369 	 * get the newly updated list.
370 	 *
371 	 * A return value of %GST_ITERATOR_ERROR indicates an unrecoverable fatal error.
372 	 *
373 	 * Params:
374 	 *     elem = pointer to hold next element
375 	 *
376 	 * Return: The result of the iteration. Unset @elem after usage.
377 	 *
378 	 *     MT safe.
379 	 */
380 	public GstIteratorResult next(out Value elem)
381 	{
382 		GValue* outelem = gMalloc!GValue();
383 		
384 		auto p = gst_iterator_next(gstIterator, outelem);
385 		
386 		elem = ObjectG.getDObject!(Value)(outelem, true);
387 		
388 		return p;
389 	}
390 
391 	/**
392 	 * Pushes @other iterator onto @it. All calls performed on @it are
393 	 * forwarded to @other. If @other returns %GST_ITERATOR_DONE, it is
394 	 * popped again and calls are handled by @it again.
395 	 *
396 	 * This function is mainly used by objects implementing the iterator
397 	 * next function to recurse into substructures.
398 	 *
399 	 * When gst_iterator_resync() is called on @it, @other will automatically be
400 	 * popped.
401 	 *
402 	 * MT safe.
403 	 *
404 	 * Params:
405 	 *     other = The #GstIterator to push
406 	 */
407 	public void push(Iterator other)
408 	{
409 		gst_iterator_push(gstIterator, (other is null) ? null : other.getIteratorStruct());
410 	}
411 
412 	/**
413 	 * Resync the iterator. this function is mostly called
414 	 * after gst_iterator_next() returned %GST_ITERATOR_RESYNC.
415 	 *
416 	 * When an iterator was pushed on @it, it will automatically be popped again
417 	 * with this function.
418 	 *
419 	 * MT safe.
420 	 */
421 	public void resync()
422 	{
423 		gst_iterator_resync(gstIterator);
424 	}
425 }