refactor: convert lt_assert from macro to variadic template function

This commit is contained in:
light7734 2025-07-11 02:09:02 +03:30
parent ae336e3bba
commit e1fdeb2692
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
3 changed files with 27 additions and 9 deletions

View file

@ -12,13 +12,25 @@ struct FailedAssertion: std::exception
}
};
#define lt_assert(x, ...) \
{ \
if (!(x)) \
{ \
log_crt(__VA_ARGS__); \
throw ::lt::FailedAssertion(__FILE__, __LINE__); \
} \
template<typename Expression_T, typename... Args>
constexpr void lt_assert(Expression_T &&expression, std::format_string<Args...> fmt, Args &&...args)
{
if (!static_cast<bool>(expression))
{
Logger::log(LogLvl::critical, fmt, std::forward<Args>(args)...);
throw ::lt::FailedAssertion(__FILE__, __LINE__);
}
}
template<typename Expression_T>
constexpr void lt_assert(Expression_T &&expression, const char *message)
{
if (!static_cast<bool>(expression))
{
Logger::log(LogLvl::critical, message);
throw ::lt::FailedAssertion(__FILE__, __LINE__);
}
}
} // namespace lt

View file

@ -12,8 +12,8 @@ try
application = lt::create_application();
lt_assert(application, "Failed to create application");
lt_assert(application->sanity_check(), "Failed to verify the sanity of the application");
lt::lt_assert(application, "Failed to create application");
lt::lt_assert(application->sanity_check(), "Failed to verify the sanity of the application");
application->game_loop();

View file

@ -53,6 +53,12 @@ public:
);
}
void static log(LogLvl lvl, const char *message)
{
instance().spd_logger->log((spdlog::level::level_enum)lvl, message);
}
private:
Logger();