-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-tobject.go
159 lines (130 loc) · 3.15 KB
/
gen-tobject.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// +build ignore
package main
import (
"log"
"os"
"os/exec"
"strings"
"text/template"
)
var (
TObjects = []Type{
{Name: "branchImpl", Var: "br"},
{Name: "branchElementImpl", Var: "be"},
{Name: "classImpl", Var: "c"},
{Name: "dataMemberImpl", Var: "dm"},
{Name: "dataTypeImpl", Var: "dt"},
{Name: "fileImpl", Var: "f"},
{Name: "h1FImpl", Var: "h"},
{Name: "leafImpl", Var: "leaf"},
{Name: "leafDImpl", Var: "leaf"},
{Name: "leafFImpl", Var: "leaf"},
{Name: "leafIImpl", Var: "leaf"},
{Name: "leafOImpl", Var: "leaf"},
{Name: "objArrayImpl", Var: "obj"},
{Name: "randomImpl", Var: "rndm"},
{
Name: "treeImpl",
Var: "tree",
PrintImpl: `func (t *treeImpl) Print(option Option) {
coption := C.CString(string(option))
defer C.free(unsafe.Pointer(coption))
C.CRoot_Tree_Print(t.c, (*C.CRoot_Option)(coption))
}`,
},
}
)
type Type struct {
Name string
Var string
PrintImpl string
}
func (t Type) ROOTType() string {
return "T" + strings.Title(t.Name)[:len(t.Name)-len("impl")]
}
func (t Type) CRootType() string {
return "CRoot_" + strings.Title(t.Name)[:len(t.Name)-len("impl")]
}
func main() {
f, err := os.Create("object_impl.go")
if err != nil {
log.Fatalf("error creating [object_impl.go]: %v\n", err)
}
defer f.Close()
tmpl, err := template.New("tobjects").Parse(tmplTObject)
if err != nil {
log.Fatalf("error parsing template: %v\n", err)
}
err = tmpl.Execute(f, TObjects)
if err != nil {
log.Fatalf("error generating TObject implementation: %v\n",
err,
)
}
err = f.Close()
if err != nil {
log.Fatalf("error closing [%s]: %v\n", f.Name(), err)
}
cmd := exec.Command("gofmt", "-w", f.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatalf("error gofmt-ing [%s]: %v\n", f.Name(), err)
}
}
const tmplTObject = `// automatically generated.
// DO NOT EDIT
package croot
// #include "croot/croot.h"
//
// #include <stdlib.h>
// #include <string.h>
//
import "C"
import (
"unsafe"
)
{{range .}}
//
// --- TObject impl for {{.Name}} ({{.ROOTType}}) ---
//
func ({{.Var}} *{{.Name}}) cptr() C.CRoot_Object {
return (C.CRoot_Object)({{.Var}}.c)
}
func ({{.Var}} *{{.Name}}) as_tobject() *objectImpl {
return &objectImpl{ {{.Var}}.cptr() }
}
func ({{.Var}} *{{.Name}}) ClassName() string {
return {{.Var}}.as_tobject().ClassName()
}
func ({{.Var}} *{{.Name}}) Clone(opt Option) Object {
return {{.Var}}.as_tobject().Clone(opt)
}
func ({{.Var}} *{{.Name}}) FindObject(name string) Object {
return {{.Var}}.as_tobject().FindObject(name)
}
func ({{.Var}} *{{.Name}}) GetName() string {
return {{.Var}}.as_tobject().GetName()
}
func ({{.Var}} *{{.Name}}) GetTitle() string {
return {{.Var}}.as_tobject().GetTitle()
}
func ({{.Var}} *{{.Name}}) InheritsFrom(clsname string) bool {
return {{.Var}}.as_tobject().InheritsFrom(clsname)
}
{{if .PrintImpl}}{{.PrintImpl}}{{else}}func ({{.Var}} *{{.Name}}) Print(option Option) {
{{.Var}}.as_tobject().Print(option)
}
{{end}}
{{end}}
func init() {
// register conversions
{{range .}}
cnvmap["{{.ROOTType}}"] = func(o c_object) Object {
return &{{.Name}}{ c: (C.{{.CRootType}})(o.cptr())}
}
{{end}}
}
`