types.go.tmpl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. {{ $view := . -}}
  2. // Copyright © 2019 - 2020 Oscar Campos <oscar.campos@thepimpam.com>
  3. // Copyright © 2017 - William Edwards
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. package gdnative
  17. // ==================================================================
  18. // This file was autogenerated by PimPam GDNative-Go binding tools
  19. // Please do not modify this file, any change will be lost
  20. // ==================================================================
  21. /*
  22. #include "gdnative.gen.h"
  23. {{ range $i, $header := $view.Headers -}}
  24. #include <{{ $header }}>
  25. {{ end -}}
  26. // Include all headers for now. TODO: Look up all the required
  27. // headers we need to import based on the method arguments and return types.
  28. #include <gdnative/aabb.h>
  29. #include <gdnative/array.h>
  30. #include <gdnative/basis.h>
  31. #include <gdnative/color.h>
  32. #include <gdnative/dictionary.h>
  33. #include <gdnative/gdnative.h>
  34. #include <gdnative/node_path.h>
  35. #include <gdnative/plane.h>
  36. #include <gdnative/pool_arrays.h>
  37. #include <gdnative/quat.h>
  38. #include <gdnative/rect2.h>
  39. #include <gdnative/rid.h>
  40. #include <gdnative/string.h>
  41. #include <gdnative/string_name.h>
  42. #include <gdnative/transform.h>
  43. #include <gdnative/transform2d.h>
  44. #include <gdnative/variant.h>
  45. #include <gdnative/vector2.h>
  46. #include <gdnative/vector3.h>
  47. #include <gdnative_api_struct.gen.h>
  48. */
  49. import "C"
  50. {{/* Loop through and define all of the type definitions as Go structs -*/}}
  51. {{ range $i, $typedef := $view.TypeDefinitions -}}
  52. {{/* Handle struct definitions */}}
  53. {{ if or (eq $typedef.Base "struct") (eq $typedef.Base "void") -}}
  54. // NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
  55. // initialized {{ $typedef.GoName }}. This is primarily used in
  56. // conjunction with MethodBindPtrCall.
  57. func NewEmpty{{ $typedef.GoName }}() Pointer {
  58. var obj C.{{ $typedef.Name }}
  59. return Pointer{base: unsafe.Pointer(&obj)}
  60. }
  61. // NewPointerFrom{{ $typedef.GoName }} will return an unsafe pointer to the given
  62. // object. This is primarily used in conjunction with MethodBindPtrCall.
  63. func NewPointerFrom{{ $typedef.GoName }}(obj {{ $typedef.GoName }}) Pointer {
  64. return Pointer{base: unsafe.Pointer(obj.getBase())}
  65. }
  66. // New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
  67. // given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
  68. func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
  69. {{/* String structs should be handled differently -*/}}
  70. {{ if (eq $typedef.GoName "String") -}}
  71. base := (*C.godot_string)(ptr.getBase())
  72. utfStr := C.go_godot_string_utf8(GDNative.api, base)
  73. char := C.go_godot_char_string_get_data(GDNative.api, &utfStr)
  74. goStr := C.GoString(char)
  75. C.go_godot_char_string_destroy(GDNative.api, &utfStr)
  76. return String(goStr)
  77. {{ else -}}
  78. {{ if (eq $typedef.GoName "Object") -}}
  79. obj := (**C.{{ $typedef.Name }})(ptr.getBase())
  80. return {{ $typedef.GoName }}{base: *obj}
  81. {{ else -}}
  82. return {{ $typedef.GoName }}{base: (*C.{{ $typedef.Name }})(ptr.getBase())}
  83. {{ end -}}
  84. {{ end -}}
  85. }
  86. {{/* The String struct is a special case to be compatible with Go strings -*/}}
  87. {{ if (eq $typedef.GoName "String") -}}
  88. type {{ $typedef.GoName }} string
  89. func (s String) getBase() *C.godot_string {
  90. return stringAsGodotString(string(s))
  91. }
  92. {{/* Handle all other kinds of Godot structs -*/}}
  93. {{ else }}
  94. // {{ $typedef.GoName }} data structure wrapper
  95. type {{ $typedef.GoName }} struct {
  96. base *C.{{ $typedef.Name }}
  97. {{/* Handle struct properties */}}
  98. {{ range $j, $propdef := $typedef.Properties -}}
  99. {{ if ($view.IsValidProperty $propdef) -}}
  100. {{ if $propdef.IsPointer -}}
  101. {{ if (eq $typedef.GoName "Signal" ) -}}
  102. {{ $propdef.GoName }} []{{ $view.ToGoName $propdef.Base }}
  103. {{ end -}}
  104. {{ else -}}
  105. {{ $propdef.GoName }} {{ $view.ToGoName $propdef.Base }}
  106. {{ end -}}
  107. {{ end -}}
  108. {{ end -}}
  109. }
  110. // returns the wrapped C base data type for this type
  111. func (gdt {{ $typedef.GoName }}) getBase() *C.{{ $typedef.Name }} {
  112. return gdt.base
  113. }
  114. {{ end }}
  115. {{/* Handle struct methods */}}
  116. {{ range $j, $method := ($view.MethodsList $typedef) -}}
  117. {{/* Handle constructor methods -*/}}
  118. // {{ $view.ToGoMethodName $typedef $method }} {{ $method.Name }} {{ $method.Arguments }} {{ $method.ReturnType }}
  119. {{ if ($view.MethodIsConstructor $method) -}}
  120. func {{ $view.ToGoMethodName $typedef $method }}({{ range $k, $arg := $method.Arguments }}{{ if (ne $k 0) }}{{ $view.ToGoArgName (index $arg 1) }} {{ $view.ToGoArgType (index $arg 0) true }}, {{ end }}{{ end }}) {{ $typedef.GoName }} {
  121. var dest C.{{ $view.StripPointer (index (index $method.Arguments 0) 0) }} {{/* First argument is always the destination */}}
  122. {{ range $k, $arg := $method.Arguments -}}
  123. {{ if (ne $k 0 ) -}}
  124. {{ if $view.IsVoidPointerType (index $arg 0) -}}
  125. arg{{ $k }} := unsafe.Pointer({{ $view.ToGoArgName (index $arg 1) }}.getBase())
  126. {{ else -}}
  127. {{ if $view.IsDoublePointer (index $arg 0) -}}
  128. arg{{ $k }}Array := {{ $view.ToGoArgType (index $arg 0) false }}Array{array: {{ $view.ToGoArgName (index $arg 1) }}}
  129. arg{{ $k }} := arg{{ $k }}Array.getBase()
  130. {{ else -}}
  131. arg{{ $k }} := {{ $view.ToGoArgName (index $arg 1) }}.getBase()
  132. {{ end -}}
  133. {{ end -}}
  134. {{ end -}}
  135. {{ end -}}
  136. C.go_{{ $method.Name }}(GDNative.api, {{ range $k, $arg := $method.Arguments }}{{ if (eq $k 0) }}&dest, {{ else }}{{ $view.OutputCArg $arg }}arg{{ $k }}, {{ end }}{{ end }})
  137. return {{ $typedef.GoName }}{base: &dest}
  138. }
  139. {{/* Handle all other kinds of methods -*/}}
  140. {{ else -}}
  141. func (gdt *{{ $typedef.GoName }}) {{ $view.ToGoMethodName $typedef $method }}({{ range $k, $arg := $method.Arguments }}{{ if $view.NotSelfArg (index $arg 1) }}{{ $view.ToGoArgName (index $arg 1) }} {{ $view.ToGoArgType (index $arg 0) true }}, {{ end }}{{ end }}) {{ $view.ToGoReturnType $method.ReturnType }} {
  142. {{ range $k, $arg := $method.Arguments -}}
  143. {{ if $view.NotSelfArg (index $arg 1) -}}
  144. {{ if $view.IsVoidPointerType (index $arg 0) -}}
  145. arg{{ $k }} := unsafe.Pointer({{ $view.ToGoArgName (index $arg 1) }}.getBase())
  146. {{ else -}}
  147. {{ if $view.IsDoublePointer (index $arg 0) -}}
  148. arg{{ $k }}Array := {{ $view.ToGoArgType (index $arg 0) false }}Array{array: {{ $view.ToGoArgName (index $arg 1) }}}
  149. arg{{ $k }} := arg{{ $k }}Array.getBase()
  150. {{ else -}}
  151. arg{{ $k }} := {{ $view.ToGoArgName (index $arg 1) }}.getBase()
  152. {{ end -}}
  153. {{ end -}}
  154. {{ else -}}
  155. arg{{ $k }} := gdt.getBase()
  156. {{ end -}}
  157. {{ end }}
  158. {{ if $view.HasReturn $method.ReturnType }}
  159. ret := C.go_{{ $method.Name }}(GDNative.api, {{ range $k, $arg := $method.Arguments }}{{ $view.OutputCArg $arg }}arg{{ $k }}, {{ end }})
  160. {{ if $view.IsBasicType ($view.ToGoReturnType $method.ReturnType) -}}
  161. {{ if ($view.IsWcharT $method.ReturnType ) -}}
  162. {{ if ($view.HasPointerReturn $method.ReturnType) -}}
  163. return newWcharT(ret)
  164. {{ else }}
  165. return newWcharT(&ret)
  166. {{ end }}
  167. {{ else -}}
  168. return {{ $view.ToGoReturnType $method.ReturnType }}(ret)
  169. {{ end -}}
  170. {{ else -}}
  171. {{ if $view.HasPointerReturn $method.ReturnType }}
  172. {{ if (eq $method.ReturnType "godot_object *") -}}
  173. return {{ $view.ToGoReturnType $method.ReturnType }}{base: (*C.godot_object)(ret)}
  174. {{ else }}
  175. return {{ $view.ToGoReturnType $method.ReturnType }}{base: ret}
  176. {{ end }}
  177. {{ else }}
  178. {{ if (eq $method.ReturnType "godot_string") -}}
  179. utfStr := C.go_godot_string_utf8(GDNative.api, &ret)
  180. char := C.go_godot_char_string_get_data(GDNative.api, &utfStr)
  181. goStr := C.GoString(char)
  182. C.go_godot_char_string_destroy(GDNative.api, &utfStr)
  183. return String(goStr)
  184. {{ else -}}
  185. return {{ $view.ToGoReturnType $method.ReturnType }}{base: &ret}
  186. {{ end -}}
  187. {{ end }}
  188. {{ end -}}
  189. {{ else -}}
  190. C.go_{{ $method.Name }}(GDNative.api, {{ range $k, $arg := $method.Arguments }}arg{{ $k }}, {{ end }})
  191. {{ end -}}
  192. }
  193. {{ end }}
  194. {{ end }}
  195. {{ end }}
  196. {{/* Handle enum definitions */}}
  197. {{ if eq $typedef.Base "enum" }}
  198. // {{ $typedef.GoName }} is a Go wrapper for the C.{{ $typedef.Name }} enum type.
  199. type {{ $typedef.GoName }} int
  200. func (e {{ $typedef.GoName }}) getBase() C.{{ $typedef.Name }} {
  201. return C.{{ $typedef.Name }}(e)
  202. }
  203. const (
  204. {{ range $j, $propdef := $typedef.Properties -}}
  205. {{ if (ne $propdef.GoName "") -}}
  206. {{ $propdef.GoName }} {{ $typedef.GoName }} = {{ $j }} {{ if $propdef.Comment }}// {{ $propdef.Comment }}{{ end }}
  207. {{ end -}}
  208. {{ end -}}
  209. )
  210. // {{ $typedef.GoName }}LookupMap is a string-based lookup table of constants for {{ $typedef.GoName }}.
  211. var {{ $typedef.GoName }}LookupMap = map[string]{{ $typedef.GoName }}{
  212. {{ range $j, $propdef := $typedef.Properties -}}
  213. {{ if (ne $propdef.GoName "") -}}
  214. "{{ $propdef.GoName }}": {{ $propdef.GoName }},
  215. {{ end -}}
  216. {{ end -}}
  217. }
  218. {{ end }}
  219. {{/* Handle base type definitions */}}
  220. {{ if and ($typedef.SimpleType) (ne $typedef.Base "void") }}
  221. {{/* This segment handles godot_pool_*_access type definitions */}}
  222. {{ if ($view.IsGodotBaseType $typedef) }}
  223. // NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
  224. // initialized {{ $typedef.GoName }}. This is primarily used in
  225. // conjunction with MethodBindPtrCall.
  226. func NewEmpty{{ $typedef.GoName }}() Pointer {
  227. var obj C.{{ $typedef.Name }}
  228. return Pointer{base: unsafe.Pointer(&obj)}
  229. }
  230. // NewPointerFrom{{ $typedef.GoName }} will return an unsafe pointer to the given
  231. // object. This is primarily used in conjunction with MethodBindPtrCall.
  232. func NewPointerFrom{{ $typedef.GoName }}(obj {{ $typedef.GoName }}) Pointer {
  233. return Pointer{base: unsafe.Pointer(obj.getBase())}
  234. }
  235. // New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
  236. // given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
  237. func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
  238. obj := (*C.{{ $typedef.Name }})(ptr.getBase())
  239. return {{ $typedef.GoName }}{base: obj}
  240. }
  241. type {{ $typedef.GoName }} struct {
  242. base *C.{{ $typedef.Name }}
  243. {{/* Handle struct properties */}}
  244. {{ range $j, $propdef := $typedef.Properties -}}
  245. {{ if ($view.IsValidProperty $propdef) -}}
  246. {{ $propdef.GoName }} {{ $view.ToGoName $propdef.Base }}
  247. {{ end -}}
  248. {{ end -}}
  249. }
  250. func (gdt {{ $typedef.GoName }}) getBase() *C.{{ $typedef.Name }} {
  251. return gdt.base
  252. }
  253. {{/* This segment handles base types like godot_bool */}}
  254. {{ else }}
  255. // NewEmpty{{ $typedef.GoName }} will return a pointer to an empty
  256. // initialized {{ $typedef.GoName }}. This is primarily used in
  257. // conjunction with MethodBindPtrCall.
  258. func NewEmpty{{ $typedef.GoName }}() Pointer {
  259. var obj C.{{ $typedef.Name }}
  260. return Pointer{base: unsafe.Pointer(&obj)}
  261. }
  262. // NewPointerFrom{{ $typedef.GoName }} will return an unsafe pointer to the given
  263. // object. This is primarily used in conjunction with MethodBindPtrCall.
  264. func NewPointerFrom{{ $typedef.GoName }}(obj {{ $typedef.GoName }}) Pointer {
  265. base := obj.getBase()
  266. return Pointer{base: unsafe.Pointer(&base)}
  267. }
  268. // New{{ $typedef.GoName }}FromPointer will return a {{ $typedef.GoName }} from the
  269. // given unsafe pointer. This is primarily used in conjunction with MethodBindPtrCall.
  270. func New{{ $typedef.GoName }}FromPointer(ptr Pointer) {{ $typedef.GoName }} {
  271. base := ptr.getBase()
  272. return {{ $typedef.GoName }}(*(*C.{{ $typedef.Name }})(base))
  273. }
  274. // {{ $typedef.GoName }} is a Go wrapper for the base C.{{ $typedef.Name }} type
  275. type {{ $typedef.GoName }} {{ $view.ToGoBaseType $typedef.Base }}
  276. func (t {{ $typedef.GoName }}) getBase() C.{{ $typedef.Name }} {
  277. return C.{{ $typedef.Name }}(t)
  278. }
  279. {{ end }}
  280. {{ end }}
  281. {{ end }}