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.
508 	 *
509 	 * Return: the number of elements in the #GSList
510 	 */
511 	public uint length()
512 	{
513 		return g_slist_length(gSList);
514 	}
515 
516 	/**
517 	 * Gets the element at the given position in a #GSList.
518 	 *
519 	 * Params:
520 	 *     n = the position of the element, counting from 0
521 	 *
522 	 * Return: the element, or %NULL if the position is off
523 	 *     the end of the #GSList
524 	 */
525 	public ListSG nth(uint n)
526 	{
527 		auto p = g_slist_nth(gSList, n);
528 		
529 		if(p is null)
530 		{
531 			return null;
532 		}
533 		
534 		return new ListSG(cast(GSList*) p);
535 	}
536 
537 	/**
538 	 * Gets the data of the element at the given position.
539 	 *
540 	 * Params:
541 	 *     n = the position of the element
542 	 *
543 	 * Return: the element's data, or %NULL if the position
544 	 *     is off the end of the #GSList
545 	 */
546 	public void* nthData(uint n)
547 	{
548 		return g_slist_nth_data(gSList, n);
549 	}
550 
551 	/**
552 	 * Gets the position of the given element
553 	 * in the #GSList (starting from 0).
554 	 *
555 	 * Params:
556 	 *     llink = an element in the #GSList
557 	 *
558 	 * Return: the position of the element in the #GSList,
559 	 *     or -1 if the element is not found
560 	 */
561 	public int position(ListSG llink)
562 	{
563 		return g_slist_position(gSList, (llink is null) ? null : llink.getListSGStruct());
564 	}
565 
566 	/**
567 	 * Adds a new element on to the start of the list.
568 	 *
569 	 * The return value is the new start of the list, which
570 	 * may have changed, so make sure you store the new value.
571 	 *
572 	 * |[<!-- language="C" -->
573 	 * // Notice that it is initialized to the empty list.
574 	 * GSList *list = NULL;
575 	 * list = g_slist_prepend (list, "last");
576 	 * list = g_slist_prepend (list, "first");
577 	 * ]|
578 	 *
579 	 * Params:
580 	 *     data = the data for the new element
581 	 *
582 	 * Return: the new start of the #GSList
583 	 */
584 	public ListSG prepend(void* data)
585 	{
586 		auto p = g_slist_prepend(gSList, data);
587 		
588 		if(p is null)
589 		{
590 			return null;
591 		}
592 		
593 		return new ListSG(cast(GSList*) p);
594 	}
595 
596 	/**
597 	 * Removes an element from a #GSList.
598 	 * If two elements contain the same data, only the first is removed.
599 	 * If none of the elements contain the data, the #GSList is unchanged.
600 	 *
601 	 * Params:
602 	 *     data = the data of the element to remove
603 	 *
604 	 * Return: the new start of the #GSList
605 	 */
606 	public ListSG remove(void* data)
607 	{
608 		auto p = g_slist_remove(gSList, data);
609 		
610 		if(p is null)
611 		{
612 			return null;
613 		}
614 		
615 		return new ListSG(cast(GSList*) p);
616 	}
617 
618 	/**
619 	 * Removes all list nodes with data equal to @data.
620 	 * Returns the new head of the list. Contrast with
621 	 * g_slist_remove() which removes only the first node
622 	 * matching the given data.
623 	 *
624 	 * Params:
625 	 *     data = data to remove
626 	 *
627 	 * Return: new head of @list
628 	 */
629 	public ListSG removeAll(void* data)
630 	{
631 		auto p = g_slist_remove_all(gSList, data);
632 		
633 		if(p is null)
634 		{
635 			return null;
636 		}
637 		
638 		return new ListSG(cast(GSList*) p);
639 	}
640 
641 	/**
642 	 * Removes an element from a #GSList, without
643 	 * freeing the element. The removed element's next
644 	 * link is set to %NULL, so that it becomes a
645 	 * self-contained list with one element.
646 	 *
647 	 * Removing arbitrary nodes from a singly-linked list
648 	 * requires time that is proportional to the length of the list
649 	 * (ie. O(n)). If you find yourself using g_slist_remove_link()
650 	 * frequently, you should consider a different data structure,
651 	 * such as the doubly-linked #GList.
652 	 *
653 	 * Params:
654 	 *     link = an element in the #GSList
655 	 *
656 	 * Return: the new start of the #GSList, without the element
657 	 */
658 	public ListSG removeLink(ListSG link)
659 	{
660 		auto p = g_slist_remove_link(gSList, (link is null) ? null : link.getListSGStruct());
661 		
662 		if(p is null)
663 		{
664 			return null;
665 		}
666 		
667 		return new ListSG(cast(GSList*) p);
668 	}
669 
670 	/**
671 	 * Reverses a #GSList.
672 	 *
673 	 * Return: the start of the reversed #GSList
674 	 */
675 	public ListSG reverse()
676 	{
677 		auto p = g_slist_reverse(gSList);
678 		
679 		if(p is null)
680 		{
681 			return null;
682 		}
683 		
684 		return new ListSG(cast(GSList*) p);
685 	}
686 
687 	/**
688 	 * Sorts a #GSList using the given comparison function.
689 	 *
690 	 * Params:
691 	 *     compareFunc = the comparison function used to sort the #GSList.
692 	 *         This function is passed the data from 2 elements of the #GSList
693 	 *         and should return 0 if they are equal, a negative value if the
694 	 *         first element comes before the second, or a positive value if
695 	 *         the first element comes after the second.
696 	 *
697 	 * Return: the start of the sorted #GSList
698 	 */
699 	public ListSG sort(GCompareFunc compareFunc)
700 	{
701 		auto p = g_slist_sort(gSList, compareFunc);
702 		
703 		if(p is null)
704 		{
705 			return null;
706 		}
707 		
708 		return new ListSG(cast(GSList*) p);
709 	}
710 
711 	/**
712 	 * Like g_slist_sort(), but the sort function accepts a user data argument.
713 	 *
714 	 * Params:
715 	 *     compareFunc = comparison function
716 	 *     userData = data to pass to comparison function
717 	 *
718 	 * Return: new head of the list
719 	 */
720 	public ListSG sortWithData(GCompareDataFunc compareFunc, void* userData)
721 	{
722 		auto p = g_slist_sort_with_data(gSList, compareFunc, userData);
723 		
724 		if(p is null)
725 		{
726 			return null;
727 		}
728 		
729 		return new ListSG(cast(GSList*) p);
730 	}
731 }