candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
errors.h
Go to the documentation of this file.
1#pragma once
2
3#if (__cpp_lib_unreachable >= 202202L)
4#include <utility>
5#endif
6
7#include <stdexcept>
8#include <string>
9#include <string_view>
10#include <format>
11#include <source_location>
12
13namespace candlewick {
14
15[[noreturn]] inline void unreachable() {
16#if (__cpp_lib_unreachable >= 202202L)
17 std::unreachable();
18#elif defined(_MSC_VER)
19 __assume(false);
20#else
21 __builtin_unreachable();
22#endif
23}
24
27struct RAIIException : std::runtime_error {
28 RAIIException(std::string_view msg, std::source_location location =
29 std::source_location::current());
30};
31
32namespace detail {
33
34 std::string _error_message_impl(std::string_view fname,
35 std::string_view fmtstr,
36 std::format_args args);
37
38 template <typename... Ts>
39 std::string error_message_format(std::string_view fname,
40 std::string_view _fmtstr, Ts &&...args) {
41 return _error_message_impl(fname.data(), _fmtstr,
42 std::make_format_args(args...));
43 }
44
45} // namespace detail
46
47// source_location default for last argument using ctad trick, see
48// https://stackoverflow.com/a/71082768
49template <typename... Ts> struct terminate_with_message {
51 std::string_view fmt, Ts &&...args,
52 std::source_location location = std::source_location::current()) {
53 throw std::runtime_error(detail::error_message_format(
54 location.function_name(), fmt, std::forward<Ts>(args)...));
55 }
56
57 [[noreturn]] terminate_with_message(std::source_location location,
58 std::string_view fmt, Ts &&...args)
59 : terminate_with_message(fmt, std::forward<Ts>(args)..., location) {}
60};
61
62template <typename... Ts>
63terminate_with_message(std::string_view, Ts &&...)
64 -> terminate_with_message<Ts...>;
65
66template <typename... Ts>
67terminate_with_message(std::source_location, std::string_view, Ts &&...)
68 -> terminate_with_message<Ts...>;
69
70[[noreturn]]
72 std::string_view msg,
73 std::source_location location = std::source_location::current());
74
75} // namespace candlewick
Definition errors.h:32
std::string error_message_format(std::string_view fname, std::string_view _fmtstr, Ts &&...args)
Definition errors.h:39
std::string _error_message_impl(std::string_view fname, std::string_view fmtstr, std::format_args args)
Definition Camera.h:8
void unreachable()
Definition errors.h:15
void unreachable_with_message(std::string_view msg, std::source_location location=std::source_location::current())
terminate_with_message(std::string_view, Ts &&...) -> terminate_with_message< Ts... >
RAIIException(std::string_view msg, std::source_location location=std::source_location::current())
terminate_with_message(std::string_view fmt, Ts &&...args, std::source_location location=std::source_location::current())
Definition errors.h:50
terminate_with_message(std::source_location location, std::string_view fmt, Ts &&...args)
Definition errors.h:57