-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpn.go
83 lines (71 loc) · 1.63 KB
/
cpn.go
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
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"runtime"
"unsafe"
)
type Cpn struct {
ptr *C.Cpn
// cached fields
_category string
_package string
_hash uint64
}
type cpnPtr interface {
p() *C.Cpn
}
func cpnFromPtr(ptr *C.Cpn) (*Cpn, error) {
if ptr != nil {
cpn := &Cpn{ptr: ptr}
runtime.SetFinalizer(cpn, func(self *Cpn) { C.pkgcraft_cpn_free(self.ptr) })
return cpn, nil
} else {
return nil, newPkgcraftError()
}
}
// Parse a string into a Cpn object.
func NewCpn(s string) (*Cpn, error) {
c_str := C.CString(s)
defer C.free(unsafe.Pointer(c_str))
ptr := C.pkgcraft_cpn_new(c_str)
return cpnFromPtr(ptr)
}
func (self *Cpn) p() *C.Cpn {
return self.ptr
}
// Return an Cpn's category.
func (self *Cpn) Category() string {
if self._category == "" {
s := C.pkgcraft_cpn_category(self.ptr)
defer C.pkgcraft_str_free(s)
self._category = C.GoString(s)
}
return self._category
}
// Return a Cpn's package name.
func (self *Cpn) Package() string {
if self._package == "" {
s := C.pkgcraft_cpn_package(self.ptr)
defer C.pkgcraft_str_free(s)
self._package = C.GoString(s)
}
return self._package
}
func (self *Cpn) String() string {
s := C.pkgcraft_cpn_str(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
func (self *Cpn) Hash() uint64 {
if self._hash == 0 {
self._hash = uint64(C.pkgcraft_cpn_hash(self.ptr))
}
return self._hash
}
// Compare two Cpns returning -1, 0, or 1 if the first is less than, equal to,
// or greater than the second, respectively.
func (self *Cpn) Cmp(other *Cpn) int {
return int(C.pkgcraft_cpn_cmp(self.ptr, other.ptr))
}