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