-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Error struct and error defining macro
- Loading branch information
1 parent
5bdcdc6
commit d9a8494
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <utility> | ||
|
||
struct Error { | ||
virtual ~Error() = default; | ||
Error() = default; | ||
Error(const Error &) = default; | ||
Error(Error &&) = default; | ||
auto operator=(const Error &) -> Error & = delete; | ||
auto operator=(Error &&) -> Error & = delete; | ||
|
||
explicit Error(std::string in_message) : message(std::move(in_message)) {} | ||
[[nodiscard]] auto get_error_type() const noexcept -> std::string { // NOLINT (bugprone-exception-escape) | ||
return "Error"; | ||
} | ||
|
||
[[nodiscard]] auto what() const noexcept -> std::string { return get_error_type() + ": " + message; } | ||
|
||
private: | ||
std::string message; | ||
}; | ||
|
||
#define DEFINE_ERROR_TYPE(name) /* NOLINT (cppcoreguidelines-macro-usage) */ \ | ||
struct name##Err : public Error { \ | ||
explicit name##Err(std::string in_message) : Error(std::move(in_message)) {} \ | ||
[[nodiscard]] auto get_error_type() const noexcept /* NOLINT (bugprone-exception-escape) */ \ | ||
-> std::string { \ | ||
return #name; \ | ||
} \ | ||
}; |