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