From e73a8b76ce8e03ce5ef3960ea86ad9994b4e3d53 Mon Sep 17 00:00:00 2001 From: itqop Date: Wed, 15 May 2024 12:33:45 +0300 Subject: [PATCH] Add utils --- CMakeLists.txt | 2 +- include/tools.h | 187 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 include/tools.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 248d82e..323ffe9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,7 @@ add_executable("${CMAKE_PROJECT_NAME}") set_property(TARGET "${CMAKE_PROJECT_NAME}" PROPERTY CXX_STANDARD 17) target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC _CRT_SECURE_NO_WARNINGS) target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/") -target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES} "include/log.h") +target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES} "include/log.h" "include/tools.h") if(MSVC) set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup") diff --git a/include/tools.h b/include/tools.h new file mode 100644 index 0000000..117519a --- /dev/null +++ b/include/tools.h @@ -0,0 +1,187 @@ +#ifndef TOOLS_H_INCLUDE +#define TOOLS_H_INCLUDE + +#define INTERNAL_BUILD 1 //when internal build is true and an assertion is hit, you have the option to debug or ignore +//when internal build if false and an asertion is hit, the error is reported and the program +//closes + + +#include +#include +#include + +#ifdef _MSC_VER +#endif + +#define _CRT_SECURE_NO_WARNINGS + +#ifdef PLATFORM_WIN32 + +#include + +inline void assertFuncProduction( + const char* expression, + const char* file_name, + unsigned const line_number, + const char* comment = "---") +{ + + char c[1024] = {}; + + sprintf(c, + "Assertion failed\n\n" + "File:\n" + "%s\n\n" + "Line:\n" + "%u\n\n" + "Expresion:\n" + "%s\n\n" + "Comment:\n" + "%s" + "\n\nPlease report this error to the developer.", + file_name, + line_number, + expression, + comment + ); + + int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL + | MB_ICONHAND | MB_OK | MB_SETFOREGROUND); + + switch (action) + { + case IDOK: // Abort the program: + { + raise(SIGABRT); + + // We won't usually get here, but it's possible that a user-registered + // abort handler returns, so exit the program immediately. Note that + // even though we are "aborting," we do not call abort() because we do + // not want to invoke Watson (the user has already had an opportunity + // to debug the error and chose not to). + _exit(3); + } + default: + { + _exit(3); + } + } + +} + +inline void assertFuncInternal( + const char* expression, + const char* file_name, + unsigned const line_number, + const char* comment = "---") +{ + + char c[1024] = {}; + + sprintf(c, + "Assertion failed\n\n" + "File:\n" + "%s\n\n" + "Line:\n" + "%u\n\n" + "Expresion:\n" + "%s\n\n" + "Comment:\n" + "%s" + "\n\nPress retry to debug.", + file_name, + line_number, + expression, + comment + ); + + int const action = MessageBox(0, c, "Platform Layer", MB_TASKMODAL + | MB_ICONHAND | MB_ABORTRETRYIGNORE | MB_SETFOREGROUND); + + switch (action) + { + case IDABORT: // Abort the program: + { + raise(SIGABRT); + + // We won't usually get here, but it's possible that a user-registered + // abort handler returns, so exit the program immediately. Note that + // even though we are "aborting," we do not call abort() because we do + // not want to invoke Watson (the user has already had an opportunity + // to debug the error and chose not to). + _exit(3); + } + case IDRETRY: // Break into the debugger then return control to caller + { + __debugbreak(); + return; + } + case IDIGNORE: // Return control to caller + { + return; + } + default: // This should not happen; treat as fatal error: + { + abort(); + } + } + +} + +#if INTERNAL_BUILD == 1 + +#define permaAssert(expression) (void)( \ + (!!(expression)) || \ + (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ + ) + +#define permaAssertComment(expression, comment) (void)( \ + (!!(expression)) || \ + (assertFuncInternal(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \ + ) + +#else + +#define permaAssert(expression) (void)( \ + (!!(expression)) || \ + (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ + ) + +#define permaAssertComment(expression, comment) (void)( \ + (!!(expression)) || \ + (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__), comment), 1) \ + +) + +#endif + + + +#else //linux or others + +inline void assertFuncProduction( + const char* expression, + const char* file_name, + unsigned const line_number, + const char* comment = "---") +{ + + raise(SIGABRT); + +} + + +#define permaAssert(expression) (void)( \ + (!!(expression)) || \ + (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0) \ + ) + +#define permaAssertComment(expression, comment) (void)( \ + (!!(expression)) || \ + (assertFuncProduction(#expression, __FILE__, (unsigned)(__LINE__)), 0, comment) \ + ) + + +#endif + +#endif \ No newline at end of file