diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-04 22:17:27 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-04 22:17:27 +0800 |
| commit | ef6cff0f308d49326bbe0c3b557cb8ab6cca455b (patch) | |
| tree | 34914a6e76093d892ff10f8400d04c285eeab2c8 /src/base | |
| parent | 8bea03e0811588e741050b598b8123865b333999 (diff) | |
| download | cru-ef6cff0f308d49326bbe0c3b557cb8ab6cca455b.tar.gz cru-ef6cff0f308d49326bbe0c3b557cb8ab6cca455b.tar.bz2 cru-ef6cff0f308d49326bbe0c3b557cb8ab6cca455b.zip | |
Move base Exception.h to Base.h.
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/Base.cpp | 38 | ||||
| -rw-r--r-- | src/base/Buffer.cpp | 1 | ||||
| -rw-r--r-- | src/base/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/base/Exception.cpp | 42 | ||||
| -rw-r--r-- | src/base/PropertyTree.cpp | 1 | ||||
| -rw-r--r-- | src/base/StringUtil.cpp | 1 | ||||
| -rw-r--r-- | src/base/io/CFileStream.cpp | 1 | ||||
| -rw-r--r-- | src/base/io/MemoryStream.cpp | 1 | ||||
| -rw-r--r-- | src/base/io/Resource.cpp | 1 | ||||
| -rw-r--r-- | src/base/io/Stream.cpp | 1 | ||||
| -rw-r--r-- | src/base/platform/unix/EventLoop.cpp | 1 | ||||
| -rw-r--r-- | src/base/platform/unix/PosixSpawnSubProcess.cpp | 1 | ||||
| -rw-r--r-- | src/base/platform/unix/UnixFile.cpp | 1 | ||||
| -rw-r--r-- | src/base/platform/unix/UnixFileStream.cpp | 1 | ||||
| -rw-r--r-- | src/base/platform/win/StreamConvert.cpp | 1 |
15 files changed, 38 insertions, 55 deletions
diff --git a/src/base/Base.cpp b/src/base/Base.cpp index c6d9ac07..49b0c462 100644 --- a/src/base/Base.cpp +++ b/src/base/Base.cpp @@ -1,8 +1,46 @@ #include "cru/base/Base.h" +#include <cerrno> #include <exception> +#include <format> +#include <string_view> namespace cru { void UnreachableCode() { std::terminate(); } void NotImplemented() { std::terminate(); } + +static constexpr auto NO_MESSAGE = "No message."; + +Exception::Exception(std::string message, std::shared_ptr<std::exception> inner) + : message_(std::move(message)), inner_(std::move(inner)) {} + +Exception::~Exception() {} + +const char* Exception::what() const noexcept { return message_.c_str(); } + +void Exception::AppendMessage(const std::string& additional_message) { + AppendMessage(std::string_view(additional_message)); +} + +void Exception::AppendMessage(std::string_view additional_message) { + message_ += ' '; + message_ += additional_message; +} + +void Exception::AppendMessage( + std::optional<std::string_view> additional_message) { + if (additional_message) AppendMessage(*additional_message); +} + +ErrnoException::ErrnoException() : ErrnoException(NO_MESSAGE) {} + +ErrnoException::ErrnoException(int errno_code) + : ErrnoException(NO_MESSAGE, errno_code) {} + +ErrnoException::ErrnoException(std::string_view message) + : ErrnoException(message, errno) {} + +ErrnoException::ErrnoException(std::string_view message, int errno_code) + : Exception(std::format("{} Errno is {}.", message, errno_code)), + errno_code_(errno_code) {} } // namespace cru diff --git a/src/base/Buffer.cpp b/src/base/Buffer.cpp index 386e46cc..838c6f12 100644 --- a/src/base/Buffer.cpp +++ b/src/base/Buffer.cpp @@ -1,5 +1,4 @@ #include "cru/base/Buffer.h" -#include "cru/base/Exception.h" #include <cstring> diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 3cffb125..aa9a8a6b 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -1,7 +1,6 @@ add_library(CruBase Base.cpp Buffer.cpp - Exception.cpp PropertyTree.cpp StringUtil.cpp SubProcess.cpp diff --git a/src/base/Exception.cpp b/src/base/Exception.cpp deleted file mode 100644 index 5bfe27db..00000000 --- a/src/base/Exception.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "cru/base/Exception.h" - -#include <cerrno> -#include <format> -#include <string_view> - -constexpr auto NO_MESSAGE = "No message."; - -namespace cru { -Exception::Exception(std::string message, std::shared_ptr<std::exception> inner) - : message_(std::move(message)), inner_(std::move(inner)) {} - -Exception::~Exception() {} - -const char* Exception::what() const noexcept { return message_.c_str(); } - -void Exception::AppendMessage(const std::string& additional_message) { - AppendMessage(std::string_view(additional_message)); -} - -void Exception::AppendMessage(std::string_view additional_message) { - message_ += ' '; - message_ += additional_message; -} - -void Exception::AppendMessage( - std::optional<std::string_view> additional_message) { - if (additional_message) AppendMessage(*additional_message); -} - -ErrnoException::ErrnoException() : ErrnoException(NO_MESSAGE) {} - -ErrnoException::ErrnoException(int errno_code) - : ErrnoException(NO_MESSAGE, errno_code) {} - -ErrnoException::ErrnoException(std::string_view message) - : ErrnoException(message, errno) {} - -ErrnoException::ErrnoException(std::string_view message, int errno_code) - : Exception(std::format("{} Errno is {}.", message, errno_code)), - errno_code_(errno_code) {} -} // namespace cru diff --git a/src/base/PropertyTree.cpp b/src/base/PropertyTree.cpp index ea47dabc..fb640761 100644 --- a/src/base/PropertyTree.cpp +++ b/src/base/PropertyTree.cpp @@ -1,5 +1,4 @@ #include "cru/base/PropertyTree.h" -#include "cru/base/Exception.h" namespace cru { std::string PropertySubTreeRef::CombineKey(std::string_view left, diff --git a/src/base/StringUtil.cpp b/src/base/StringUtil.cpp index 5c8686dd..a5cc3122 100644 --- a/src/base/StringUtil.cpp +++ b/src/base/StringUtil.cpp @@ -1,6 +1,5 @@ #include "cru/base/StringUtil.h" #include "cru/base/Base.h" -#include "cru/base/Exception.h" #include <algorithm> #include <cctype> diff --git a/src/base/io/CFileStream.cpp b/src/base/io/CFileStream.cpp index f0d4790b..db477077 100644 --- a/src/base/io/CFileStream.cpp +++ b/src/base/io/CFileStream.cpp @@ -1,5 +1,4 @@ #include "cru/base/io/CFileStream.h" -#include "cru/base/Exception.h" #include "cru/base/io/Stream.h" #include <cstdio> diff --git a/src/base/io/MemoryStream.cpp b/src/base/io/MemoryStream.cpp index bba0e618..4d289197 100644 --- a/src/base/io/MemoryStream.cpp +++ b/src/base/io/MemoryStream.cpp @@ -1,7 +1,6 @@ #include "cru/base/io/MemoryStream.h" #include <cstring> -#include "cru/base/Exception.h" #include "cru/base/io/Stream.h" namespace cru::io { diff --git a/src/base/io/Resource.cpp b/src/base/io/Resource.cpp index 48045e59..df7277b6 100644 --- a/src/base/io/Resource.cpp +++ b/src/base/io/Resource.cpp @@ -1,6 +1,5 @@ #include "cru/base/io/Resource.h" #include "cru/base/Base.h" -#include "cru/base/Exception.h" #if defined(CRU_PLATFORM_OSX) #include <CoreFoundation/CoreFoundation.h> diff --git a/src/base/io/Stream.cpp b/src/base/io/Stream.cpp index e07f2899..56db547a 100644 --- a/src/base/io/Stream.cpp +++ b/src/base/io/Stream.cpp @@ -1,5 +1,4 @@ #include "cru/base/io/Stream.h" -#include "cru/base/Exception.h" #include <algorithm> #include <format> diff --git a/src/base/platform/unix/EventLoop.cpp b/src/base/platform/unix/EventLoop.cpp index 86c0deb9..200f6abb 100644 --- a/src/base/platform/unix/EventLoop.cpp +++ b/src/base/platform/unix/EventLoop.cpp @@ -1,5 +1,4 @@ #include "cru/base/platform/unix/EventLoop.h" -#include "cru/base/Exception.h" #include <poll.h> #include <algorithm> diff --git a/src/base/platform/unix/PosixSpawnSubProcess.cpp b/src/base/platform/unix/PosixSpawnSubProcess.cpp index 7362175f..c287d253 100644 --- a/src/base/platform/unix/PosixSpawnSubProcess.cpp +++ b/src/base/platform/unix/PosixSpawnSubProcess.cpp @@ -1,5 +1,4 @@ #include "cru/base/platform/unix/PosixSpawnSubProcess.h" -#include "cru/base/Exception.h" #include "cru/base/Guard.h" #include "cru/base/SubProcess.h" #include "cru/base/log/Logger.h" diff --git a/src/base/platform/unix/UnixFile.cpp b/src/base/platform/unix/UnixFile.cpp index 00ed3b9c..001b4fb9 100644 --- a/src/base/platform/unix/UnixFile.cpp +++ b/src/base/platform/unix/UnixFile.cpp @@ -1,5 +1,4 @@ #include "cru/base/platform/unix/UnixFile.h" -#include "cru/base/Exception.h" #include "cru/base/log/Logger.h" #include <fcntl.h> diff --git a/src/base/platform/unix/UnixFileStream.cpp b/src/base/platform/unix/UnixFileStream.cpp index 43ff2244..df1ddffa 100644 --- a/src/base/platform/unix/UnixFileStream.cpp +++ b/src/base/platform/unix/UnixFileStream.cpp @@ -1,5 +1,4 @@ #include "cru/base/platform/unix/UnixFileStream.h" -#include "cru/base/Exception.h" #include "cru/base/io/Stream.h" #include <fcntl.h> diff --git a/src/base/platform/win/StreamConvert.cpp b/src/base/platform/win/StreamConvert.cpp index f7a0537c..e0266765 100644 --- a/src/base/platform/win/StreamConvert.cpp +++ b/src/base/platform/win/StreamConvert.cpp @@ -1,7 +1,6 @@ #include "cru/base/platform/win/StreamConvert.h" #include "BrigdeComStream.h" #include "Win32FileStreamPrivate.h" -#include "cru/base/Exception.h" #include "cru/base/io/MemoryStream.h" #include "cru/base/io/OpenFileFlag.h" #include "cru/base/platform/win/ComAutoInit.h" |
