candlewick 0.1.0
A renderer
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 <SDL3/SDL_log.h>
8#include <SDL3/SDL_assert.h>
9#include <stdexcept>
10#include <string>
11#include <string_view>
12#include <format>
13#include <source_location>
14
15namespace candlewick {
16
17[[noreturn]] inline void unreachable() {
18#if (__cpp_lib_unreachable >= 202202L)
19 std::unreachable();
20#elif defined(_MSC_VER)
21 __assume(false);
22#else
23 __builtin_unreachable();
24#endif
25}
26
29struct RAIIException : std::runtime_error {
30 RAIIException(std::string_view msg, std::source_location location =
31 std::source_location::current());
32};
33
34namespace detail {
35
36 std::string _error_message_impl(std::string_view fname,
37 std::string_view fmtstr,
38 std::format_args args);
39
40 template <typename... Ts>
41 std::string error_message_format(std::string_view fname,
42 std::string_view _fmtstr, Ts &&...args) {
43 return _error_message_impl(fname.data(), _fmtstr,
44 std::make_format_args(args...));
45 }
46
47} // namespace detail
48
49template <typename... Ts>
50[[noreturn]]
51void terminate_with_message(std::source_location location, std::string_view fmt,
52 Ts &&...args) {
53 throw std::runtime_error(detail::error_message_format(
54 location.function_name(), fmt, std::forward<Ts>(args)...));
55}
56
57template <typename... Ts>
58[[noreturn]]
59void terminate_with_message(std::string_view fmt, Ts &&...args) {
60 terminate_with_message(std::source_location::current(), fmt,
61 std::forward<Ts>(args)...);
62}
63
64[[noreturn]]
66 std::string_view msg,
67 std::source_location location = std::source_location::current()) {
68 SDL_LogError(
69 SDL_LOG_CATEGORY_APPLICATION, "%s",
70 detail::error_message_format(location.function_name(), "{:s}", msg)
71 .c_str());
73}
74
75} // namespace candlewick
Definition errors.h:34
std::string error_message_format(std::string_view fname, std::string_view _fmtstr, Ts &&...args)
Definition errors.h:41
std::string _error_message_impl(std::string_view fname, std::string_view fmtstr, std::format_args args)
Definition Camera.h:8
void terminate_with_message(std::source_location location, std::string_view fmt, Ts &&...args)
Definition errors.h:51
void unreachable()
Definition errors.h:17
void unreachable_with_message(std::string_view msg, std::source_location location=std::source_location::current())
Definition errors.h:65
RAIIException(std::string_view msg, std::source_location location=std::source_location::current())