diff options
Diffstat (limited to 'include/cru/base/Base.h')
| -rw-r--r-- | include/cru/base/Base.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/include/cru/base/Base.h b/include/cru/base/Base.h index 6dfe995a..af4d38ff 100644 --- a/include/cru/base/Base.h +++ b/include/cru/base/Base.h @@ -1,8 +1,12 @@ #pragma once #include <cassert> #include <cstddef> +#include <exception> #include <functional> #include <memory> +#include <optional> +#include <string> +#include <string_view> #ifdef CRU_IS_DLL #ifdef CRU_BASE_EXPORT_API @@ -105,4 +109,53 @@ inline void hash_combine(std::size_t& s, const T& v) { #define CRU_DEFINE_CLASS_LOG_TAG(tag) \ private: \ constexpr static const char* kLogTag = tag; + +#ifdef _MSC_VER +#pragma warning(disable : 4275) +#endif +class CRU_BASE_API Exception : public std::exception { + public: + explicit Exception(std::string message = "", + std::shared_ptr<std::exception> inner = nullptr); + + ~Exception() override; + + public: + std::string GetUtf8Message() const { return this->message_; } + + std::exception* GetInner() const noexcept { return inner_.get(); } + + const char* what() const noexcept override; + + protected: + void SetMessage(std::string message) { message_ = std::move(message); } + void AppendMessage(const std::string& additional_message); + void AppendMessage(std::string_view additional_message); + void AppendMessage(std::optional<std::string_view> additional_message); + + private: + std::string message_; + std::shared_ptr<std::exception> inner_; +}; + +class CRU_BASE_API PlatformException : public Exception { + public: + using Exception::Exception; // inherit constructors +}; + +class ErrnoException : public Exception { + public: + ErrnoException(); + explicit ErrnoException(int error_code); + /** + * @brief will retrieve errno automatically. + */ + explicit ErrnoException(std::string_view message); + ErrnoException(std::string_view message, int errno_code); + + int GetErrnoCode() const { return errno_code_; } + + private: + int errno_code_; +}; } // namespace cru |
