Skip to content

Use C++20 source_location feature in exceptions #473

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions include/inexor/vulkan-renderer/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@

#include <vulkan/vulkan_core.h>

#include <source_location>
#include <stdexcept>
#include <string>

namespace inexor::vulkan_renderer {

/// @brief A custom base class for exceptions
/// A custom base class for exceptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A custom base class for exceptions
/// A custom base class for exceptions.

class InexorException : public std::runtime_error {
public:
// No need to define own constructors.
// There is no need to define our own constructors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// There is no need to define our own constructors
// There is no need to define our own constructors.

using std::runtime_error::runtime_error;
};

/// @brief InexorException for Vulkan specific things.
/// Custom exception class for Vulkan related exceptions
class VulkanException final : public InexorException {
public:
/// @param message The exception message.
/// @param result The VkResult value of the Vulkan API call which failed.
VulkanException(std::string message, VkResult result);
/// 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());
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

};

} // namespace inexor::vulkan_renderer
11 changes: 10 additions & 1 deletion src/vulkan-renderer/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@

namespace inexor::vulkan_renderer {

VulkanException::VulkanException(std::string message, const VkResult result)
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())
Comment on lines +7 to +20
Copy link
Member

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.

.append(")")) {}

} // namespace inexor::vulkan_renderer