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