Foray Library
rapid prototyping framework for crossplatform development of vulkan hardware ray tracing applications
Loading...
Searching...
No Matches
foray_exception.hpp
Go to the documentation of this file.
1#pragma once
2#include <exception>
3#if __clang__ && __clang_major__ <= 14
4#include <experimental/source_location>
5using source_location = std::experimental::source_location;
6#else
7#include <source_location>
8using source_location = std::source_location;
9#endif
10#include <spdlog/fmt/fmt.h>
11
12namespace foray {
13
15 class Exception : std::exception
16 {
17 public:
18 inline Exception() : mReason("") {}
19 inline explicit Exception(std::string_view reason) : mReason(reason) {}
20 template <typename... Args>
21 Exception(const char* const format, Args&&... args)
22 {
23 mReason = fmt::vformat(format, fmt::make_format_args(std::forward<Args>(args)...));
24 }
25
26 inline virtual const char* what() const noexcept override { return mReason.c_str(); }
27
33 [[ noreturn ]] static void Throw(std::string_view reason, const source_location location = source_location::current());
40 template <typename... Args>
41 [[ noreturn ]] inline static void Throw(const source_location location, const char* const format, Args&&... args)
42 {
43 std::string reason = fmt::vformat(format, fmt::make_format_args(std::forward<Args>(args)...));
44 Throw(reason, location);
45 }
46
47 protected:
48 std::string mReason = std::string("");
49 };
50
52 inline void Assert(bool condition, const source_location location = source_location::current())
53 {
54 if(!condition)
55 {
56 Exception::Throw("Assertion failed!", location);
57 }
58 }
59
61 inline void Assert(bool condition, std::string_view message, const source_location location = source_location::current())
62 {
63 if(!condition)
64 {
65 Exception::Throw(message, location);
66 }
67 }
68} // namespace foray
69
72#define FORAY_ASSERTFMT(val, fmt, ...) \
73 if(!(val)) \
74 { \
75 const source_location __foray_location = source_location::current(); \
76 foray::Exception::Throw(__foray_location, fmt, __VA_ARGS__); \
77 }
78
81#define FORAY_THROWFMT(fmt, ...) \
82 { \
83 const source_location __foray_location = source_location::current(); \
84 foray::Exception::Throw(__foray_location, fmt, __VA_ARGS__); \
85 }
An extension of std::exception providing support for formatted error messages. Exceptions should alwa...
Definition foray_exception.hpp:16
static void Throw(std::string_view reason, const source_location location=source_location::current())
Throws an exception.
static void Throw(const source_location location, const char *const format, Args &&... args)
Throws an exception.
Definition foray_exception.hpp:41
Exception(const char *const format, Args &&... args)
Definition foray_exception.hpp:21
Exception(std::string_view reason)
Definition foray_exception.hpp:19
Exception()
Definition foray_exception.hpp:18
std::string mReason
Definition foray_exception.hpp:48
virtual const char * what() const noexcept override
Definition foray_exception.hpp:26
std::source_location source_location
Definition foray_exception.hpp:8
Definition foray_api.hpp:19
void Assert(bool condition, const source_location location=source_location::current())
Asserts condition. Throws a generic error message if conditition is false.
Definition foray_exception.hpp:52