Some checks failed
Conan Packaging / Package Bin2CPP/1.0.0 (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clangd (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clangd (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clangd_asan (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clangd_asan (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clang (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clang (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clangd (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clangd (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clangd_asan (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clangd (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clangd_asan (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clang (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test RelWithDebInfo with ./ConanProfiles/clangd_asan (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clang (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clang (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clangd (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Build & Test Release with ./ConanProfiles/clangd_asan (Unity Build: ON) (push) Has been cancelled
Bin2CPP / Clang Format Checks (push) Has been cancelled
Bin2CPP / Build & Test Debug with ./ConanProfiles/clang (Unity Build: OFF) (push) Has been cancelled
Bin2CPP / Sonarqube (push) Has been cancelled
Reviewed-on: #2 Co-authored-by: Romain BOULLARD <romain.boullard@protonmail.com> Co-committed-by: Romain BOULLARD <romain.boullard@protonmail.com>
135 lines
8.4 KiB
C++
135 lines
8.4 KiB
C++
/*********************************************************************
|
|
* \file Assert.hpp
|
|
*
|
|
* \author Romain BOULLARD
|
|
* \date October 2025
|
|
*********************************************************************/
|
|
#ifndef BIN2CPP_ASSERT_HPP
|
|
#define BIN2CPP_ASSERT_HPP
|
|
#include <Log.hpp>
|
|
|
|
#if defined BIN2CPP_NOT_OPTIMIZED
|
|
|
|
#include <cpptrace/cpptrace.hpp>
|
|
|
|
#include <source_location>
|
|
#include <string>
|
|
|
|
#if defined BIN2CPP_LINUX
|
|
#include <csignal>
|
|
#endif
|
|
|
|
namespace Bin2CPP
|
|
{
|
|
class AssertHandler
|
|
{
|
|
public:
|
|
AssertHandler() = delete;
|
|
|
|
AssertHandler(const AssertHandler& p_handler) = delete;
|
|
AssertHandler(AssertHandler&& p_handler) = delete;
|
|
|
|
~AssertHandler() = delete;
|
|
|
|
/**
|
|
* Handle an assertion.
|
|
*
|
|
* \param p_location Location of the assertion.
|
|
* \param p_stacktrace The stack trace
|
|
* \param p_format Format string for the assertion message.
|
|
* \param p_args Arguments for the format string.
|
|
*/
|
|
template<typename... ARGS>
|
|
static void Handle(const std::source_location& p_location,
|
|
const std::string_view p_stacktrace,
|
|
std::format_string<ARGS...> p_format,
|
|
ARGS&&... p_args)
|
|
{
|
|
BIN2CPP_LOG_FATAL("Assert: {} (File:{}, Line:{}, Function:{})\n{}",
|
|
std::format(p_format, std::forward<ARGS>(p_args)...),
|
|
p_location.file_name(),
|
|
p_location.line(),
|
|
p_location.function_name(),
|
|
p_stacktrace);
|
|
}
|
|
|
|
AssertHandler& operator=(const AssertHandler& p_handler) = delete;
|
|
AssertHandler& operator=(AssertHandler&& p_handler) = delete;
|
|
};
|
|
} // namespace Bin2CPP
|
|
|
|
#if defined BIN2CPP_WINDOWS
|
|
#define BIN2CPP_BREAK \
|
|
do \
|
|
{ \
|
|
__debugbreak(); \
|
|
} while (false)
|
|
#elif defined BIN2CPP_LINUX
|
|
#define BIN2CPP_BREAK \
|
|
do \
|
|
{ \
|
|
std::raise(SIGTRAP); \
|
|
} while (false)
|
|
#endif
|
|
|
|
#define BIN2CPP_ASSERT(p_assert, p_message, ...) \
|
|
do \
|
|
{ \
|
|
constexpr std::source_location location = std::source_location::current(); \
|
|
if (!(p_assert)) [[unlikely]] \
|
|
{ \
|
|
Bin2CPP::AssertHandler::Handle(location, \
|
|
cpptrace::generate_trace().to_string(), \
|
|
p_message __VA_OPT__(, ) __VA_ARGS__); \
|
|
if (Bin2CPP::Singleton<Bin2CPP::Log>::HasInstance()) \
|
|
{ \
|
|
Bin2CPP::Singleton<Bin2CPP::Log>::Instance().Flush(); \
|
|
} \
|
|
BIN2CPP_BREAK; \
|
|
} \
|
|
} while (false)
|
|
|
|
#define BIN2CPP_SOFT_ASSERT(p_assert, p_message, ...) \
|
|
do \
|
|
{ \
|
|
constexpr std::source_location location = std::source_location::current(); \
|
|
if (!(p_assert)) [[unlikely]] \
|
|
{ \
|
|
Bin2CPP::AssertHandler::Handle(location, \
|
|
cpptrace::generate_trace().to_string(), \
|
|
p_message __VA_OPT__(, ) __VA_ARGS__); \
|
|
if (Bin2CPP::Singleton<Bin2CPP::Log>::HasInstance()) \
|
|
{ \
|
|
Bin2CPP::Singleton<Bin2CPP::Log>::Instance().Flush(); \
|
|
} \
|
|
BIN2CPP_BREAK; \
|
|
} \
|
|
} while (false)
|
|
|
|
#define BIN2CPP_CRITICAL_ASSERT(p_assert, p_message, ...) \
|
|
do \
|
|
{ \
|
|
constexpr std::source_location location = std::source_location::current(); \
|
|
if (!(p_assert)) [[unlikely]] \
|
|
{ \
|
|
Bin2CPP::AssertHandler::Handle(location, \
|
|
cpptrace::generate_trace().to_string(), \
|
|
p_message __VA_OPT__(, ) __VA_ARGS__); \
|
|
if (Bin2CPP::Singleton<Bin2CPP::Log>::HasInstance()) \
|
|
{ \
|
|
Bin2CPP::Singleton<Bin2CPP::Log>::Instance().Flush(); \
|
|
} \
|
|
BIN2CPP_BREAK; \
|
|
std::abort(); \
|
|
} \
|
|
} while (false)
|
|
|
|
#else
|
|
#define BIN2CPP_ASSERT(p_assert, p_message, ...)
|
|
|
|
#define BIN2CPP_SOFT_ASSERT(p_assert, p_message, ...)
|
|
|
|
#define BIN2CPP_CRITICAL_ASSERT(p_assert, p_message, ...)
|
|
#endif
|
|
#endif
|