28#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
29 if (::superdex::OnAssertionFailure(file, line, conditionStr __VA_OPT__(, ) __VA_ARGS__)) { \
30 MOCHI_DEBUG_BREAK(); \
33#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
34 if (::superdex::OnAssertionFailure(file, line, conditionStr, __VA_ARGS__)) { \
35 MOCHI_DEBUG_BREAK(); \
44#define MOCHI_ASSERT_ON_FAILURE(file, line, conditionStr, ...) \
45 printf("%s(%d): Assertion Failure! Expected (%s)\n", file, line, conditionStr); \
46 printf("" __VA_ARGS__); \
50#error Expected MOCHI_ARCH_CPU or MOCHI_ARCH_GPU
51#define MOCHI_ASSERT_ON_FAILURE
56#define MOCHI_ASSERT_IMPL(condition, ...) \
58 } else MOCHI_UNLIKELY { \
59 MOCHI_ASSERT_ON_FAILURE(__FILE__, __LINE__, #condition __VA_OPT__(, ) __VA_ARGS__); \
62#define MOCHI_ASSERT_IMPL(condition, ...) \
64 } else MOCHI_UNLIKELY { \
65 MOCHI_ASSERT_ON_FAILURE(__FILE__, __LINE__, #condition, __VA_ARGS__); \
71namespace assert_impl {
74inline std::recursive_mutex& GetAssertMutexRef() {
75 static std::recursive_mutex s_assertMutex;
82inline int& GetAssertCallDepthRef() {
83 static int s_assertCallDepth = 0;
84 return s_assertCallDepth;
89DefaultAssertHandler(
char const* condition,
char const* message,
char const* file,
int line) {
92 "*****************************************************************************\n"
93 "MOCHI ASSERTION FAILURE:\n"
97 "*****************************************************************************\n",
110 bool const triggerDebugBreak =
true;
112 return triggerDebugBreak;
117template <
class DstPtrT,
class SrcT>
119 static_assert(std::is_pointer_v<DstPtrT>,
"Invalid cast. Expected a pointer type.");
120 MOCHI_ASSERT(
dynamic_cast<DstPtrT
>(ptr) ==
static_cast<DstPtrT
>(ptr),
"Invalid cast");
121 return static_cast<DstPtrT
>(ptr);
123template <
class DstRefT,
class SrcT>
125 static_assert(std::is_reference_v<DstRefT>,
"Invalid cast. Expected a reference type.");
127 dynamic_cast<std::remove_reference_t<DstRefT>*
>(&ref) ==
128 static_cast<std::remove_reference_t<DstRefT>*
>(&ref),
130 return static_cast<DstRefT
>(ref);
135inline OnAssertFn GetAssertionFailureCallback() {
137 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
138 auto& fn = assert_impl::GetOnAssertFnRef();
139 return fn ? fn :
OnAssertFn{assert_impl::DefaultAssertHandler};
141 return OnAssertFn{assert_impl::DefaultAssertHandler};
145inline void SetAssertionFailureCallback([[maybe_unused]]
OnAssertFn fn) {
147 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
148 assert_impl::GetOnAssertFnRef() = fn ? fn :
OnAssertFn{assert_impl::DefaultAssertHandler};
153MOCHI_NO_INLINE
inline bool
154OnAssertionFailure(
char const* file,
int line,
char const* condition,
char const* msg) {
156 std::scoped_lock lock(assert_impl::GetAssertMutexRef());
157 int& callDepth = assert_impl::GetAssertCallDepthRef();
158 if (callDepth != 0) {
163 bool shouldBreak = GetAssertionFailureCallback()(condition, msg, file, line);
167 return GetAssertionFailureCallback()(condition, msg, file, line);
172template <
typename... Args>
173MOCHI_NO_INLINE
bool OnAssertionFailure(
176 char const* condition,
179 return OnAssertionFailure(file, line, condition,
Format(fmt, args...).c_str());
183MOCHI_NO_INLINE
inline bool OnAssertionFailure(
char const* file,
int line,
char const* condition) {
184 return OnAssertionFailure(file, line, condition,
"");
#define MOCHI_ASSERT(condition_without_side_effects,...)
#define MOCHI_LOG_ERROR(...)
std::function< bool(char const *condition, char const *message, char const *file, int line)> OnAssertFn
Callback invoked when an assertion fails.