From 6a44e6c95031179d29fca433851219eef402d71e Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 28 Apr 2023 16:42:01 +0200 Subject: add customization of assert implementation (#263) * add customization of assert implementation --- zencore/include/zencore/zencore.h | 63 ++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 27 deletions(-) (limited to 'zencore/include') diff --git a/zencore/include/zencore/zencore.h b/zencore/include/zencore/zencore.h index db089d9ea..5bcd77239 100644 --- a/zencore/include/zencore/zencore.h +++ b/zencore/include/zencore/zencore.h @@ -249,38 +249,47 @@ namespace zen AssertException(const char* Msg) : std::logic_error(Msg) {} }; + struct AssertImpl + { + static void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION ExecAssert + [[noreturn]] (const char* Filename, int LineNumber, const char* FunctionName, const char* Msg) + { + CurrentAssertImpl->OnAssert(Filename, LineNumber, FunctionName, Msg); + throw AssertException{Msg}; + } + + protected: + virtual void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION OnAssert(const char* Filename, + int LineNumber, + const char* FunctionName, + const char* Msg) + { + (void(Filename)); + (void(LineNumber)); + (void(FunctionName)); + (void(Msg)); + } + static AssertImpl DefaultAssertImpl; + static AssertImpl* CurrentAssertImpl; + }; + } // namespace zen -template -RetType ZEN_FORCENOINLINE ZEN_DEBUG_SECTION -DispatchAssert(InnerType&& Inner, ArgTypes const&... Args) -{ - return Inner(Args...); -} - -#define ZEN_ASSERT(x, ...) \ - do \ - { \ - if (x) [[unlikely]] \ - break; \ - struct Impl \ - { \ - static void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION ExecThrow [[noreturn]] () { throw ::zen::AssertException{#x}; } \ - }; \ - Impl::ExecThrow(); \ +#define ZEN_ASSERT(x, ...) \ + do \ + { \ + if (x) [[unlikely]] \ + break; \ + zen::AssertImpl::ExecAssert(__FILE__, __LINE__, __FUNCTION__, #x); \ } while (false) #ifndef NDEBUG -# define ZEN_ASSERT_SLOW(x, ...) \ - do \ - { \ - if (x) [[unlikely]] \ - break; \ - struct Impl \ - { \ - static void ZEN_FORCENOINLINE ZEN_DEBUG_SECTION ExecThrow [[noreturn]] () { throw ::zen::AssertException{#x}; } \ - }; \ - Impl::ExecThrow(); \ +# define ZEN_ASSERT_SLOW(x, ...) \ + do \ + { \ + if (x) [[unlikely]] \ + break; \ + zen::AssertImpl::ExecAssert(__FILE__, __LINE__, __FUNCTION__, #x); \ } while (false) #else # define ZEN_ASSERT_SLOW(x, ...) -- cgit v1.2.3