light/modules/debug/public/assertions.hpp

37 lines
776 B
C++
Raw Normal View History

#pragma once
#include <format>
#include <logger/logger.hpp>
#include <source_location>
2025-07-11 00:05:48 +03:30
namespace lt {
2025-07-31 08:42:42 +03:30
template<typename Expression_T, typename... Args_T>
struct ensure
{
2025-07-31 08:42:42 +03:30
ensure(
Expression_T expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location = std::source_location::current()
)
{
2025-07-31 08:42:42 +03:30
if (!static_cast<bool>(expression))
{
throw std::runtime_error { std::format(
"exception: {}\nlocation: {}:{}",
std::format(fmt, std::forward<Args_T>(args)...),
location.file_name(),
location.line()
) };
}
}
};
2025-07-31 08:42:42 +03:30
template<typename Expression_T, typename... Args_T>
ensure(Expression_T, std::format_string<Args_T...>, Args_T &&...)
-> ensure<Expression_T, Args_T...>;
2025-07-11 00:05:48 +03:30
} // namespace lt