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 glib.ListSG;
26 
27 private import gobject.ObjectG;
28 private import gtkc.glib;
29 public  import gtkc.glibtypes;
30 
31 
32 /**
33  * The #GSList struct is used for each element in the singly-linked
34  * list.
35  */
36 public class ListSG
37 {
38 	/** the main Gtk struct */
39 	protected GSList* gSList;
40 	protected bool ownedRef;
41 
42 	/** Get the main Gtk struct */
43 	public GSList* getListSGStruct(bool transferOwnership = false)
44 	{
45 		if (transferOwnership)
46 			ownedRef = false;
47 		return gSList;
48 	}
49 
50 	/** the main Gtk struct as a void* */
51 	protected void* getStruct()
52 	{
53 		return cast(void*)gSList;
54 	}
55 
56 	/**
57 	 * Sets our main struct and passes it to the parent class.
58 	 */
59 	public this (GSList* gSList, bool ownedRef = false)
60 	{
61 		this.gSList = gSList;
62 		this.ownedRef = ownedRef;
63 	}
64 
65 	/** */
66 	@property void* data()
67 	{
68 		return gSList.data;
69 	}
70 	
71 	/**
72 	 * get the next element
73 	 * Returns: the next element, or NULL if there are no more elements.
74 	 */
75 	@property ListSG next()
76 	{
77 		if ( gSList.next is null )
78 		{
79 			return null;
80 		}
81 		
82 		return new ListSG(gSList.next);
83 	}
84 	
85 	/**
86 	 * Turn the list into a D array of the desiered type.
87 	 * Type T wraps should match the type of the data.
88 	 */
89 	public T[] toArray(T, TC = typeof(T.tupleof[0]))()
90 	{
91 		T[] arr = new T[length()];
92 		ListSG list = this;
93 		size_t count;
94 		
95 		while(list !is null && count < arr.length)
96 		{
97 			arr[count] = ObjectG.getDObject!(T)(cast(TC)list.data);
98 			list = list.next();
99 			count++;
100 		}
101 		
102 		return arr;
103 	}
104 	
105 	unittest
106 	{
107 		import gobject.Value;
108 		
109 		auto list = new ListSG(null);
110 		list = list.append(new Value(0).getValueStruct());
111 		list = list.append(new Value(1).getValueStruct());
112 		auto arr = list.toArray!Value();
113 		
114 		assert(arr[0].getInt() == 0);
115 		assert(arr[1].getInt() == 1);
116 	}
117 
118 	/**
119 	 */
120 
121 	/**
122 	 * Allocates space for one #GSList element. It is called by the
123 	 * g_slist_append(), g_slist_prepend(), g_slist_insert() and
124 	 * g_slist_insert_sorted() functions and so is rarely used on its own.
125 	 *
126 	 * Returns: a pointer to the newly-allocated #GSList element.
127 	 */
128 	public static ListSG alloc()
129 	{
130 		auto p = g_slist_alloc();
131 		
132 		if(p is null)
133 		{
134 			return null;
135 		}
136 		
137 		return new ListSG(cast(GSList*) p);
138 	}
139 
140 	/**
141 	 * Adds a new element on to the end of the list.
142 	 *
143 	 * The return value is the new start of the list, which may
144 	 * have changed, so make sure you store the new value.
145 	 *
146 	 * Note that g_slist_append() has to traverse the entire list
147 	 * to find the end, which is inefficient when adding multiple
148 	 * elements. A common idiom to avoid the inefficiency is to prepend
149 	 * the elements and reverse the list when all elements have been added.
150 	 *
151 	 * |[<!-- language="C" -->
152 	 * // Notice that these are initialized to the empty list.
153 	 * GSList *list = NULL, *number_list = NULL;
154 	 *
155 	 * // This is a list of strings.
156 	 * list = g_slist_append (list, "first");
157 	 * list = g_slist_append (list, "second");
158 	 *
159 	 * // This is a list of integers.
160 	 * number_list = g_slist_append (number_list, GINT_TO_POINTER (27));
161 	 * number_list = g_slist_append (number_list, GINT_TO_POINTER (14));
162 	 * ]|
163 	 *
164 	 * Params:
165 	 *     data = the data for the new element
166 	 *
167 	 * Returns: the new start of the #GSList
168 	 */
169 	public ListSG append(void* data)
170 	{
171 		auto p = g_slist_append(gSList, data);
172 		
173 		if(p is null)
174 		{
175 			return null;
176 		}
177 		
178 		return new ListSG(cast(GSList*) p);
179 	}
180 
181 	/**
182 	 * Adds the second #GSList onto the end of the first #GSList.
183 	 * Note that the elements of the second #GSList are not copied.
184 	 * They are used directly.
185 	 *
186 	 * Params:
187 	 *     list2 = the #GSList to add to the end of the first #GSList
188 	 *
189 	 * Returns: the start of the new #GSList
190 	 */
191 	public ListSG concat(ListSG list2)
192 	{
193 		auto p = g_slist_concat(gSList, (list2 is null) ? null : list2.getListSGStruct());
194 		
195 		if(p is null)
196 		{
197 			return null;
198 		}
199 		
200 		return new ListSG(cast(GSList*) p);
201 	}
202 
203 	/**
204 	 * Copies a #GSList.
205 	 *
206 	 * Note that this is a "shallow" copy. If the list elements
207 	 * consist of pointers to data, the pointers are copied but
208 	 * the actual data isn't. See g_slist_copy_deep() if you need
209 	 * to copy the data as well.
210 	 *
211 	 * Returns: a copy of @list
212 	 */
213 	public ListSG copy()
214 	{
215 		auto p = g_slist_copy(gSList);
216 		
217 		if(p is null)
218 		{
219 			return null;
220 		}
221 		
222 		return new ListSG(cast(GSList*) p);
223 	}
224 
225 	/**
226 	 * Makes a full (deep) copy of a #GSList.
227 	 *
228 	 * In contrast with g_slist_copy(), this function uses @func to make a copy of
229 	 * each list element, in addition to copying the list container itself.
230 	 *
231 	 * @func, as a #GCopyFunc, takes two arguments, the data to be copied and a user
232 	 * pointer. It's safe to pass #NULL as user_data, if the copy function takes only
233 	 * one argument.
234 	 *
235 	 * For instance, if @list holds a list of GObjects, you can do:
236 	 * |[<!-- language="C" -->
237 	 * another_list = g_slist_copy_deep (list, (GCopyFunc) g_object_ref, NULL);
238 	 * ]|
239 	 *
240 	 * And, to entirely free the new list, you could do:
241 	 * |[<!-- language="C" -->
242 	 * g_slist_free_full (another_list, g_object_unref);
243 	 * ]|
244 	 *
245 	 * Params:
246 	 *     func = a copy function used to copy every element in the list
247 	 *     userData = user data passed to the copy function @func, or #NULL
248 	 *
249 	 * Returns: a full copy of @list, use #g_slist_free_full to free it
250 	 *
251 	 * Since: 2.34
252 	 */
253 	public ListSG copyDeep(GCopyFunc func, void* userData)
254 	{
255 		auto p = g_slist_copy_deep(gSList, func, userData);
256 		
257 		if(p is null)
258 		{
259 			return null;
260 		}
261 		
262 		return new ListSG(cast(GSList*) p);
263 	}
264 
265 	/**
266 	 * Removes the node link_ from the list and frees it.
267 	 * Compare this to g_slist_remove_link() which removes the node
268 	 * without freeing it.
269 	 *
270 	 * Removing arbitrary nodes from a singly-linked list requires time
271 	 * that is proportional to the length of the list (ie. O(n)). If you
272 	 * find yourself using g_slist_delete_link() frequently, you should
273 	 * consider a different data structure, such as the doubly-linked
274 	 * #GList.
275 	 *
276 	 * Params:
277 	 *     link = node to delete
278 	 *
279 	 * Returns: the new head of @list
280 	 */
281 	public ListSG deleteLink(ListSG link)
282 	{
283 		auto p = g_slist_delete_link(gSList, (link is null) ? null : link.getListSGStruct());
284 		
285 		if(p is null)
286 		{
287 			return null;
288 		}
289 		
290 		return new ListSG(cast(GSList*) p);
291 	}
292 
293 	/**
294 	 * Finds the element in a #GSList which
295 	 * contains the given data.
296 	 *
297 	 * Params:
298 	 *     data = the element data to find
299 	 *
300 	 * Returns: the found #GSList element,
301 	 *     or %NULL if it is not found
302 	 */
303 	public ListSG find(void* data)
304 	{
305 		auto p = g_slist_find(gSList, data);
306 		
307 		if(p is null)
308 		{
309 			return null;
310 		}
311 		
312 		return new ListSG(cast(GSList*) p);
313 	}
314 
315 	/**
316 	 * Finds an element in a #GSList, using a supplied function to
317 	 * find the desired element. It iterates over the list, calling
318 	 * the given function which should return 0 when the desired
319 	 * element is found. The function takes two #gconstpointer arguments,
320 	 * the #GSList element's data as the first argument and the
321 	 * given user data.
322 	 *
323 	 * Params:
324 	 *     data = user data passed to the function
325 	 *     func = the function to call for each element.
326 	 *         It should return 0 when the desired element is found
327 	 *
328 	 * Returns: the found #GSList element, or %NULL if it is not found
329 	 */
330 	public ListSG findCustom(void* data, GCompareFunc func)
331 	{
332 		auto p = g_slist_find_custom(gSList, data, func);
333 		
334 		if(p is null)
335 		{
336 			return null;
337 		}
338 		
339 		return new ListSG(cast(GSList*) p);
340 	}
341 
342 	/**
343 	 * Calls a function for each element of a #GSList.
344 	 *
345 	 * Params:
346 	 *     func = the function to call with each element's data
347 	 *     userData = user data to pass to the function
348 	 */
349 	public void foreac(GFunc func, void* userData)
350 	{
351 		g_slist_foreach(gSList, func, userData);
352 	}
353 
354 	/**
355 	 * Frees all of the memory used by a #GSList.
356 	 * The freed elements are returned to the slice allocator.
357 	 *
358 	 * If list elements contain dynamically-allocated memory,
359 	 * you should either use g_slist_free_full() or free them manually
360 	 * first.
361 	 */
362 	public void free()
363 	{
364 		g_slist_free(gSList);
365 	}
366 
367 	/**
368 	 * Frees one #GSList element.
369 	 * It is usually used after g_slist_remove_link().
370 	 */
371 	public void free1()
372 	{
373 		g_slist_free_1(gSList);
374 	}
375 
376 	/**
377 	 * Convenience method, which frees all the memory used by a #GSList, and
378 	 * calls the specified destroy function on every element's data.
379 	 *
380 	 * Params:
381 	 *     freeFunc = the function to be called to free each element's data
382 	 *
383 	 * Since: 2.28
384 	 */
385 	public void freeFull(GDestroyNotify freeFunc)
386 	{
387 		g_slist_free_full(gSList, freeFunc);
388 	}
389 
390 	/**
391 	 * Gets the position of the element containing
392 	 * the given data (starting from 0).
393 	 *
394 	 * Params:
395 	 *     data = the data to find
396 	 *
397 	 * Returns: the index of the element containing the data,
398 	 *     or -1 if the data is not found
399 	 */
400 	public int index(void* data)
401 	{
402 		return g_slist_index(gSList, data);
403 	}
404 
405 	/**
406 	 * Inserts a new element into the list at the given position.
407 	 *
408 	 * Params:
409 	 *     data = the data for the new element
410 	 *     position = the position to insert the element.
411 	 *         If this is negative, or is larger than the number
412 	 *         of elements in the list, the new element is added on
413 	 *         to the end of the list.
414 	 *
415 	 * Returns: the new start of the #GSList
416 	 */
417 	public ListSG insert(void* data, int position)
418 	{
419 		auto p = g_slist_insert(gSList, data, position);
420 		
421 		if(p is null)
422 		{
423 			return null;
424 		}
425 		
426 		return new ListSG(cast(GSList*) p);
427 	}
428 
429 	/**
430 	 * Inserts a node before @sibling containing @data.
431 	 *
432 	 * Params:
433 	 *     sibling = node to insert @data before
434 	 *     data = data to put in the newly-inserted node
435 	 *
436 	 * Returns: the new head of the list.
437 	 */
438 	public ListSG insertBefore(ListSG sibling, void* data)
439 	{
440 		auto p = g_slist_insert_before(gSList, (sibling is null) ? null : sibling.getListSGStruct(), data);
441 		
442 		if(p is null)
443 		{
444 			return null;
445 		}
446 		
447 		return new ListSG(cast(GSList*) p);
448 	}
449 
450 	/**
451 	 * Inserts a new element into the list, using the given
452 	 * comparison function to determine its position.
453 	 *
454 	 * Params:
455 	 *     data = the data for the new element
456 	 *     func = the function to compare elements in the list.
457 	 *         It should return a number > 0 if the first parameter
458 	 *         comes after the second parameter in the sort order.
459 	 *
460 	 * Returns: the new start of the #GSList
461 	 */
462 	public ListSG insertSorted(void* data, GCompareFunc func)
463 	{
464 		auto p = g_slist_insert_sorted(gSList, data, func);
465 		
466 		if(p is null)
467 		{
468 			return null;
469 		}
470 		
471 		return new ListSG(cast(GSList*) p);
472 	}
473 
474 	/**
475 	 * Inserts a new element into the list, using the given
476 	 * comparison function to determine its position.
477 	 *
478 	 * Params:
479 	 *     data = the data for the new element
480 	 *     func = the function to compare elements in the list.
481 	 *         It should return a number > 0 if the first parameter
482 	 *         comes after the second parameter in the sort order.
483 	 *     userData = data to pass to comparison function
484 	 *
485 	 * Returns: the new start of the #GSList
486 	 *
487 	 * Since: 2.10
488 	 */
489 	public ListSG insertSortedWithData(void* data, GCompareDataFunc func, void* userData)
490 	{
491 		auto p = g_slist_insert_sorted_with_data(gSList, data, func, userData);
492 		
493 		if(p is null)
494 		{
495 			return null;
496 		}
497 		
498 		return new ListSG(cast(GSList*) p);
499 	}
500 
501 	/**
502 	 * Gets the last element in a #GSList.
503 	 *
504 	 * This function iterates over the whole list.
505 	 *
506 	 * Returns: the last element in the #GSList,
507 	 *     or %NULL if the #GSList has no elements
508 	 */
509 	public ListSG last()
510 	{
511 		auto p = g_slist_last(gSList);
512 		
513 		if(p is null)
514 		{
515 			return null;
516 		}
517 		
518 		return new ListSG(cast(GSList*) p);
519 	}
520 
521 	/**
522 	 * Gets the number of elements in a #GSList.
523 	 *
524 	 * This function iterates over the whole list to
525 	 * count its elements. To check whether the list is non-empty, it is faster to
526 	 * check @list against %NULL.
527 	 *
528 	 * Returns: the number of elements in the #GSList
529 	 */
530 	public uint length()
531 	{
532 		return g_slist_length(gSList);
533 	}
534 
535 	/**
536 	 * Gets the element at the given position in a #GSList.
537 	 *
538 	 * Params:
539 	 *     n = the position of the element, counting from 0
540 	 *
541 	 * Returns: the element, or %NULL if the position is off
542 	 *     the end of the #GSList
543 	 */
544 	public ListSG nth(uint n)
545 	{
546 		auto p = g_slist_nth(gSList, n);
547 		
548 		if(p is null)
549 		{
550 			return null;
551 		}
552 		
553 		return new ListSG(cast(GSList*) p);
554 	}
555 
556 	/**
557 	 * Gets the data of the element at the given position.
558 	 *
559 	 * Params:
560 	 *     n = the position of the element
561 	 *
562 	 * Returns: the element's data, or %NULL if the position
563 	 *     is off the end of the #GSList
564 	 */
565 	public void* nthData(uint n)
566 	{
567 		return g_slist_nth_data(gSList, n);
568 	}
569 
570 	/**
571 	 * Gets the position of the given element
572 	 * in the #GSList (starting from 0).
573 	 *
574 	 * Params:
575 	 *     llink = an element in the #GSList
576 	 *
577 	 * Returns: the position of the element in the #GSList,
578 	 *     or -1 if the element is not found
579 	 */
580 	public int position(ListSG llink)
581 	{
582 		return g_slist_position(gSList, (llink is null) ? null : llink.getListSGStruct());
583 	}
584 
585 	/**
586 	 * Adds a new element on to the start of the list.
587 	 *
588 	 * The return value is the new start of the list, which
589 	 * may have changed, so make sure you store the new value.
590 	 *
591 	 * |[<!-- language="C" -->
592 	 * // Notice that it is initialized to the empty list.
593 	 * GSList *list = NULL;
594 	 * list = g_slist_prepend (list, "last");
595 	 * list = g_slist_prepend (list, "first");
596 	 * ]|
597 	 *
598 	 * Params:
599 	 *     data = the data for the new element
600 	 *
601 	 * Returns: the new start of the #GSList
602 	 */
603 	public ListSG prepend(void* data)
604 	{
605 		auto p = g_slist_prepend(gSList, data);
606 		
607 		if(p is null)
608 		{
609 			return null;
610 		}
611 		
612 		return new ListSG(cast(GSList*) p);
613 	}
614 
615 	/**
616 	 * Removes an element from a #GSList.
617 	 * If two elements contain the same data, only the first is removed.
618 	 * If none of the elements contain the data, the #GSList is unchanged.
619 	 *
620 	 * Params:
621 	 *     data = the data of the element to remove
622 	 *
623 	 * Returns: the new start of the #GSList
624 	 */
625 	public ListSG remove(void* data)
626 	{
627 		auto p = g_slist_remove(gSList, data);
628 		
629 		if(p is null)
630 		{
631 			return null;
632 		}
633 		
634 		return new ListSG(cast(GSList*) p);
635 	}
636 
637 	/**
638 	 * Removes all list nodes with data equal to @data.
639 	 * Returns the new head of the list. Contrast with
640 	 * g_slist_remove() which removes only the first node
641 	 * matching the given data.
642 	 *
643 	 * Params:
644 	 *     data = data to remove
645 	 *
646 	 * Returns: new head of @list
647 	 */
648 	public ListSG removeAll(void* data)
649 	{
650 		auto p = g_slist_remove_all(gSList, data);
651 		
652 		if(p is null)
653 		{
654 			return null;
655 		}
656 		
657 		return new ListSG(cast(GSList*) p);
658 	}
659 
660 	/**
661 	 * Removes an element from a #GSList, without
662 	 * freeing the element. The removed element's next
663 	 * link is set to %NULL, so that it becomes a
664 	 * self-contained list with one element.
665 	 *
666 	 * Removing arbitrary nodes from a singly-linked list
667 	 * requires time that is proportional to the length of the list
668 	 * (ie. O(n)). If you find yourself using g_slist_remove_link()
669 	 * frequently, you should consider a different data structure,
670 	 * such as the doubly-linked #GList.
671 	 *
672 	 * Params:
673 	 *     link = an element in the #GSList
674 	 *
675 	 * Returns: the new start of the #GSList, without the element
676 	 */
677 	public ListSG removeLink(ListSG link)
678 	{
679 		auto p = g_slist_remove_link(gSList, (link is null) ? null : link.getListSGStruct());
680 		
681 		if(p is null)
682 		{
683 			return null;
684 		}
685 		
686 		return new ListSG(cast(GSList*) p);
687 	}
688 
689 	/**
690 	 * Reverses a #GSList.
691 	 *
692 	 * Returns: the start of the reversed #GSList
693 	 */
694 	public ListSG reverse()
695 	{
696 		auto p = g_slist_reverse(gSList);
697 		
698 		if(p is null)
699 		{
700 			return null;
701 		}
702 		
703 		return new ListSG(cast(GSList*) p);
704 	}
705 
706 	/**
707 	 * Sorts a #GSList using the given comparison function.
708 	 *
709 	 * Params:
710 	 *     compareFunc = the comparison function used to sort the #GSList.
711 	 *         This function is passed the data from 2 elements of the #GSList
712 	 *         and should return 0 if they are equal, a negative value if the
713 	 *         first element comes before the second, or a positive value if
714 	 *         the first element comes after the second.
715 	 *
716 	 * Returns: the start of the sorted #GSList
717 	 */
718 	public ListSG sort(GCompareFunc compareFunc)
719 	{
720 		auto p = g_slist_sort(gSList, compareFunc);
721 		
722 		if(p is null)
723 		{
724 			return null;
725 		}
726 		
727 		return new ListSG(cast(GSList*) p);
728 	}
729 
730 	/**
731 	 * Like g_slist_sort(), but the sort function accepts a user data argument.
732 	 *
733 	 * Params:
734 	 *     compareFunc = comparison function
735 	 *     userData = data to pass to comparison function
736 	 *
737 	 * Returns: new head of the list
738 	 */
739 	public ListSG sortWithData(GCompareDataFunc compareFunc, void* userData)
740 	{
741 		auto p = g_slist_sort_with_data(gSList, compareFunc, userData);
742 		
743 		if(p is null)
744 		{
745 			return null;
746 		}
747 		
748 		return new ListSG(cast(GSList*) p);
749 	}
750 }