detached dfc009c

Gen: Add methods for records

commit
dfc009c4291560ff6166c2bd47614c983f16a196
author
Peter Fern · · 2023-12-31 04:36 UTC
committer
Peter Fern · · 2024-01-03 01:46 UTC
parent
8fc4929
signature
unsigned
4 files · +49 -35
M internal/gir/pass/pass.go +23 -1
@@ -97,7 +97,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
9797 for _, rec := range ns.Records {
9898 name := util.SnakeToCamel(rec.Name)
9999 constructors := make([]types.FuncTemplate, len(rec.Constructors))
100- fields := make([]types.RecordField, len(rec.Fields))
100+ receivers := make([]types.FuncTemplate, 0, len(rec.Methods))
101+ fields := make([]types.RecordField, 0, len(rec.Fields))
101102 fn := rec.FilenameSafe()
102103 files = append(files, fn)
103104 for i, c := range rec.Constructors {
@@ -131,10 +132,30 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
131132 Type: _type,
132133 })
133134 }
135+ for _, f := range rec.Methods {
136+ name := util.SnakeToCamel(f.Name)
137+ if name == "" {
138+ name = util.SnakeToCamel(f.CIdentifier)
139+ }
140+ for _, f := range fields {
141+ if f.Name == name {
142+ name = name + "Fn"
143+ break
144+ }
145+ }
146+ receivers = append(receivers, types.FuncTemplate{
147+ Doc: f.Doc.StringSafe(),
148+ Name: name,
149+ CName: f.CIdentifier,
150+ Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws),
151+ Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws),
152+ })
153+ }
134154 records[fn] = append(records[fn], types.RecordTemplate{
135155 Name: name,
136156 Doc: rec.Doc.StringSafe(),
137157 Constructors: constructors,
158+ Receivers: receivers,
138159 Fields: fields,
139160 })
140161 recordLookup[name] = true
@@ -291,6 +312,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
291312 }
292313 for _, i := range records[fn] {
293314 methods += len(i.Constructors)
315+ methods += len(i.Receivers)
294316 }
295317 for _, i := range classes[fn] {
296318 methods += len(i.Constructors)
@@ -97,7 +97,8 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
97 for _, rec := range ns.Records {97 for _, rec := range ns.Records {
98 name := util.SnakeToCamel(rec.Name)98 name := util.SnakeToCamel(rec.Name)
99 constructors := make([]types.FuncTemplate, len(rec.Constructors))99 constructors := make([]types.FuncTemplate, len(rec.Constructors))
100- fields := make([]types.RecordField, len(rec.Fields))100+ receivers := make([]types.FuncTemplate, 0, len(rec.Methods))
101+ fields := make([]types.RecordField, 0, len(rec.Fields))
101 fn := rec.FilenameSafe()102 fn := rec.FilenameSafe()
102 files = append(files, fn)103 files = append(files, fn)
103 for i, c := range rec.Constructors {104 for i, c := range rec.Constructors {
@@ -131,10 +132,30 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
131 Type: _type,132 Type: _type,
132 })133 })
133 }134 }
135+ for _, f := range rec.Methods {
136+ name := util.SnakeToCamel(f.Name)
137+ if name == "" {
138+ name = util.SnakeToCamel(f.CIdentifier)
139+ }
140+ for _, f := range fields {
141+ if f.Name == name {
142+ name = name + "Fn"
143+ break
144+ }
145+ }
146+ receivers = append(receivers, types.FuncTemplate{
147+ Doc: f.Doc.StringSafe(),
148+ Name: name,
149+ CName: f.CIdentifier,
150+ Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws),
151+ Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws),
152+ })
153+ }
134 records[fn] = append(records[fn], types.RecordTemplate{154 records[fn] = append(records[fn], types.RecordTemplate{
135 Name: name,155 Name: name,
136 Doc: rec.Doc.StringSafe(),156 Doc: rec.Doc.StringSafe(),
137 Constructors: constructors,157 Constructors: constructors,
158+ Receivers: receivers,
138 Fields: fields,159 Fields: fields,
139 })160 })
140 recordLookup[name] = true161 recordLookup[name] = true
@@ -291,6 +312,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string
291 }312 }
292 for _, i := range records[fn] {313 for _, i := range records[fn] {
293 methods += len(i.Constructors)314 methods += len(i.Constructors)
315+ methods += len(i.Receivers)
294 }316 }
295 for _, i := range classes[fn] {317 for _, i := range classes[fn] {
296 methods += len(i.Constructors)318 methods += len(i.Constructors)
M internal/gir/types/template.go +3 -0
@@ -177,6 +177,9 @@ type RecordTemplate struct {
177177 // Constructors is the slice of functions that create the class struct
178178 Constructors []FuncTemplate
179179
180+ // Receivers is the slice of functions that have value receivers to the struct
181+ Receivers []FuncTemplate
182+
180183 // Fields is the list of record fields
181184 Fields []RecordField
182185 }
@@ -177,6 +177,9 @@ type RecordTemplate struct {
177 // Constructors is the slice of functions that create the class struct177 // Constructors is the slice of functions that create the class struct
178 Constructors []FuncTemplate178 Constructors []FuncTemplate
179 179
180+ // Receivers is the slice of functions that have value receivers to the struct
181+ Receivers []FuncTemplate
182+
180 // Fields is the list of record fields183 // Fields is the list of record fields
181 Fields []RecordField184 Fields []RecordField
182 }185 }
M templates/go +21 -2
@@ -31,6 +31,10 @@ type {{.Name}} struct {
3131 {{end}}
3232 }
3333
34+func (x *{{.Name}}) GoPointer() uintptr {
35+ return uintptr(unsafe.Pointer(x))
36+}
37+
3438 {{$outer := .}}
3539
3640 {{range .Constructors -}}
@@ -45,6 +49,18 @@ func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
4549 }
4650 {{end}}
4751
52+{{$outer := .}}
53+{{range .Receivers -}}
54+var x{{$outer.Name}}{{.Name}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret.Raw}}
55+
56+{{.Doc}}
57+func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
58+ {{.Ret.Preamble $NotGLib}}
59+ {{if .Ret.Value}}cret :={{end}}x{{$outer.Name}}{{.Name}}(x.GoPointer() {{convc .Args.API.Call}})
60+ {{.Ret.Fmt $NotGObject}}
61+}
62+{{end}}
63+
4864 {{end}}
4965
5066 {{range .Interfaces -}}
@@ -216,9 +232,12 @@ func init() {
216232 {{end}}
217233
218234 {{range .Records -}}
219- {{$cls := .}}
235+ {{$rec := .}}
220236 {{range .Constructors -}}
221- core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")
237+ core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}")
238+ {{end}}
239+ {{range .Receivers -}}
240+ core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}")
222241 {{end}}
223242 {{end}}
224243
@@ -31,6 +31,10 @@ type {{.Name}} struct {
31 {{end}}31 {{end}}
32 }32 }
33 33
34+func (x *{{.Name}}) GoPointer() uintptr {
35+ return uintptr(unsafe.Pointer(x))
36+}
37+
34 {{$outer := .}}38 {{$outer := .}}
35 39
36 {{range .Constructors -}}40 {{range .Constructors -}}
@@ -45,6 +49,18 @@ func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
45 }49 }
46 {{end}}50 {{end}}
47 51
52+{{$outer := .}}
53+{{range .Receivers -}}
54+var x{{$outer.Name}}{{.Name}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret.Raw}}
55+
56+{{.Doc}}
57+func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} {
58+ {{.Ret.Preamble $NotGLib}}
59+ {{if .Ret.Value}}cret :={{end}}x{{$outer.Name}}{{.Name}}(x.GoPointer() {{convc .Args.API.Call}})
60+ {{.Ret.Fmt $NotGObject}}
61+}
62+{{end}}
63+
48 {{end}}64 {{end}}
49 65
50 {{range .Interfaces -}}66 {{range .Interfaces -}}
@@ -216,9 +232,12 @@ func init() {
216 {{end}}232 {{end}}
217 233
218 {{range .Records -}}234 {{range .Records -}}
219- {{$cls := .}}235+ {{$rec := .}}
220 {{range .Constructors -}}236 {{range .Constructors -}}
221- core.PuregoSafeRegister(&x{{.Name}}{{$cls.Name}}, lib, "{{.CName}}")237+ core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}")
238+ {{end}}
239+ {{range .Receivers -}}
240+ core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}")
222 {{end}}241 {{end}}
223 {{end}}242 {{end}}
224 243
M templates/gobject +2 -32
@@ -1,11 +1,9 @@
11 package gobject
22
33 import (
4- "os"
54 "reflect"
65
76 "github.com/jwijenbergh/purego"
8- "github.com/jwijenbergh/puregotk/internal/core"
97 )
108
119 type Ptr interface {
@@ -39,11 +37,11 @@ func (o Object) Cast(v Ptr) {
3937 }
4038
4139 func (o Object) ConnectSignal(signal string, cb func()) uint32 {
42- return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb))
40+ return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb))
4341 }
4442
4543 func (o Object) DisconnectSignal(handler uint32) {
46- SignalHandlerDisconnect(&o, handler)
44+ SignalHandlerDisconnect(&o, handler)
4745 }
4846
4947 // NewCallback is an alias to purego.NewCallback
@@ -79,31 +77,3 @@ const (
7977 TypeReservedBseLastVal = 48 << 2
8078 TypeReservedUserFirstVal = 49 << 2
8179 )
82-
83-var xValueGetString func(*Value) string
84-
85-func (v *Value) String() string {
86- return xValueGetString(v)
87-}
88-
89-var xValueGetInt func(*Value) int
90-
91-func (v *Value) Int() int {
92- return xValueGetInt(v)
93-}
94-
95-var xValueUnset func(*Value)
96-
97-func (x *Value) Unset() {
98- xValueUnset(x)
99-}
100-
101-func init() {
102- lib, err := purego.Dlopen(os.Getenv("PUREGOTK_GTK_PATH"), purego.RTLD_NOW|purego.RTLD_GLOBAL)
103- if err != nil {
104- panic(err)
105- }
106- core.PuregoSafeRegister(&xValueGetString, lib, "g_value_get_string")
107- core.PuregoSafeRegister(&xValueGetInt, lib, "g_value_get_int")
108- core.PuregoSafeRegister(&xValueUnset, lib, "g_value_unset")
109-}
@@ -1,11 +1,9 @@
1 package gobject1 package gobject
2 2
3 import (3 import (
4- "os"
5 "reflect"4 "reflect"
6 5
7 "github.com/jwijenbergh/purego"6 "github.com/jwijenbergh/purego"
8- "github.com/jwijenbergh/puregotk/internal/core"
9 )7 )
10 8
11 type Ptr interface {9 type Ptr interface {
@@ -39,11 +37,11 @@ func (o Object) Cast(v Ptr) {
39 }37 }
40 38
41 func (o Object) ConnectSignal(signal string, cb func()) uint32 {39 func (o Object) ConnectSignal(signal string, cb func()) uint32 {
42- return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb))40+ return SignalConnect(o.GoPointer(), signal, purego.NewCallback(cb))
43 }41 }
44 42
45 func (o Object) DisconnectSignal(handler uint32) {43 func (o Object) DisconnectSignal(handler uint32) {
46- SignalHandlerDisconnect(&o, handler)44+ SignalHandlerDisconnect(&o, handler)
47 }45 }
48 46
49 // NewCallback is an alias to purego.NewCallback47 // NewCallback is an alias to purego.NewCallback
@@ -79,31 +77,3 @@ const (
79 TypeReservedBseLastVal = 48 << 277 TypeReservedBseLastVal = 48 << 2
80 TypeReservedUserFirstVal = 49 << 278 TypeReservedUserFirstVal = 49 << 2
81 )79 )
82-
83-var xValueGetString func(*Value) string
84-
85-func (v *Value) String() string {
86- return xValueGetString(v)
87-}
88-
89-var xValueGetInt func(*Value) int
90-
91-func (v *Value) Int() int {
92- return xValueGetInt(v)
93-}
94-
95-var xValueUnset func(*Value)
96-
97-func (x *Value) Unset() {
98- xValueUnset(x)
99-}
100-
101-func init() {
102- lib, err := purego.Dlopen(os.Getenv("PUREGOTK_GTK_PATH"), purego.RTLD_NOW|purego.RTLD_GLOBAL)
103- if err != nil {
104- panic(err)
105- }
106- core.PuregoSafeRegister(&xValueGetString, lib, "g_value_get_string")
107- core.PuregoSafeRegister(&xValueGetInt, lib, "g_value_get_int")
108- core.PuregoSafeRegister(&xValueUnset, lib, "g_value_unset")
109-}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close