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