BLT/include/blt/std/assert.h

71 lines
2.3 KiB
C
Raw Normal View History

/*
* Created by Brett on 23/08/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_ASSERT_H
#define BLT_ASSERT_H
namespace blt
{
void printStacktrace(char** messages, int size, const char* path, int line);
void b_assert_failed(const char* expression, const char* path, int line);
void b_throw(const char* what, const char* path, int line);
2023-11-14 01:05:28 -05:00
#if defined(__GNUC__) || defined(__llvm__)
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))
#else
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#define BLT_ATTRIB_NO_INLINE __declspec(noinline)
#else
#define BLT_ATTRIB_NO_INLINE
#endif
#endif
template<typename T>
void BLT_ATTRIB_NO_INLINE black_box_ref(const T& val){
volatile void* hell;
hell = (void*)&val;
}
template<typename T>
void BLT_ATTRIB_NO_INLINE black_box(T val){
volatile void* hell2;
hell2 = (void*)&val;
}
template<typename T>
const T& BLT_ATTRIB_NO_INLINE black_box_ref_ret(const T& val){
volatile void* hell;
hell = (void*)&val;
return val;
}
template<typename T>
T BLT_ATTRIB_NO_INLINE black_box_ret(T val){
volatile void* hell2;
hell2 = (void*)&val;
return val;
}
}
// prints error with stack trace if assertion fails. Does not stop execution.
2023-09-07 00:21:02 -04:00
#define blt_assert(expr) do {static_cast<bool>(expr) ? void(0) : blt::b_assert_failed(#expr, __FILE__, __LINE__) } while (0)
// prints error with stack trace then exits with failure.
2023-10-15 17:48:10 -04:00
#define BLT_ASSERT(expr) do { \
2023-11-13 15:15:27 -05:00
if (!static_cast<bool>(expr)) { \
2023-10-15 17:48:10 -04:00
blt::b_assert_failed(#expr, __FILE__, __LINE__); \
std::exit(EXIT_FAILURE); \
} \
} while (0)
// prints as error but does not throw the exception.
2023-09-07 00:21:02 -04:00
#define blt_throw(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__);} while (0)
// prints as error with stack trace and throws the exception.
2023-09-07 00:21:02 -04:00
#define BLT_THROW(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__); throw throwable;} while(0)
#endif //BLT_ASSERT_H