diff --git a/assert.go b/assert.go new file mode 100644 index 000000000..ac4557adc --- /dev/null +++ b/assert.go @@ -0,0 +1,47 @@ +package imgui + +import "C" +import ( + "fmt" +) + +// AssertHandler is a handler for an assertion that happened in the native part of ImGui. +type AssertHandler func(expression string, file string, line int) + +// AssertionError is the standard error being thrown by the default handler. +type AssertionError struct { + Expression string + File string + Line int +} + +// Error returns the string representation. +func (err AssertionError) Error() string { + return fmt.Sprintf(`Assertion failed! +File: %s, Line %d + +Expression: %s +`, err.File, err.Line, err.Expression) +} + +var assertHandler AssertHandler = func(expression string, file string, line int) { + panic(AssertionError{ + Expression: expression, + File: file, + Line: line, + }) +} + +// SetAssertHandler registers a handler function for all future assertions. +// Setting nil will disable special handling. +// The default handler panics. +func SetAssertHandler(handler AssertHandler) { + assertHandler = handler +} + +//export goImguiAssertHandler +func goImguiAssertHandler(expression *C.char, file *C.char, line C.int) { + if assertHandler != nil { + assertHandler(C.GoString(expression), C.GoString(file), int(line)) + } +} diff --git a/cimgui/imgui/imconfig.h b/cimgui/imgui/imconfig.h index 62d1c8832..2c6d87b0f 100644 --- a/cimgui/imgui/imconfig.h +++ b/cimgui/imgui/imconfig.h @@ -135,22 +135,13 @@ namespace ImGui * * cimgui-go: https://github.com/AllenDang/cimgui-go * From: templates/assert.h - * - * TODO: - * - tbh we can return data about an error back to GO and let it panic from GO. - * - figure out how to convert cond into an human-readable expression (like in default assert func) */ -#define IM_ASSERT(_EXPR) GoFriendlyAssert(_EXPR, __FILE__, __LINE__) -#include -#include -static void GoFriendlyAssert(bool cond/*const char *expr*/, const char *file, int line) { - if (cond) { - return; - } - - fprintf(stdout, "File: %s, Line: %d\n", file, line); - - // Instead of throwing an exception, we're using exit(1) to terminate the program. - exit(1); -} - +extern "C" void goImguiAssertHandler(char const *expression, char const *file, int line); +#define IM_ASSERT(_EXPR) \ + do \ + { \ + if ((_EXPR) == 0) \ + { \ + goImguiAssertHandler(#_EXPR, __FILE__, __LINE__); \ + } \ + } while (false) diff --git a/templates/assert.h b/templates/assert.h index 021e506cf..89bed32e5 100644 --- a/templates/assert.h +++ b/templates/assert.h @@ -4,22 +4,13 @@ * * cimgui-go: https://github.com/AllenDang/cimgui-go * From: templates/assert.h - * - * TODO: - * - tbh we can return data about an error back to GO and let it panic from GO. - * - figure out how to convert cond into an human-readable expression (like in default assert func) */ -#define IM_ASSERT(_EXPR) GoFriendlyAssert(_EXPR, __FILE__, __LINE__) -#include -#include -static void GoFriendlyAssert(bool cond/*const char *expr*/, const char *file, int line) { - if (cond) { - return; - } - - fprintf(stdout, "File: %s, Line: %d\n", file, line); - - // Instead of throwing an exception, we're using exit(1) to terminate the program. - exit(1); -} - +extern "C" void goImguiAssertHandler(char const *expression, char const *file, int line); +#define IM_ASSERT(_EXPR) \ + do \ + { \ + if ((_EXPR) == 0) \ + { \ + goImguiAssertHandler(#_EXPR, __FILE__, __LINE__); \ + } \ + } while (false)