@@ -9,24 +9,6 @@ import (
99 "github.com/thoas/go-funk"
1010)
1111
12- type TypeMap struct {
13- GoType string
14- CgoWrapper string
15- }
16-
17- func tm (goType string , cgoWrapper string ) * TypeMap {
18- return & TypeMap {
19- GoType : goType ,
20- CgoWrapper : cgoWrapper ,
21- }
22- }
23-
24- var structMemberTypeMap = map [string ]* TypeMap {
25- "unsigned int" : tm ("uint32" , "C.uint(%s)" ),
26- "float" : tm ("float32" , "C.float(%s)" ),
27- "int" : tm ("int32" , "C.int(%s)" ),
28- }
29-
3012func trimImGuiPrefix (id string ) string {
3113 // don't trim prefixes for implot's ImAxis - it conflicts with ImGuIAxis (from imgui_internal.h)
3214 if strings .HasPrefix (id , "ImAxis" ) {
@@ -141,113 +123,6 @@ import "unsafe"
141123
142124` , prefix ))
143125
144- argWrapperMap := map [string ]typeWrapper {
145- "char*" : constCharW ,
146- "const char*" : constCharW ,
147- "const char**" : charPtrPtrW ,
148- "const char* const[]" : charPtrPtrW ,
149- "unsigned char" : ucharW ,
150- "unsigned char**" : uCharPtrW ,
151- "size_t" : sizeTW ,
152- "size_t*" : sizeTPtrW ,
153- "float" : floatW ,
154- "float*" : floatPtrW ,
155- "const float*" : floatArrayW ,
156- "short" : shortW ,
157- "unsigned short" : ushortW ,
158- "ImU8" : u8W ,
159- "const ImU8*" : u8SliceW ,
160- "ImU16" : u16W ,
161- "const ImU16*" : u16SliceW ,
162- "ImU32" : u32W ,
163- "const ImU32*" : u32SliceW ,
164- "ImU64" : u64W ,
165- "const ImU64*" : uint64ArrayW ,
166- "ImS8" : s8W ,
167- "const ImS8*" : s8SliceW ,
168- "ImS16" : s16W ,
169- "const ImS16*" : s16SliceW ,
170- "ImS32" : s32W ,
171- "const ImS32*" : s32SliceW ,
172- "const ImS64*" : int64ArrayW ,
173- "int" : intW ,
174- "int*" : intPtrW ,
175- "unsigned int" : uintW ,
176- "unsigned int*" : uintPtrW ,
177- "double" : doubleW ,
178- "double*" : doublePtrW ,
179- "bool" : boolW ,
180- "bool*" : boolPtrW ,
181- "int[2]" : int2W ,
182- "int[3]" : int3W ,
183- "int[4]" : int4W ,
184- "float[2]" : float2W ,
185- "float[3]" : float3W ,
186- "float[4]" : float4W ,
187- "ImWchar" : imWcharW ,
188- "const ImWchar*" : imWcharPtrW ,
189- "ImGuiID" : imGuiIDW ,
190- "ImTextureID" : imTextureIDW ,
191- "ImDrawIdx" : imDrawIdxW ,
192- "ImGuiTableColumnIdx" : imTableColumnIdxW ,
193- "ImGuiTableDrawChannelIdx" : imTableDrawChannelIdxW ,
194- "void*" : voidPtrW ,
195- "const void*" : voidPtrW ,
196- "const ImVec2" : imVec2W ,
197- "const ImVec2*" : imVec2PtrW ,
198- "ImVec2" : imVec2W ,
199- "ImVec2*" : imVec2PtrW ,
200- "ImVec2[2]" : imVec22W ,
201- "const ImVec4" : imVec4W ,
202- "const ImVec4*" : imVec4PtrW ,
203- "ImVec4" : imVec4W ,
204- "ImVec4*" : imVec4PtrW ,
205- "ImColor*" : imColorPtrW ,
206- "ImRect" : imRectW ,
207- "ImRect*" : imRectPtrW ,
208- "ImPlotPoint" : imPlotPointW ,
209- "const ImPlotPoint" : imPlotPointW ,
210- "ImPlotPoint*" : imPlotPointPtrW ,
211- }
212-
213- returnWrapperMap := map [string ]returnWrapper {
214- "bool" : boolReturnW ,
215- "char*" : constCharReturnW ,
216- "const char*" : constCharReturnW ,
217- "const ImWchar*" : constWCharPtrReturnW ,
218- "ImWchar" : imWcharReturnW ,
219- "float" : floatReturnW ,
220- "double" : doubleReturnW ,
221- "int" : intReturnW ,
222- "unsigned int" : uintReturnW ,
223- "short" : intReturnW ,
224- "ImS8" : intReturnW ,
225- "ImS16" : intReturnW ,
226- "ImS32" : intReturnW ,
227- "ImU8" : uintReturnW ,
228- "ImU16" : uintReturnW ,
229- "ImU32" : u32ReturnW ,
230- "ImU64" : uint64ReturnW ,
231- "ImVec4" : imVec4ReturnW ,
232- "const ImVec4*" : imVec4PtrReturnW ,
233- "ImGuiID" : idReturnW ,
234- "ImTextureID" : textureIdReturnW ,
235- "ImVec2" : imVec2ReturnW ,
236- "ImColor" : imColorReturnW ,
237- "ImPlotPoint" : imPlotPointReturnW ,
238- "ImRect" : imRectReturnW ,
239- "ImGuiTableColumnIdx" : imTableColumnIdxReturnW ,
240- "ImGuiTableDrawChannelIdx" : imTableDrawChannelIdxReturnW ,
241- "void*" : voidPtrReturnW ,
242- "size_t" : doubleReturnW ,
243- }
244-
245- type argOutput struct {
246- ArgType string
247- ArgDef string
248- VarName string
249- }
250-
251126 isEnum := func (argType string ) bool {
252127 for _ , en := range enumNames {
253128 if argType == en {
@@ -299,7 +174,7 @@ import "unsafe"
299174 continue
300175 }
301176
302- if v , ok := argWrapperMap [ a .Type ]; ok {
177+ if v , err := argWrapper ( a .Type ); err == nil {
303178 argType , argDef , varName := v (a )
304179 if goEnumName := trimImGuiPrefix (argType ); isEnum (goEnumName ) {
305180 argType = goEnumName
@@ -359,19 +234,6 @@ import "unsafe"
359234 fmt .Printf ("generated: %s%s\n " , f .FuncName , f .Args )
360235 }
361236
362- // Generate function args
363- argStmtFunc := func () string {
364- var invokeStmt []string
365- for _ , aw := range argWrappers {
366- invokeStmt = append (invokeStmt , aw .VarName )
367- if len (aw .ArgDef ) > 0 {
368- sb .WriteString (fmt .Sprintf ("%s\n \n " , aw .ArgDef ))
369- }
370- }
371-
372- return strings .Join (invokeStmt , "," )
373- }
374-
375237 skipStructs := []string {
376238 "ImVec1" ,
377239 "ImVec2" ,
@@ -444,8 +306,8 @@ import "unsafe"
444306 // find out the return type
445307 outArg := f .ArgsT [0 ]
446308 outArgT := strings .TrimSuffix (outArg .Type , "*" )
447- returnWrapper , found := returnWrapperMap [ outArgT ]
448- if ! found {
309+ returnWrapper , err := getReturnTypeWrapperFunc ( outArgT )
310+ if err != nil {
449311 fmt .Printf ("Unknown return type \" %s\" in function %s\n " , f .Ret , f .FuncName )
450312 continue
451313 }
@@ -457,7 +319,7 @@ import "unsafe"
457319 // temporary out arg definition
458320 sb .WriteString (fmt .Sprintf ("%s := &%s{}\n " , outArg .Name , returnType ))
459321
460- argInvokeStmt := argStmtFunc ()
322+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
461323
462324 // C function call
463325 sb .WriteString (fmt .Sprintf ("C.%s(%s)\n " , f .FuncName , argInvokeStmt ))
@@ -478,27 +340,27 @@ import "unsafe"
478340
479341 sb .WriteString (fmt .Sprintf ("func (self %[1]s) %[2]s(%[3]s) {\n " , funcParts [0 ], funcName , strings .Join (args , "," )))
480342
481- argInvokeStmt := argStmtFunc ()
343+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
482344
483345 sb .WriteString (fmt .Sprintf ("C.%s(self.handle(), %s)\n " , f .FuncName , argInvokeStmt ))
484346 sb .WriteString ("}\n \n " )
485347 } else {
486348 sb .WriteString (funcSignatureFunc (f .FuncName , args , "" ))
487349
488- argInvokeStmt := argStmtFunc ()
350+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
489351
490352 sb .WriteString (fmt .Sprintf ("C.%s(%s)\n " , f .FuncName , argInvokeStmt ))
491353 sb .WriteString ("}\n \n " )
492354 }
493355
494356 convertedFuncCount += 1
495357 default :
496- if rf , ok := returnWrapperMap [ f .Ret ]; ok {
358+ if rf , err := getReturnTypeWrapperFunc ( f .Ret ); err == nil {
497359 returnType , returnStmt := rf ()
498360
499361 sb .WriteString (funcSignatureFunc (f .FuncName , args , returnType ))
500362
501- argInvokeStmt := argStmtFunc ()
363+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
502364
503365 sb .WriteString (fmt .Sprintf (returnStmt , fmt .Sprintf ("C.%s(%s)" , f .FuncName , argInvokeStmt )))
504366 sb .WriteString ("}\n \n " )
@@ -509,7 +371,7 @@ import "unsafe"
509371
510372 sb .WriteString (funcSignatureFunc (f .FuncName , args , returnType ))
511373
512- argInvokeStmt := argStmtFunc ()
374+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
513375
514376 sb .WriteString (fmt .Sprintf ("return %s(%s)" , returnType , fmt .Sprintf ("C.%s(%s)" , f .FuncName , argInvokeStmt )))
515377 sb .WriteString ("}\n \n " )
@@ -522,7 +384,7 @@ import "unsafe"
522384
523385 sb .WriteString (funcSignatureFunc (f .FuncName , args , pureReturnType ))
524386
525- argInvokeStmt := argStmtFunc ()
387+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
526388
527389 sb .WriteString (fmt .Sprintf ("return (%s)(unsafe.Pointer(%s))" , pureReturnType , fmt .Sprintf ("C.%s(%s)" , f .FuncName , argInvokeStmt )))
528390 sb .WriteString ("}\n \n " )
@@ -531,7 +393,7 @@ import "unsafe"
531393 } else if f .StructGetter && funk .ContainsString (structNames , f .Ret ) {
532394 sb .WriteString (funcSignatureFunc (f .FuncName , args , f .Ret ))
533395
534- argInvokeStmt := argStmtFunc ()
396+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
535397
536398 sb .WriteString (fmt .Sprintf ("return new%sFromC(C.%s(%s))" , f .Ret , f .FuncName , argInvokeStmt ))
537399 sb .WriteString ("}\n \n " )
@@ -559,7 +421,7 @@ import "unsafe"
559421
560422 sb .WriteString (fmt .Sprintf ("func %s(%s) %s {\n " , newFuncName , strings .Join (args , "," ), returnType ))
561423
562- argInvokeStmt := argStmtFunc ()
424+ argInvokeStmt := argStmtFunc (argWrappers , & sb )
563425
564426 sb .WriteString (fmt .Sprintf ("return (%s)(unsafe.Pointer(C.%s(%s)))" , returnType , f .FuncName , argInvokeStmt ))
565427
@@ -582,3 +444,22 @@ import "unsafe"
582444
583445 _ , _ = goFile .WriteString (sb .String ())
584446}
447+
448+ type argOutput struct {
449+ ArgType string
450+ ArgDef string
451+ VarName string
452+ }
453+
454+ // Generate function args
455+ func argStmtFunc (argWrappers []argOutput , sb * strings.Builder ) string {
456+ var invokeStmt []string
457+ for _ , aw := range argWrappers {
458+ invokeStmt = append (invokeStmt , aw .VarName )
459+ if len (aw .ArgDef ) > 0 {
460+ sb .WriteString (fmt .Sprintf ("%s\n \n " , aw .ArgDef ))
461+ }
462+ }
463+
464+ return strings .Join (invokeStmt , "," )
465+ }
0 commit comments