Porytiles
Loading...
Searching...
No Matches
panic.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fmt/format.h>
4
5#include <concepts>
6#include <format>
7#include <source_location>
8#include <string_view>
9#include <type_traits>
10
11namespace porytiles {
12
20 template <class T>
21 requires std::constructible_from<std::string_view, T>
22 StringViewSourceLoc(const T &msg, std::source_location loc = std::source_location::current()) noexcept
23 : msg_{msg}, loc_{loc} {}
24
25 std::string_view msg_;
26 std::source_location loc_;
27};
28
29[[noreturn]] void PanicImpl(const char *s) noexcept;
30
31[[noreturn]] inline void Panic(const StringViewSourceLoc &s) noexcept {
32 const auto msg = fmt::format("{}:{} panic: {}\n", s.loc_.file_name(), s.loc_.line(), s.msg_);
33 PanicImpl(msg.c_str());
34}
35
36inline void AssertOrPanic(const bool condition, const StringViewSourceLoc &s) {
37 if (!condition) {
38 const auto msg = fmt::format("{}:{} panic: {}\n", s.loc_.file_name(), s.loc_.line(), s.msg_);
39 PanicImpl(msg.c_str());
40 }
41}
42
43} // namespace porytiles
void Panic(const StringViewSourceLoc &s) noexcept
Definition panic.hpp:31
void AssertOrPanic(const bool condition, const StringViewSourceLoc &s)
Definition panic.hpp:36
void PanicImpl(const char *s) noexcept
Definition panic.cpp:8
A wrapper for std::string_view with a taggable std::source_location.
Definition panic.hpp:19
std::string_view msg_
Definition panic.hpp:25
StringViewSourceLoc(const T &msg, std::source_location loc=std::source_location::current()) noexcept
Definition panic.hpp:22
std::source_location loc_
Definition panic.hpp:26