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