inexorgame / vulkan-renderer

A new 3D game engine for Linux and Windows using C++20 and Vulkan API 1.3, in very early but ongoing development
https://inexor.org
MIT License
777 stars 34 forks source link

Use C++20 source_location feature in exceptions #473

Open IAmNotHanni opened 2 years ago

IAmNotHanni commented 2 years ago

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());
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())
                          .append(")")) {}

Throwing an example exception from rendergraph:

throw VulkanException("I am throwing a new exception!", VK_ERROR_INCOMPATIBLE_DRIVER);

Results in:

terminate called after throwing an instance of 'inexor::vulkan_renderer::VulkanException' what(): I am throwing a new exception! (VK_ERROR_INCOMPATIBLE_DRIVER: The requested version of Vulkan is not supported by the driver or is otherwise incompatible for implementation-specific reasons) (file: /home/johannes/Inexor/exception_refactoring/vulkan-renderer/src/vulkan-renderer/render_graph.cpp, line: 85, column: 89, function name: void inexor::vulkan_renderer::RenderGraph::build_buffer(const inexor::vulkan_renderer::BufferResource&, inexor::vulkan_renderer::PhysicalBuffer&) const)

(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!

IAmNotHanni commented 2 years ago

Oh it looks like clang isn't ready for source_location yet.

BrettDong commented 1 year ago

Clang 15 now supports std::source_location.

IAmNotHanni commented 1 year ago

Note for myself: The exception class can be made [[nodiscard]]