LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
exceptions.hpp
1#pragma once
2
3#include <optional>
4#include <stdexcept>
5#include <source_location>
6#include <stacktrace>
7#include <string>
8#include <type_traits>
9#include <format>
10
11namespace LiteFX {
12
16 class Exception : public std::runtime_error {
17 private:
18 std::source_location m_location;
19 std::stacktrace m_trace;
20
21 protected:
28 explicit Exception(const std::string& message, const std::source_location& location, std::stacktrace trace) :
29 std::runtime_error(message), m_location(location), m_trace(std::move(trace)) { }
30
31 public:
32 Exception(const Exception&) = default;
33 Exception(Exception&&) noexcept = default;
34 ~Exception() noexcept override = default;
35
36 Exception& operator=(const Exception&) = default;
37 Exception& operator=(Exception&&) noexcept = default;
38
39 public:
44 const std::source_location& location() const noexcept {
45 return m_location;
46 }
47
52 const std::stacktrace& trace() const noexcept {
53 return m_trace;
54 }
55 };
56
61 private:
62 std::string m_argument;
63
64 public:
69 explicit InvalidArgumentException(std::string_view argument) :
70 Exception(std::format("Invalid argument provided: {}.", argument), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
71
77 explicit InvalidArgumentException(std::string_view argument, std::string_view message) :
78 Exception(std::format("Invalid argument provided: {}. {}", argument, message), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
79
86 template <typename ...TArgs>
87 explicit InvalidArgumentException(std::string_view argument, std::format_string<TArgs...> format, TArgs&&... args) :
88 Exception(std::format("Invalid argument provided: {}. {}", argument, std::format(format, std::forward<TArgs>(args)...)), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
89
92 ~InvalidArgumentException() noexcept override = default;
93
95 InvalidArgumentException& operator=(InvalidArgumentException&&) noexcept = default;
96
97 public:
102 const std::string& argument() const noexcept {
103 return m_argument;
104 }
105 };
106
111 private:
112 std::string m_argument;
113
114 public:
119 explicit ArgumentOutOfRangeException(std::string_view argument) :
120 Exception(std::format("Argument was out of range: {}.", argument), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
121
127 explicit ArgumentOutOfRangeException(std::string_view argument, std::string_view message) :
128 Exception(std::format("Argument was out of range: {}. {}", argument, message), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
129
138 //template <typename T>
139 //explicit ArgumentOutOfRangeException(std::string_view argument, std::pair<T, T> validRange, T value, std::string_view message) :
140 // Exception(std::format("Argument was out of range: {} (valid range is [{}, {}] but provided value was {}). {}", argument, validRange.first, validRange.second, value, message), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
141
148 template <typename ...TArgs>
149 explicit ArgumentOutOfRangeException(std::string_view argument, std::format_string<TArgs...> format, TArgs&&... args) :
150 Exception(std::format("Argument was out of range: {}. {}", argument, std::format(format, std::forward<TArgs>(args)...)), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
151
161 template <typename T, typename ...TArgs>
162 explicit ArgumentOutOfRangeException(std::string_view argument, std::pair<T, T> validRange, T value, std::format_string<TArgs...> format, TArgs&&... args) :
163 Exception(std::format("Argument was out of range: {} (valid range is [{}, {}) but actual value was {}). {}", argument, validRange.first, validRange.second, value, std::format(format, std::forward<TArgs>(args)...)), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
164
167 ~ArgumentOutOfRangeException() noexcept override = default;
168
171
172 public:
177 const std::string& argument() const noexcept {
178 return m_argument;
179 }
180 };
181
186 private:
187 std::string m_argument;
188
189 public:
194 explicit ArgumentNotInitializedException(std::string_view argument) :
195 Exception(std::format("Argument was not initialized: {}.", argument), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
196
202 explicit ArgumentNotInitializedException(std::string_view argument, std::string_view message) :
203 Exception(std::format("Argument was not initialized: {}. {}", argument, message), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
204
211 template <typename ...TArgs>
212 explicit ArgumentNotInitializedException(std::string_view argument, std::format_string<TArgs...> format, TArgs&&... args) :
213 Exception(std::format("Argument was not initialized: {}. {}", argument, std::format(format, std::forward<TArgs>(args)...)), std::source_location::current(), std::stacktrace::current()), m_argument(argument) { }
214
217 ~ArgumentNotInitializedException() noexcept override = default;
218
221
222 public:
227 const std::string& argument() const noexcept {
228 return m_argument;
229 }
230 };
231
236 public:
240 explicit RuntimeException() :
241 Exception("The operation could not be executed.", std::source_location::current(), std::stacktrace::current()) { }
242
247 explicit RuntimeException(std::string_view message) :
248 Exception(std::format("The operation could not be executed: {}", message), std::source_location::current(), std::stacktrace::current()) { }
249
255 template <typename ...TArgs>
256 explicit RuntimeException(std::format_string<TArgs...> format, TArgs&&... args) :
257 Exception(std::format("The operation could not be executed: {}", std::format(format, std::forward<TArgs>(args)...)), std::source_location::current(), std::stacktrace::current()) { }
258
260 RuntimeException(RuntimeException&&) noexcept = default;
261 ~RuntimeException() noexcept override = default;
262
263 RuntimeException& operator=(const RuntimeException&) = default;
264 RuntimeException& operator=(RuntimeException&&) noexcept = default;
265 };
266};
An exception that is thrown, if a provided non-optional argument was not initialized.
Definition exceptions.hpp:185
const std::string & argument() const noexcept
Gets the name of the argument that was not initialized.
Definition exceptions.hpp:227
ArgumentNotInitializedException(ArgumentNotInitializedException &&) noexcept=default
ArgumentNotInitializedException(std::string_view argument, std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition exceptions.hpp:212
ArgumentNotInitializedException(const ArgumentNotInitializedException &)=default
ArgumentNotInitializedException(std::string_view argument)
Initializes a new exception.
Definition exceptions.hpp:194
ArgumentNotInitializedException(std::string_view argument, std::string_view message)
Initializes a new exception.
Definition exceptions.hpp:202
An exception that is thrown, if a provided argument is not within the expected range.
Definition exceptions.hpp:110
ArgumentOutOfRangeException(const ArgumentOutOfRangeException &)=default
ArgumentOutOfRangeException(std::string_view argument, std::pair< T, T > validRange, T value, std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition exceptions.hpp:162
ArgumentOutOfRangeException(ArgumentOutOfRangeException &&) noexcept=default
ArgumentOutOfRangeException(std::string_view argument, std::string_view message)
Initializes a new exception.
Definition exceptions.hpp:127
ArgumentOutOfRangeException(std::string_view argument, std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition exceptions.hpp:149
const std::string & argument() const noexcept
Gets the name of the argument that was out of range.
Definition exceptions.hpp:177
ArgumentOutOfRangeException(std::string_view argument)
Initializes a new exception.
Definition exceptions.hpp:119
The base class for exceptions thrown by the SDK.
Definition exceptions.hpp:16
Exception(const std::string &message, const std::source_location &location, std::stacktrace trace)
Initializes the exception.
Definition exceptions.hpp:28
const std::stacktrace & trace() const noexcept
Gets the stack trace leading to the exception.
Definition exceptions.hpp:52
Exception(Exception &&) noexcept=default
Exception(const Exception &)=default
const std::source_location & location() const noexcept
Gets the source location that identifies where the exception has been thrown.
Definition exceptions.hpp:44
An exception that is thrown, if a provided argument is not valid.
Definition exceptions.hpp:60
InvalidArgumentException(std::string_view argument, std::string_view message)
Initializes a new exception.
Definition exceptions.hpp:77
const std::string & argument() const noexcept
Gets the name of the argument that was invalid.
Definition exceptions.hpp:102
InvalidArgumentException(InvalidArgumentException &&) noexcept=default
InvalidArgumentException(const InvalidArgumentException &)=default
InvalidArgumentException(std::string_view argument, std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition exceptions.hpp:87
InvalidArgumentException(std::string_view argument)
Initializes a new exception.
Definition exceptions.hpp:69
An exception that is thrown, if a requested operation could not be executed.
Definition exceptions.hpp:235
RuntimeException()
Initializes a new exception.
Definition exceptions.hpp:240
RuntimeException(std::string_view message)
Initializes a new exception.
Definition exceptions.hpp:247
RuntimeException(const RuntimeException &)=default
RuntimeException(std::format_string< TArgs... > format, TArgs &&... args)
Initializes a new exception.
Definition exceptions.hpp:256
RuntimeException(RuntimeException &&) noexcept=default
Definition app.hpp:6