Implement fmt::ensure (assert-like with formatted message) + fmt::tie

Arguments should be provided via fmt::tie() due to some limitations.
Allow to specify simple message (non-formatting) for plain ::ensure().
Remove redundant function for narrow error.
This commit is contained in:
Nekotekina 2021-06-09 19:05:25 +03:00
parent 766cc2d42f
commit 696be6aacd
4 changed files with 76 additions and 17 deletions

View file

@ -802,6 +802,12 @@ struct const_str_t<umax>
const char* chars2;
};
const_str_t()
: size(0)
, chars(nullptr)
{
}
template <usz N>
constexpr const_str_t(const char8_t(&a)[N])
: size(N - 1)
@ -845,12 +851,11 @@ struct src_loc
namespace fmt
{
[[noreturn]] void raw_verify_error(const src_loc& loc);
[[noreturn]] void raw_narrow_error(const src_loc& loc);
[[noreturn]] void raw_verify_error(const src_loc& loc, const char8_t* msg);
}
template <typename T>
constexpr decltype(auto) ensure(T&& arg,
constexpr decltype(auto) ensure(T&& arg, const_str msg = const_str(),
u32 line = __builtin_LINE(),
u32 col = __builtin_COLUMN(),
const char* file = __builtin_FILE(),
@ -861,7 +866,7 @@ constexpr decltype(auto) ensure(T&& arg,
return std::forward<T>(arg);
}
fmt::raw_verify_error({line, col, file, func});
fmt::raw_verify_error({line, col, file, func}, msg);
}
// narrow() function details
@ -946,8 +951,7 @@ template <typename To = void, typename From, typename = decltype(static_cast<To>
// Narrow check
if (narrow_impl<From, To>::test(value)) [[unlikely]]
{
// Pack value as formatting argument
fmt::raw_narrow_error({line, col, file, func});
fmt::raw_verify_error({line, col, file, func}, u8"Narrowing error");
}
return static_cast<To>(value);