Merge pull request #1 from pdf/add_generators
Add generators
5 files · +172 -45
M internal/gir/pass/pass.go +65 -13
| @@ -92,13 +92,32 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 92 | 92 | enums[fn] = append(enums[fn], temp) | |
| 93 | 93 | } | |
| 94 | 94 | ||
| 95 | + | constants := make(map[string][]types.ConstantTemplate) | |
| 96 | + | for _, con := range ns.Constants { | |
| 97 | + | fn := con.FilenameSafe() | |
| 98 | + | files = append(files, fn) | |
| 99 | + | constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types)) | |
| 100 | + | } | |
| 101 | + | ||
| 95 | 102 | records := make(map[string][]types.RecordTemplate) | |
| 96 | 103 | recordLookup := make(map[string]bool) | |
| 97 | 104 | for _, rec := range ns.Records { | |
| 98 | 105 | name := util.SnakeToCamel(rec.Name) | |
| 99 | - | fields := make([]types.RecordField, len(rec.Fields)) | |
| 106 | + | constructors := make([]types.FuncTemplate, len(rec.Constructors)) | |
| 107 | + | receivers := make([]types.FuncTemplate, 0, len(rec.Methods)) | |
| 108 | + | fields := make([]types.RecordField, 0, len(rec.Fields)) | |
| 100 | 109 | fn := rec.FilenameSafe() | |
| 101 | 110 | files = append(files, fn) | |
| 111 | + | for i, c := range rec.Constructors { | |
| 112 | + | name := util.SnakeToCamel(c.Name) | |
| 113 | + | constructors[i] = types.FuncTemplate{ | |
| 114 | + | Name: name, | |
| 115 | + | CName: c.CIdentifier, | |
| 116 | + | Doc: c.Doc.StringSafe(), | |
| 117 | + | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | |
| 118 | + | Ret: c.ReturnValue.Template(ns.Name, "", p.Types, c.Throws), | |
| 119 | + | } | |
| 120 | + | } | |
| 102 | 121 | for _, f := range rec.Fields { | |
| 103 | 122 | _type := f.Translate(ns.Name, p.Types) | |
| 104 | 123 | if _type == "" { | |
| @@ -120,10 +139,31 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 120 | 139 | Type: _type, | |
| 121 | 140 | }) | |
| 122 | 141 | } | |
| 142 | + | for _, f := range rec.Methods { | |
| 143 | + | name := util.SnakeToCamel(f.Name) | |
| 144 | + | if name == "" { | |
| 145 | + | name = util.SnakeToCamel(f.CIdentifier) | |
| 146 | + | } | |
| 147 | + | for _, f := range fields { | |
| 148 | + | if f.Name == name { | |
| 149 | + | name = name + "Fn" | |
| 150 | + | break | |
| 151 | + | } | |
| 152 | + | } | |
| 153 | + | receivers = append(receivers, types.FuncTemplate{ | |
| 154 | + | Doc: f.Doc.StringSafe(), | |
| 155 | + | Name: name, | |
| 156 | + | CName: f.CIdentifier, | |
| 157 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |
| 158 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |
| 159 | + | }) | |
| 160 | + | } | |
| 123 | 161 | records[fn] = append(records[fn], types.RecordTemplate{ | |
| 124 | - | Name: name, | |
| 125 | - | Doc: rec.Doc.StringSafe(), | |
| 126 | - | Fields: fields, | |
| 162 | + | Name: name, | |
| 163 | + | Doc: rec.Doc.StringSafe(), | |
| 164 | + | Constructors: constructors, | |
| 165 | + | Receivers: receivers, | |
| 166 | + | Fields: fields, | |
| 127 | 167 | }) | |
| 128 | 168 | recordLookup[name] = true | |
| 129 | 169 | } | |
| @@ -200,6 +240,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 200 | 240 | for _, cls := range ns.Classes { | |
| 201 | 241 | implemented := make(map[string]bool) | |
| 202 | 242 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | |
| 243 | + | functions := make([]types.FuncTemplate, len(cls.Functions)) | |
| 203 | 244 | fn := cls.FilenameSafe() | |
| 204 | 245 | files = append(files, fn) | |
| 205 | 246 | ||
| @@ -236,6 +277,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 236 | 277 | } | |
| 237 | 278 | } | |
| 238 | 279 | var interfaces []types.InterfaceTemplate | |
| 280 | + | for i, f := range cls.Functions { | |
| 281 | + | name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name)) | |
| 282 | + | functions[i] = types.FuncTemplate{ | |
| 283 | + | Name: name, | |
| 284 | + | CName: f.CIdentifier, | |
| 285 | + | Doc: f.Doc.StringSafe(), | |
| 286 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |
| 287 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |
| 288 | + | } | |
| 289 | + | } | |
| 239 | 290 | for _, impl := range cls.Implements { | |
| 240 | 291 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | |
| 241 | 292 | } | |
| @@ -246,6 +297,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 246 | 297 | Constructors: constructors, | |
| 247 | 298 | Receivers: receivers, | |
| 248 | 299 | Interfaces: interfaces, | |
| 300 | + | Functions: functions, | |
| 249 | 301 | Signals: signals, | |
| 250 | 302 | }) | |
| 251 | 303 | } | |
| @@ -263,17 +315,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 263 | 315 | //} | |
| 264 | 316 | methods := 0 | |
| 265 | 317 | for _, i := range interfaces[fn] { | |
| 266 | - | for range i.Methods { | |
| 267 | - | methods += 1 | |
| 268 | - | } | |
| 318 | + | methods += len(i.Methods) | |
| 319 | + | } | |
| 320 | + | for _, i := range records[fn] { | |
| 321 | + | methods += len(i.Constructors) | |
| 322 | + | methods += len(i.Receivers) | |
| 269 | 323 | } | |
| 270 | 324 | for _, i := range classes[fn] { | |
| 271 | - | for range i.Constructors { | |
| 272 | - | methods += 1 | |
| 273 | - | } | |
| 274 | - | for range i.Receivers { | |
| 275 | - | methods += 1 | |
| 276 | - | } | |
| 325 | + | methods += len(i.Constructors) | |
| 326 | + | methods += len(i.Receivers) | |
| 327 | + | methods += len(i.Functions) | |
| 277 | 328 | } | |
| 278 | 329 | // we do not need to add the length of interfaces in here | |
| 279 | 330 | // as they should only be loaded when there are classes | |
| @@ -288,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||
| 288 | 339 | Callbacks: callbacks[fn], | |
| 289 | 340 | Records: records[fn], | |
| 290 | 341 | Enums: enums[fn], | |
| 342 | + | Constants: constants[fn], | |
| 291 | 343 | Functions: functions[fn], | |
| 292 | 344 | Interfaces: interfaces[fn], | |
| 293 | 345 | Classes: classes[fn], | |
| @@ -92,13 +92,32 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 92 | enums[fn] = append(enums[fn], temp) | 92 | enums[fn] = append(enums[fn], temp) | ||
| 93 | } | 93 | } | ||
| 94 | 94 | ||||
| 95 | + | constants := make(map[string][]types.ConstantTemplate) | |||
| 96 | + | for _, con := range ns.Constants { | |||
| 97 | + | fn := con.FilenameSafe() | |||
| 98 | + | files = append(files, fn) | |||
| 99 | + | constants[fn] = append(constants[fn], con.Template(ns.Name, p.Types)) | |||
| 100 | + | } | |||
| 101 | + | ||||
| 95 | records := make(map[string][]types.RecordTemplate) | 102 | records := make(map[string][]types.RecordTemplate) | ||
| 96 | recordLookup := make(map[string]bool) | 103 | recordLookup := make(map[string]bool) | ||
| 97 | for _, rec := range ns.Records { | 104 | for _, rec := range ns.Records { | ||
| 98 | name := util.SnakeToCamel(rec.Name) | 105 | name := util.SnakeToCamel(rec.Name) | ||
| 99 | - | fields := make([]types.RecordField, len(rec.Fields)) | 106 | + | constructors := make([]types.FuncTemplate, len(rec.Constructors)) |
| 107 | + | receivers := make([]types.FuncTemplate, 0, len(rec.Methods)) | |||
| 108 | + | fields := make([]types.RecordField, 0, len(rec.Fields)) | |||
| 100 | fn := rec.FilenameSafe() | 109 | fn := rec.FilenameSafe() | ||
| 101 | files = append(files, fn) | 110 | files = append(files, fn) | ||
| 111 | + | for i, c := range rec.Constructors { | |||
| 112 | + | name := util.SnakeToCamel(c.Name) | |||
| 113 | + | constructors[i] = types.FuncTemplate{ | |||
| 114 | + | Name: name, | |||
| 115 | + | CName: c.CIdentifier, | |||
| 116 | + | Doc: c.Doc.StringSafe(), | |||
| 117 | + | Args: c.Parameters.Template(ns.Name, "", p.Types, c.Throws), | |||
| 118 | + | Ret: c.ReturnValue.Template(ns.Name, "", p.Types, c.Throws), | |||
| 119 | + | } | |||
| 120 | + | } | |||
| 102 | for _, f := range rec.Fields { | 121 | for _, f := range rec.Fields { | ||
| 103 | _type := f.Translate(ns.Name, p.Types) | 122 | _type := f.Translate(ns.Name, p.Types) | ||
| 104 | if _type == "" { | 123 | if _type == "" { | ||
| @@ -120,10 +139,31 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 120 | Type: _type, | 139 | Type: _type, | ||
| 121 | }) | 140 | }) | ||
| 122 | } | 141 | } | ||
| 142 | + | for _, f := range rec.Methods { | |||
| 143 | + | name := util.SnakeToCamel(f.Name) | |||
| 144 | + | if name == "" { | |||
| 145 | + | name = util.SnakeToCamel(f.CIdentifier) | |||
| 146 | + | } | |||
| 147 | + | for _, f := range fields { | |||
| 148 | + | if f.Name == name { | |||
| 149 | + | name = name + "Fn" | |||
| 150 | + | break | |||
| 151 | + | } | |||
| 152 | + | } | |||
| 153 | + | receivers = append(receivers, types.FuncTemplate{ | |||
| 154 | + | Doc: f.Doc.StringSafe(), | |||
| 155 | + | Name: name, | |||
| 156 | + | CName: f.CIdentifier, | |||
| 157 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |||
| 158 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |||
| 159 | + | }) | |||
| 160 | + | } | |||
| 123 | records[fn] = append(records[fn], types.RecordTemplate{ | 161 | records[fn] = append(records[fn], types.RecordTemplate{ | ||
| 124 | - | Name: name, | 162 | + | Name: name, |
| 125 | - | Doc: rec.Doc.StringSafe(), | 163 | + | Doc: rec.Doc.StringSafe(), |
| 126 | - | Fields: fields, | 164 | + | Constructors: constructors, |
| 165 | + | Receivers: receivers, | |||
| 166 | + | Fields: fields, | |||
| 127 | }) | 167 | }) | ||
| 128 | recordLookup[name] = true | 168 | recordLookup[name] = true | ||
| 129 | } | 169 | } | ||
| @@ -200,6 +240,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 200 | for _, cls := range ns.Classes { | 240 | for _, cls := range ns.Classes { | ||
| 201 | implemented := make(map[string]bool) | 241 | implemented := make(map[string]bool) | ||
| 202 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | 242 | constructors := make([]types.FuncTemplate, len(cls.Constructors)) | ||
| 243 | + | functions := make([]types.FuncTemplate, len(cls.Functions)) | |||
| 203 | fn := cls.FilenameSafe() | 244 | fn := cls.FilenameSafe() | ||
| 204 | files = append(files, fn) | 245 | files = append(files, fn) | ||
| 205 | 246 | ||||
| @@ -236,6 +277,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 236 | } | 277 | } | ||
| 237 | } | 278 | } | ||
| 238 | var interfaces []types.InterfaceTemplate | 279 | var interfaces []types.InterfaceTemplate | ||
| 280 | + | for i, f := range cls.Functions { | |||
| 281 | + | name := fmt.Sprintf("%s%s", util.SnakeToCamel(cls.Name), util.SnakeToCamel(f.Name)) | |||
| 282 | + | functions[i] = types.FuncTemplate{ | |||
| 283 | + | Name: name, | |||
| 284 | + | CName: f.CIdentifier, | |||
| 285 | + | Doc: f.Doc.StringSafe(), | |||
| 286 | + | Args: f.Parameters.Template(ns.Name, "", p.Types, f.Throws), | |||
| 287 | + | Ret: f.ReturnValue.Template(ns.Name, "", p.Types, f.Throws), | |||
| 288 | + | } | |||
| 289 | + | } | |||
| 239 | for _, impl := range cls.Implements { | 290 | for _, impl := range cls.Implements { | ||
| 240 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | 291 | interfaces = append(interfaces, types.GetInterfaceFuncs(ns.Name, impl.Name, implemented, p.Types)) | ||
| 241 | } | 292 | } | ||
| @@ -246,6 +297,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 246 | Constructors: constructors, | 297 | Constructors: constructors, | ||
| 247 | Receivers: receivers, | 298 | Receivers: receivers, | ||
| 248 | Interfaces: interfaces, | 299 | Interfaces: interfaces, | ||
| 300 | + | Functions: functions, | |||
| 249 | Signals: signals, | 301 | Signals: signals, | ||
| 250 | }) | 302 | }) | ||
| 251 | } | 303 | } | ||
| @@ -263,17 +315,16 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 263 | //} | 315 | //} | ||
| 264 | methods := 0 | 316 | methods := 0 | ||
| 265 | for _, i := range interfaces[fn] { | 317 | for _, i := range interfaces[fn] { | ||
| 266 | - | for range i.Methods { | 318 | + | methods += len(i.Methods) |
| 267 | - | methods += 1 | 319 | + | } |
| 268 | - | } | 320 | + | for _, i := range records[fn] { |
| 321 | + | methods += len(i.Constructors) | |||
| 322 | + | methods += len(i.Receivers) | |||
| 269 | } | 323 | } | ||
| 270 | for _, i := range classes[fn] { | 324 | for _, i := range classes[fn] { | ||
| 271 | - | for range i.Constructors { | 325 | + | methods += len(i.Constructors) |
| 272 | - | methods += 1 | 326 | + | methods += len(i.Receivers) |
| 273 | - | } | 327 | + | methods += len(i.Functions) |
| 274 | - | for range i.Receivers { | |||
| 275 | - | methods += 1 | |||
| 276 | - | } | |||
| 277 | } | 328 | } | ||
| 278 | // we do not need to add the length of interfaces in here | 329 | // we do not need to add the length of interfaces in here | ||
| 279 | // as they should only be loaded when there are classes | 330 | // as they should only be loaded when there are classes | ||
| @@ -288,6 +339,7 @@ func (p *Pass) writeGo(r types.Repository, gotemp *template.Template, dir string | |||||
| 288 | Callbacks: callbacks[fn], | 339 | Callbacks: callbacks[fn], | ||
| 289 | Records: records[fn], | 340 | Records: records[fn], | ||
| 290 | Enums: enums[fn], | 341 | Enums: enums[fn], | ||
| 342 | + | Constants: constants[fn], | |||
| 291 | Functions: functions[fn], | 343 | Functions: functions[fn], | ||
| 292 | Interfaces: interfaces[fn], | 344 | Interfaces: interfaces[fn], | ||
| 293 | Classes: classes[fn], | 345 | Classes: classes[fn], | ||
M internal/gir/types/template.go +21 -0
| @@ -174,6 +174,12 @@ type RecordTemplate struct { | |||
| 174 | 174 | // Doc is the documentation of the alias | |
| 175 | 175 | Doc string | |
| 176 | 176 | ||
| 177 | + | // Constructors is the slice of functions that create the class struct | |
| 178 | + | Constructors []FuncTemplate | |
| 179 | + | ||
| 180 | + | // Receivers is the slice of functions that have value receivers to the struct | |
| 181 | + | Receivers []FuncTemplate | |
| 182 | + | ||
| 177 | 183 | // Fields is the list of record fields | |
| 178 | 184 | Fields []RecordField | |
| 179 | 185 | } | |
| @@ -196,6 +202,17 @@ type EnumTemplate struct { | |||
| 196 | 202 | Values []enumValues | |
| 197 | 203 | } | |
| 198 | 204 | ||
| 205 | + | type ConstantTemplate struct { | |
| 206 | + | // Name is the name of the constant | |
| 207 | + | Name string | |
| 208 | + | // Doc is the documentation for the constant | |
| 209 | + | Doc string | |
| 210 | + | // Type is the Go type for the constant | |
| 211 | + | Type string | |
| 212 | + | // Values are the list of values for the constant | |
| 213 | + | Value string | |
| 214 | + | } | |
| 215 | + | ||
| 199 | 216 | type funcRetTemplate struct { | |
| 200 | 217 | // Raw is the raw value for the underlying purego function | |
| 201 | 218 | Raw string | |
| @@ -342,6 +359,8 @@ type ClassTemplate struct { | |||
| 342 | 359 | Receivers []FuncTemplate | |
| 343 | 360 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | |
| 344 | 361 | Interfaces []InterfaceTemplate | |
| 362 | + | // Functions are the Go function declarations | |
| 363 | + | Functions []FuncTemplate | |
| 345 | 364 | // Signals are helpers for ConnectX receivers | |
| 346 | 365 | Signals []SignalsTemplate | |
| 347 | 366 | } | |
| @@ -372,6 +391,8 @@ type TemplateArg struct { | |||
| 372 | 391 | Callbacks []CallbackTemplate | |
| 373 | 392 | // Enums are enumerations declared as const ... .... = .... | |
| 374 | 393 | Enums []EnumTemplate | |
| 394 | + | // Constants are declared as const ... .... = .... | |
| 395 | + | Constants []ConstantTemplate | |
| 375 | 396 | // Functions are the Go function declarations | |
| 376 | 397 | Functions []FuncTemplate | |
| 377 | 398 | // Interfaces is the list of interfaces that this package implements | |
| @@ -174,6 +174,12 @@ type RecordTemplate struct { | |||||
| 174 | // Doc is the documentation of the alias | 174 | // Doc is the documentation of the alias | ||
| 175 | Doc string | 175 | Doc string | ||
| 176 | 176 | ||||
| 177 | + | // Constructors is the slice of functions that create the class struct | |||
| 178 | + | Constructors []FuncTemplate | |||
| 179 | + | ||||
| 180 | + | // Receivers is the slice of functions that have value receivers to the struct | |||
| 181 | + | Receivers []FuncTemplate | |||
| 182 | + | ||||
| 177 | // Fields is the list of record fields | 183 | // Fields is the list of record fields | ||
| 178 | Fields []RecordField | 184 | Fields []RecordField | ||
| 179 | } | 185 | } | ||
| @@ -196,6 +202,17 @@ type EnumTemplate struct { | |||||
| 196 | Values []enumValues | 202 | Values []enumValues | ||
| 197 | } | 203 | } | ||
| 198 | 204 | ||||
| 205 | + | type ConstantTemplate struct { | |||
| 206 | + | // Name is the name of the constant | |||
| 207 | + | Name string | |||
| 208 | + | // Doc is the documentation for the constant | |||
| 209 | + | Doc string | |||
| 210 | + | // Type is the Go type for the constant | |||
| 211 | + | Type string | |||
| 212 | + | // Values are the list of values for the constant | |||
| 213 | + | Value string | |||
| 214 | + | } | |||
| 215 | + | ||||
| 199 | type funcRetTemplate struct { | 216 | type funcRetTemplate struct { | ||
| 200 | // Raw is the raw value for the underlying purego function | 217 | // Raw is the raw value for the underlying purego function | ||
| 201 | Raw string | 218 | Raw string | ||
| @@ -342,6 +359,8 @@ type ClassTemplate struct { | |||||
| 342 | Receivers []FuncTemplate | 359 | Receivers []FuncTemplate | ||
| 343 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | 360 | // Interfaces are receiver methods that are implemented because it needs to satisfy a certain interface | ||
| 344 | Interfaces []InterfaceTemplate | 361 | Interfaces []InterfaceTemplate | ||
| 362 | + | // Functions are the Go function declarations | |||
| 363 | + | Functions []FuncTemplate | |||
| 345 | // Signals are helpers for ConnectX receivers | 364 | // Signals are helpers for ConnectX receivers | ||
| 346 | Signals []SignalsTemplate | 365 | Signals []SignalsTemplate | ||
| 347 | } | 366 | } | ||
| @@ -372,6 +391,8 @@ type TemplateArg struct { | |||||
| 372 | Callbacks []CallbackTemplate | 391 | Callbacks []CallbackTemplate | ||
| 373 | // Enums are enumerations declared as const ... .... = .... | 392 | // Enums are enumerations declared as const ... .... = .... | ||
| 374 | Enums []EnumTemplate | 393 | Enums []EnumTemplate | ||
| 394 | + | // Constants are declared as const ... .... = .... | |||
| 395 | + | Constants []ConstantTemplate | |||
| 375 | // Functions are the Go function declarations | 396 | // Functions are the Go function declarations | ||
| 376 | Functions []FuncTemplate | 397 | Functions []FuncTemplate | ||
| 377 | // Interfaces is the list of interfaces that this package implements | 398 | // Interfaces is the list of interfaces that this package implements | ||
M internal/gir/types/types.go +18 -0
| @@ -4,6 +4,7 @@ package types | |||
| 4 | 4 | ||
| 5 | 5 | import ( | |
| 6 | 6 | "encoding/xml" | |
| 7 | + | "fmt" | |
| 7 | 8 | "strconv" | |
| 8 | 9 | "strings" | |
| 9 | 10 | ||
| @@ -333,6 +334,23 @@ type Constant struct { | |||
| 333 | 334 | InfoElements | |
| 334 | 335 | } | |
| 335 | 336 | ||
| 337 | + | func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate { | |
| 338 | + | t := c.Type.Template(ns, kinds) | |
| 339 | + | v := c.Value | |
| 340 | + | ||
| 341 | + | switch t { | |
| 342 | + | case "string": | |
| 343 | + | v = fmt.Sprintf(`"%s"`, v) | |
| 344 | + | } | |
| 345 | + | ||
| 346 | + | return ConstantTemplate{ | |
| 347 | + | Name: c.Name, | |
| 348 | + | Doc: c.Doc.StringSafe(), | |
| 349 | + | Type: t, | |
| 350 | + | Value: v, | |
| 351 | + | } | |
| 352 | + | } | |
| 353 | + | ||
| 336 | 354 | type Constructor struct { | |
| 337 | 355 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | |
| 338 | 356 | CallableAttrs | |
| @@ -4,6 +4,7 @@ package types | |||||
| 4 | 4 | ||||
| 5 | import ( | 5 | import ( | ||
| 6 | "encoding/xml" | 6 | "encoding/xml" | ||
| 7 | + | "fmt" | |||
| 7 | "strconv" | 8 | "strconv" | ||
| 8 | "strings" | 9 | "strings" | ||
| 9 | 10 | ||||
| @@ -333,6 +334,23 @@ type Constant struct { | |||||
| 333 | InfoElements | 334 | InfoElements | ||
| 334 | } | 335 | } | ||
| 335 | 336 | ||||
| 337 | + | func (c *Constant) Template(ns string, kinds KindMap) ConstantTemplate { | |||
| 338 | + | t := c.Type.Template(ns, kinds) | |||
| 339 | + | v := c.Value | |||
| 340 | + | ||||
| 341 | + | switch t { | |||
| 342 | + | case "string": | |||
| 343 | + | v = fmt.Sprintf(`"%s"`, v) | |||
| 344 | + | } | |||
| 345 | + | ||||
| 346 | + | return ConstantTemplate{ | |||
| 347 | + | Name: c.Name, | |||
| 348 | + | Doc: c.Doc.StringSafe(), | |||
| 349 | + | Type: t, | |||
| 350 | + | Value: v, | |||
| 351 | + | } | |||
| 352 | + | } | |||
| 353 | + | ||||
| 336 | type Constructor struct { | 354 | type Constructor struct { | ||
| 337 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | 355 | XMLName xml.Name `xml:"http://www.gtk.org/introspection/core/1.0 constructor"` | ||
| 338 | CallableAttrs | 356 | CallableAttrs | ||
M templates/go +66 -0
| @@ -30,6 +30,37 @@ type {{.Name}} struct { | |||
| 30 | 30 | {{.Name}} {{.Type}} | |
| 31 | 31 | {{end}} | |
| 32 | 32 | } | |
| 33 | + | ||
| 34 | + | func (x *{{.Name}}) GoPointer() uintptr { | |
| 35 | + | return uintptr(unsafe.Pointer(x)) | |
| 36 | + | } | |
| 37 | + | ||
| 38 | + | {{$outer := .}} | |
| 39 | + | ||
| 40 | + | {{range .Constructors -}} | |
| 41 | + | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 42 | + | ||
| 43 | + | ||
| 44 | + | {{.Doc}} | |
| 45 | + | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 46 | + | {{.Ret.Preamble $NotGLib}} | |
| 47 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | |
| 48 | + | {{.Ret.Fmt $NotGObject}} | |
| 49 | + | } | |
| 50 | + | {{end}} | |
| 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 | + | ||
| 33 | 64 | {{end}} | |
| 34 | 65 | ||
| 35 | 66 | {{range .Interfaces -}} | |
| @@ -73,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret | |||
| 73 | 104 | type {{.Name}} = {{.Value}} | |
| 74 | 105 | {{end}} | |
| 75 | 106 | ||
| 107 | + | {{if .Constants -}} | |
| 108 | + | const ( | |
| 109 | + | {{range .Constants -}} | |
| 110 | + | {{.Doc}} | |
| 111 | + | {{.Name}} {{.Type}} = {{.Value}} | |
| 112 | + | {{end}} | |
| 113 | + | ) | |
| 114 | + | {{end}} | |
| 115 | + | ||
| 76 | 116 | {{range .Enums -}} | |
| 77 | 117 | {{.Doc}} | |
| 78 | 118 | type {{.Name}} int | |
| @@ -175,6 +215,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 175 | 215 | {{end}} | |
| 176 | 216 | {{end}} | |
| 177 | 217 | ||
| 218 | + | {{range .Functions -}} | |
| 219 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |
| 220 | + | ||
| 221 | + | {{.Doc}} | |
| 222 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |
| 223 | + | {{.Ret.Preamble $NotGLib}} | |
| 224 | + | {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}}) | |
| 225 | + | {{.Ret.Fmt $NotGObject}} | |
| 226 | + | } | |
| 227 | + | {{end}} | |
| 228 | + | ||
| 178 | 229 | {{end}} | |
| 179 | 230 | ||
| 180 | 231 | ||
| @@ -188,6 +239,17 @@ func init() { | |||
| 188 | 239 | {{range .Functions -}} | |
| 189 | 240 | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 190 | 241 | {{end}} | |
| 242 | + | ||
| 243 | + | {{range .Records -}} | |
| 244 | + | {{$rec := .}} | |
| 245 | + | {{range .Constructors -}} | |
| 246 | + | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") | |
| 247 | + | {{end}} | |
| 248 | + | {{range .Receivers -}} | |
| 249 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | |
| 250 | + | {{end}} | |
| 251 | + | {{end}} | |
| 252 | + | ||
| 191 | 253 | {{range .Classes -}} | |
| 192 | 254 | {{$cls := .}} | |
| 193 | 255 | {{range .Constructors -}} | |
| @@ -196,7 +258,11 @@ func init() { | |||
| 196 | 258 | {{range .Receivers -}} | |
| 197 | 259 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | |
| 198 | 260 | {{end}} | |
| 261 | + | {{range .Functions -}} | |
| 262 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |
| 263 | + | {{end}} | |
| 199 | 264 | {{end}} | |
| 265 | + | ||
| 200 | 266 | {{range .Interfaces -}} | |
| 201 | 267 | {{range .Methods -}} | |
| 202 | 268 | core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}") | |
| @@ -30,6 +30,37 @@ type {{.Name}} struct { | |||||
| 30 | {{.Name}} {{.Type}} | 30 | {{.Name}} {{.Type}} | ||
| 31 | {{end}} | 31 | {{end}} | ||
| 32 | } | 32 | } | ||
| 33 | + | ||||
| 34 | + | func (x *{{.Name}}) GoPointer() uintptr { | |||
| 35 | + | return uintptr(unsafe.Pointer(x)) | |||
| 36 | + | } | |||
| 37 | + | ||||
| 38 | + | {{$outer := .}} | |||
| 39 | + | ||||
| 40 | + | {{range .Constructors -}} | |||
| 41 | + | var x{{.Name}}{{$outer.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |||
| 42 | + | ||||
| 43 | + | ||||
| 44 | + | {{.Doc}} | |||
| 45 | + | func {{.Name}}{{$outer.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 46 | + | {{.Ret.Preamble $NotGLib}} | |||
| 47 | + | {{if .Ret.Value}}cret :={{end}}x{{.Name}}{{$outer.Name}}({{conv .Args.API.Call}}) | |||
| 48 | + | {{.Ret.Fmt $NotGObject}} | |||
| 49 | + | } | |||
| 50 | + | {{end}} | |||
| 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 | + | ||||
| 33 | {{end}} | 64 | {{end}} | ||
| 34 | 65 | ||||
| 35 | {{range .Interfaces -}} | 66 | {{range .Interfaces -}} | ||
| @@ -73,6 +104,15 @@ var {{.Namespace}}X{{.FullName}} func(uintptr {{convc .Args.Pure.Types}}) {{.Ret | |||||
| 73 | type {{.Name}} = {{.Value}} | 104 | type {{.Name}} = {{.Value}} | ||
| 74 | {{end}} | 105 | {{end}} | ||
| 75 | 106 | ||||
| 107 | + | {{if .Constants -}} | |||
| 108 | + | const ( | |||
| 109 | + | {{range .Constants -}} | |||
| 110 | + | {{.Doc}} | |||
| 111 | + | {{.Name}} {{.Type}} = {{.Value}} | |||
| 112 | + | {{end}} | |||
| 113 | + | ) | |||
| 114 | + | {{end}} | |||
| 115 | + | ||||
| 76 | {{range .Enums -}} | 116 | {{range .Enums -}} | ||
| 77 | {{.Doc}} | 117 | {{.Doc}} | ||
| 78 | type {{.Name}} int | 118 | type {{.Name}} int | ||
| @@ -175,6 +215,17 @@ func (x *{{$outer.Name}}) {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||||
| 175 | {{end}} | 215 | {{end}} | ||
| 176 | {{end}} | 216 | {{end}} | ||
| 177 | 217 | ||||
| 218 | + | {{range .Functions -}} | |||
| 219 | + | var x{{.Name}} func({{conv .Args.Pure.Types}}) {{.Ret.Raw}} | |||
| 220 | + | ||||
| 221 | + | {{.Doc}} | |||
| 222 | + | func {{.Name}}({{conv .Args.API.Full}}) {{.Ret.Return}} { | |||
| 223 | + | {{.Ret.Preamble $NotGLib}} | |||
| 224 | + | {{if .Ret.Value}}cret := {{end}}x{{.Name}}({{conv .Args.API.Call}}) | |||
| 225 | + | {{.Ret.Fmt $NotGObject}} | |||
| 226 | + | } | |||
| 227 | + | {{end}} | |||
| 228 | + | ||||
| 178 | {{end}} | 229 | {{end}} | ||
| 179 | 230 | ||||
| 180 | 231 | ||||
| @@ -188,6 +239,17 @@ func init() { | |||||
| 188 | {{range .Functions -}} | 239 | {{range .Functions -}} | ||
| 189 | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | 240 | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | ||
| 190 | {{end}} | 241 | {{end}} | ||
| 242 | + | ||||
| 243 | + | {{range .Records -}} | |||
| 244 | + | {{$rec := .}} | |||
| 245 | + | {{range .Constructors -}} | |||
| 246 | + | core.PuregoSafeRegister(&x{{.Name}}{{$rec.Name}}, lib, "{{.CName}}") | |||
| 247 | + | {{end}} | |||
| 248 | + | {{range .Receivers -}} | |||
| 249 | + | core.PuregoSafeRegister(&x{{$rec.Name}}{{.Name}}, lib, "{{.CName}}") | |||
| 250 | + | {{end}} | |||
| 251 | + | {{end}} | |||
| 252 | + | ||||
| 191 | {{range .Classes -}} | 253 | {{range .Classes -}} | ||
| 192 | {{$cls := .}} | 254 | {{$cls := .}} | ||
| 193 | {{range .Constructors -}} | 255 | {{range .Constructors -}} | ||
| @@ -196,7 +258,11 @@ func init() { | |||||
| 196 | {{range .Receivers -}} | 258 | {{range .Receivers -}} | ||
| 197 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | 259 | core.PuregoSafeRegister(&x{{$cls.Name}}{{.Name}}, lib, "{{.CName}}") | ||
| 198 | {{end}} | 260 | {{end}} | ||
| 261 | + | {{range .Functions -}} | |||
| 262 | + | core.PuregoSafeRegister(&x{{.Name}}, lib, "{{.CName}}") | |||
| 263 | + | {{end}} | |||
| 199 | {{end}} | 264 | {{end}} | ||
| 265 | + | ||||
| 200 | {{range .Interfaces -}} | 266 | {{range .Interfaces -}} | ||
| 201 | {{range .Methods -}} | 267 | {{range .Methods -}} | ||
| 202 | core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}") | 268 | core.PuregoSafeRegister(&{{.Namespace}}X{{.FullName}}, lib, "{{.CName}}") | ||
M templates/gobject +2 -32
| @@ -1,11 +1,9 @@ | |||
| 1 | 1 | package gobject | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | - | "os" | |
| 5 | 4 | "reflect" | |
| 6 | 5 | ||
| 7 | 6 | "github.com/jwijenbergh/purego" | |
| 8 | - | "github.com/jwijenbergh/puregotk/internal/core" | |
| 9 | 7 | ) | |
| 10 | 8 | ||
| 11 | 9 | type Ptr interface { | |
| @@ -39,11 +37,11 @@ func (o Object) Cast(v Ptr) { | |||
| 39 | 37 | } | |
| 40 | 38 | ||
| 41 | 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 | 43 | func (o Object) DisconnectSignal(handler uint32) { | |
| 46 | - | SignalHandlerDisconnect(&o, handler) | |
| 44 | + | SignalHandlerDisconnect(&o, handler) | |
| 47 | 45 | } | |
| 48 | 46 | ||
| 49 | 47 | // NewCallback is an alias to purego.NewCallback | |
| @@ -79,31 +77,3 @@ const ( | |||
| 79 | 77 | TypeReservedBseLastVal = 48 << 2 | |
| 80 | 78 | 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 | - | } | |
| @@ -1,11 +1,9 @@ | |||||
| 1 | package gobject | 1 | 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.NewCallback | 47 | // NewCallback is an alias to purego.NewCallback | ||
| @@ -79,31 +77,3 @@ const ( | |||||
| 79 | TypeReservedBseLastVal = 48 << 2 | 77 | TypeReservedBseLastVal = 48 << 2 | ||
| 80 | TypeReservedUserFirstVal = 49 << 2 | 78 | 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 | - | } | |||