MCPcopy Create free account
hub / github.com/Autodesk/AutomaticComponentToolkit / buildDynamicCLoadTableCode

Function buildDynamicCLoadTableCode

Source/buildbindingccpp.go:397–473  ·  view source on GitHub ↗
(component ComponentDefinition, w LanguageWriter, NameSpace string, BaseName string, useStrictC bool)

Source from the content-addressed store, hash-verified

395}
396
397func buildDynamicCLoadTableCode(component ComponentDefinition, w LanguageWriter, NameSpace string, BaseName string, useStrictC bool) error {
398 global := component.Global
399
400 nullPtrStr := "nullptr"
401 if (useStrictC) {
402 nullPtrStr = "NULL"
403 }
404
405 w.Writeln("if (pWrapperTable == %s)", nullPtrStr)
406 w.Writeln(" return %s_ERROR_INVALIDPARAM;", strings.ToUpper(NameSpace))
407 w.Writeln("if (pLibraryFileName == %s)", nullPtrStr)
408 w.Writeln(" return %s_ERROR_INVALIDPARAM;", strings.ToUpper(NameSpace))
409
410 w.Writeln("")
411
412 w.Writeln("#ifdef _WIN32")
413 w.Writeln("// Convert filename to UTF16-string")
414 w.Writeln("int nLength = (int)strlen(pLibraryFileName);")
415 w.Writeln("int nBufferSize = nLength * 2 + 2;")
416 if (!useStrictC) {
417 w.Writeln("std::vector<wchar_t> wsLibraryFileName(nBufferSize);")
418 w.Writeln("int nResult = MultiByteToWideChar(CP_UTF8, 0, pLibraryFileName, nLength, &wsLibraryFileName[0], nBufferSize);")
419 w.Writeln("if (nResult == 0)")
420 w.Writeln(" return %s_ERROR_COULDNOTLOADLIBRARY;", strings.ToUpper(NameSpace))
421 w.Writeln("")
422 w.Writeln("HMODULE hLibrary = LoadLibraryW(wsLibraryFileName.data());")
423 } else {
424 w.Writeln("wchar_t* wsLibraryFileName = malloc(nBufferSize*sizeof(wchar_t));")
425 w.Writeln("memset(wsLibraryFileName, 0, nBufferSize*sizeof(wchar_t));")
426 w.Writeln("int nResult = MultiByteToWideChar(CP_UTF8, 0, pLibraryFileName, nLength, wsLibraryFileName, nBufferSize);")
427 w.Writeln("if (nResult == 0) {")
428 w.Writeln(" free(wsLibraryFileName);")
429 w.Writeln(" return %s_ERROR_COULDNOTLOADLIBRARY;", strings.ToUpper(NameSpace))
430 w.Writeln("}")
431 w.Writeln("")
432 w.Writeln("HMODULE hLibrary = LoadLibraryW(wsLibraryFileName);")
433 w.Writeln("free(wsLibraryFileName);")
434 }
435
436 w.Writeln("if (hLibrary == 0) ")
437 w.Writeln(" return %s_ERROR_COULDNOTLOADLIBRARY;", strings.ToUpper(NameSpace))
438 w.Writeln("#else // _WIN32")
439 w.Writeln("void* hLibrary = dlopen(pLibraryFileName, RTLD_LAZY);")
440 w.Writeln("if (hLibrary == 0) ")
441 w.Writeln(" return %s_ERROR_COULDNOTLOADLIBRARY;", strings.ToUpper(NameSpace))
442 w.Writeln("dlerror();")
443 w.Writeln("#endif // _WIN32")
444 w.Writeln("")
445
446 for i := 0; i < len(component.Classes); i++ {
447 class := component.Classes[i]
448 for j := 0; j < len(class.Methods); j++ {
449 method := class.Methods[j]
450 WriteLoadingOfMethod(class, method, w, NameSpace, useStrictC)
451 }
452 }
453
454 global = component.Global

Callers 2

buildCppHeaderFunction · 0.85

Calls 2

WriteLoadingOfMethodFunction · 0.85
WritelnMethod · 0.80

Tested by

no test coverage detected