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