Skip to content

Commit

Permalink
Relay asserts to go
Browse files Browse the repository at this point in the history
  • Loading branch information
Wieku committed Feb 24, 2024
1 parent c56ea53 commit 2a037ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 36 deletions.
47 changes: 47 additions & 0 deletions assert.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
27 changes: 9 additions & 18 deletions cimgui/imgui/imconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
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)
27 changes: 9 additions & 18 deletions templates/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <stdlib.h>
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)

0 comments on commit 2a037ba

Please sign in to comment.