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