Mjolnir Core
Core functionality of the Mjolnir API
exception.h
Go to the documentation of this file.
1 
7 
8 #pragma once
9 
10 #include <cstring>
11 #include <stdexcept>
12 
13 // === EXCEPTION MACROS ===============================================================================================
14 
15 
16 #if defined(_MSC_VER)
17 # define FUNCTION_SIG __FUNCSIG__
18 #elif defined(__GNUC__)
19 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
20 # define FUNCTION_SIG __PRETTY_FUNCTION__ // NOLINT(cppcoreguidelines-macro-usage)
21 #else
22 static_assert(false, "Incompatible compiler");
23 #endif
24 
25 
28 
36 #define THROW_EXCEPTION(exception_type, message) throw exception_type(FUNCTION_SIG, message)
37 
47 #define THROW_EXCEPTION_IF(condition, exception_type, message) \
48  if (condition) [[unlikely]] /* NOLINT(clang-diagnostic-unknown-attributes) */ \
49  THROW_EXCEPTION(exception_type, message)
50 
51 
53 
54 // ====================================================================================================================
55 
56 
57 namespace mjolnir
58 {
61 
64 class Exception : public std::runtime_error
65 {
66 public:
68 
69  Exception() = delete;
70  Exception(const Exception& other) = default;
71  Exception(Exception&& other) = default;
72  ~Exception() override = default;
73  auto operator=(const Exception& other) -> Exception& = default;
74  auto operator=(Exception&& other) -> Exception& = default;
75 
77 
85  Exception(const std::string& origin, const std::string& message);
86 };
87 
88 
91 class AllocationError : public Exception
92 {
94 };
95 
96 
99 class RuntimeError : public Exception
100 {
101  using Exception::Exception;
102 };
103 
104 
107 class ValueError : public Exception
108 {
109  using Exception::Exception;
110 };
111 
112 
114 } // namespace mjolnir
115 
116 
117 // ====================================================================================================================
118 
119 
120 namespace mjolnir
121 {
122 // --------------------------------------------------------------------------------------------------------------------
123 
124 inline Exception::Exception(const std::string& origin, const std::string& message)
125  : std::runtime_error{std::string("[") + origin + std::string("] ") + message}
126 {
127 }
128 
129 
130 } // namespace mjolnir
Exception class for failed memory allocations.
Definition: exception.h:92
Exception(const std::string &origin, const std::string &message)
Constructor.
Definition: exception.h:124
Base exception class that stores origin and message of an exception.
Definition: exception.h:65
Exception class for errors that do not fit into any other category.
Definition: exception.h:100
Exception class that should be throwm if an invalid value is detected.
Definition: exception.h:108