-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use C++20 source_location feature in exceptions #473
base: main
Are you sure you want to change the base?
Conversation
Oh it looks like clang isn't ready for source_location yet. |
class InexorException : public std::runtime_error { | ||
public: | ||
// No need to define own constructors. | ||
// There is no need to define our own constructors |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// There is no need to define our own constructors | |
// There is no need to define our own constructors. |
#include <stdexcept> | ||
#include <string> | ||
|
||
namespace inexor::vulkan_renderer { | ||
|
||
/// @brief A custom base class for exceptions | ||
/// A custom base class for exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// A custom base class for exceptions | |
/// A custom base class for exceptions. |
/// Default constructor | ||
/// Here we are using `C++20 source location feature <https://en.cppreference.com/w/cpp/utility/source_location>`__ | ||
/// @param message The exception message | ||
/// @param result The VkResult value of the Vulkan API call which failed | ||
/// @param location The source location | ||
VulkanException(std::string message, VkResult result, | ||
std::source_location location = std::source_location::current()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Default constructor | |
/// Here we are using `C++20 source location feature <https://en.cppreference.com/w/cpp/utility/source_location>`__ | |
/// @param message The exception message | |
/// @param result The VkResult value of the Vulkan API call which failed | |
/// @param location The source location | |
VulkanException(std::string message, VkResult result, | |
std::source_location location = std::source_location::current()); | |
/// Default constructor. | |
/// @param message The exception message. | |
/// @param result The VkResult value of the Vulkan API call which failed. | |
/// @param location The source location. | |
VulkanException(std::string message, VkResult result, | |
std::source_location location = std::source_location::current()); |
I would not especially point out the new C++20 feature.
VulkanException::VulkanException(std::string message, const VkResult result, const std::source_location location) | ||
: InexorException(message.append(" (") | ||
.append(vk_tools::as_string(result)) | ||
.append(": ") | ||
.append(vk_tools::result_to_description(result)) | ||
.append(") (") | ||
.append("file: ") | ||
.append(location.file_name()) | ||
.append(", line: ") | ||
.append(std::to_string(location.line())) | ||
.append(", column: ") | ||
.append(std::to_string(location.column())) | ||
.append(", function name: ") | ||
.append(location.function_name()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are using C++20 we could use std::format here.
Clang 15 now supports |
Note for myself: The exception class can be made |
Closes #468
Source locations!
C++20 gives us a very nice new feature, std::source_location!
VulkanException(std::string message, VkResult result, std::source_location location = std::source_location::current());
Throwing an example exception from rendergraph:
Results in:
(Note that I didn't add a catch block, so the exception will not be caught properly. However this was only a proof of concept)
This will make debugging a lot easier!