AuroraRuntime/Include/Aurora/Debug/SysAssertions.hpp
2021-09-06 11:58:08 +01:00

193 lines
5.0 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: SysAssertions.hpp
Date: 2021-7-14
Author: Reece
***/
//#pragma once <- AURORA_NO_ASSERTIONS may be redefined. do not cache
#if !defined(AURORA_NO_ASSERTIONS)
// defines SysAssert and SysAssertDbg
#if defined(DEBUG)
/// @private
template<typename ... T>
static inline void SysAssertFileEx(const char *file, int fileno, const char *func, bool tru, T... args)
{
if (tru)
{
return;
}
Aurora::Console::Logging::WriteLinef(
Aurora::Console::EAnsiColor::eBoldRed,
"Fatal",
"Expression address: {} {}:{}", func, file, fileno);
if constexpr (sizeof...(T) == 0)
{
SysPanic("That's all folks");
}
else
{
SysPanic(args...);
}
}
/**
A simple assertion with an optional message
*/
#define SysAssert(tru, ...) \
do \
{ \
SysAssertFileEx(__FILE__, __LINE__, __FUNCTION__, static_cast<bool>(tru), ## __VA_ARGS__); \
} while (0)
/**
A simple assertion with an optional message under debug targets
*/
#define SysAssertDbg SysAssert
#else
/// @private
template<typename ... T>
static inline void SysAssertEx(bool tru, T... args)
{
if (tru)
{
return;
}
if constexpr (sizeof...(T) == 0)
{
SysPanic("That's all folks");
}
else
{
SysPanic(args...);
}
}
/**
A simple assertion with an optional message
*/
#define SysAssert(tru, ...) \
do \
{ \
SysAssertEx(static_cast<bool>(tru), ## __VA_ARGS__); \
} while (0)
/**
A simple assertion with an optional message under debug targets
*/
#define SysAssertDbg(tru, ...) do {} while (0)
#endif
// defines SysAssertExp and SysAssertDbgExp
#if defined(DEBUG)
/// @private
template<typename ... T>
static inline void SysAssertFileExpEx(const char *file, int fileno, const char *func, const char *exp, bool tru, T... args)
{
if (tru)
{
return;
}
Aurora::Console::Logging::WriteLinef(
Aurora::Console::EAnsiColor::eBoldRed,
"Fatal",
"Expression address: {} {}:{}", func, file, fileno);
Aurora::Console::Logging::WriteLinef(
Aurora::Console::EAnsiColor::eBoldRed,
"Fatal",
"Expression failed: {}", exp);
if constexpr (sizeof...(T) == 0)
{
SysPanic("That's all folks");
}
else
{
SysPanic(args...);
}
}
/**
A simple assertion with a debug expresison and optional message debugging
*/
#define SysAssertExp(tru, ...) \
do \
{ \
SysAssertFileExpEx(__FILE__, __LINE__, __FUNCTION__, _AUKCON_STRINGIFY_X(tru), static_cast<bool>(tru), ## __VA_ARGS__); \
} while (0)
/**
A simple assertion with a debug expresison and optional message debugging under debug targets only
*/
#define SysAssertDbgExp SysAssertExp
#else
/// @private
template<typename ... T>
static inline void SysAssertExpEx(const char *exp, bool tru, T... args)
{
if (tru)
{
return;
}
Aurora::Console::Logging::WriteLinef(
Aurora::Console::EAnsiColor::eBoldRed,
"Fatal",
"Expression failed: {}", exp);
if constexpr (sizeof...(T) == 0)
{
SysPanic("That's all folks");
}
else
{
SysPanic(args...);
}
}
/**
A simple assertion with a debug expresison and optional message debugging
*/
#define SysAssertExp(tru, ...) \
do \
{ \
SysAssertExpEx(_AUKCON_STRINGIFY_X(tru), static_cast<bool>(tru), ## __VA_ARGS__); \
} while (0)
/**
A simple assertion with a debug expresison and optional message debugging under debug targets only
*/
#define SysAssertDbgExp(tru, ...) do {} while (0)
#endif
#endif