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