File: slist.go

package info (click to toggle)
golang-github-gotk3-gotk3 0.0~GOTK3~0~2~0%2Bgit20170418.0.96d4110-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,368 kB
  • sloc: ansic: 1,056; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package glib

// #cgo pkg-config: glib-2.0 gobject-2.0
// #include <glib.h>
// #include <glib-object.h>
// #include "glib.go.h"
import "C"
import "unsafe"

// SList is a representation of Glib's GSList. A SList must be manually freed
// by either calling Free() or FreeFull()
type SList struct {
	list *C.struct__GSList
}

func WrapSList(obj uintptr) *SList {
	return wrapSList((*C.struct__GSList)(unsafe.Pointer(obj)))
}

func wrapSList(obj *C.struct__GSList) *SList {
	if obj == nil {
		return nil
	}

	//NOTE a list should be freed by calling either
	//g_slist_free() or g_slist_free_full(). However, it's not possible to use a
	//finalizer for this.
	return &SList{obj}
}

func (v *SList) Native() uintptr {
	return uintptr(unsafe.Pointer(v.list))
}

func (v *SList) native() *C.struct__GSList {
	if v == nil || v.list == nil {
		return nil
	}
	return v.list
}

func (v *SList) Append(data uintptr) *SList {
	ret := C.g_slist_append(v.native(), C.gpointer(data))
	if ret == v.native() {
		return v
	}

	return wrapSList(ret)
}

// Length is a wrapper around g_slist_length().
func (v *SList) Length() uint {
	return uint(C.g_slist_length(v.native()))
}

// Next is a wrapper around the next struct field
func (v *SList) Next() *SList {
	n := v.native()
	if n == nil {
		return nil
	}

	return wrapSList(n.next)
}

// Foreach acts the same as g_slist_foreach().
// No user_data arguement is implemented because of Go clojure capabilities.
func (v *SList) Foreach(fn func(ptr unsafe.Pointer)) {
	for l := v; l != nil; l = l.Next() {
		fn(unsafe.Pointer(l.native().data))
	}
}

// Free is a wrapper around g_slist_free().
func (v *SList) Free() {
	C.g_slist_free(v.native())
	v.list = nil
}

// FreeFull is a wrapper around g_slist_free_full().
func (v *SList) FreeFull() {
	//TODO implement GDestroyNotify callback
	C.g_slist_free_full(v.native(), nil)
	v.list = nil
}

// GSList * 	g_slist_alloc ()
// GSList * 	g_slist_prepend ()
// GSList * 	g_slist_insert ()
// GSList * 	g_slist_insert_before ()
// GSList * 	g_slist_insert_sorted ()
// GSList * 	g_slist_remove ()
// GSList * 	g_slist_remove_link ()
// GSList * 	g_slist_delete_link ()
// GSList * 	g_slist_remove_all ()
// void 	g_slist_free_1 ()
// GSList * 	g_slist_copy ()
// GSList * 	g_slist_copy_deep ()
// GSList * 	g_slist_reverse ()
// GSList * 	g_slist_insert_sorted_with_data ()
// GSList * 	g_slist_sort ()
// GSList * 	g_slist_sort_with_data ()
// GSList * 	g_slist_concat ()
// GSList * 	g_slist_last ()
// GSList * 	g_slist_nth ()
// gpointer 	g_slist_nth_data ()
// GSList * 	g_slist_find ()
// GSList * 	g_slist_find_custom ()
// gint 	g_slist_position ()
// gint 	g_slist_index ()