Porytiles
Loading...
Searching...
No Matches
panic.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstdlib>
5#include <source_location>
6#include <string_view>
7
8#include "fmt/format.h"
9
10namespace porytiles2 {
11
32 template <class T>
33 requires std::constructible_from<std::string_view, T>
34 // NOLINTNEXTLINE
35 StringViewSourceLoc(const T &msg, const std::source_location loc = std::source_location::current()) noexcept
36 : msg_{msg}, loc_{loc}
37 {
38 }
39
40 std::string_view msg_;
41 std::source_location loc_;
42};
43
53[[noreturn]] inline void panic(const StringViewSourceLoc &s)
54{
55 const auto msg = fmt::format("{}:{} panic: {}\n", s.loc_.file_name(), s.loc_.line(), s.msg_);
56 std::fputs(msg.c_str(), stderr);
57 std::abort();
58}
59
70inline void assert_or_panic(const bool condition, const StringViewSourceLoc &s)
71{
72 if (!condition) {
73 const auto msg = fmt::format("{}:{} panic: {}\n", s.loc_.file_name(), s.loc_.line(), s.msg_);
74 std::fputs(msg.c_str(), stderr);
75 std::abort();
76 }
77}
78
79} // namespace porytiles2
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.hpp:53
void assert_or_panic(const bool condition, const StringViewSourceLoc &s)
Conditionally panics if the given condition is false.
Definition panic.hpp:70
A wrapper for std::string_view with a taggable std::source_location.
Definition panic.hpp:20
StringViewSourceLoc(const T &msg, const std::source_location loc=std::source_location::current()) noexcept
Constructs a StringViewSourceLoc with a message and optional source location.
Definition panic.hpp:35
std::source_location loc_
Definition panic.hpp:41