diff options
author | crupest <crupest@outlook.com> | 2023-10-12 17:47:30 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2023-10-12 17:47:30 +0800 |
commit | 0ec90627298b8b7bdc74a3993ca58f1dde14dc26 (patch) | |
tree | 6bd6d25debf39bf969864b75599e824497b094a6 | |
parent | 9a077caa6d3f7eb8255ae68916dccac9b50a4333 (diff) | |
download | cru-0ec90627298b8b7bdc74a3993ca58f1dde14dc26.tar.gz cru-0ec90627298b8b7bdc74a3993ca58f1dde14dc26.tar.bz2 cru-0ec90627298b8b7bdc74a3993ca58f1dde14dc26.zip |
...
-rw-r--r-- | include/cru/common/Exception.h | 5 | ||||
-rw-r--r-- | include/cru/platform/Check.h | 15 | ||||
-rw-r--r-- | include/cru/platform/Exception.h | 33 | ||||
-rw-r--r-- | include/cru/platform/graphics/SvgGeometry.h | 13 | ||||
-rw-r--r-- | src/common/Exception.cpp | 9 | ||||
-rw-r--r-- | src/platform/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/platform/Exception.cpp | 44 | ||||
-rw-r--r-- | src/platform/graphics/SvgGeometry.cpp | 6 |
8 files changed, 109 insertions, 17 deletions
diff --git a/include/cru/common/Exception.h b/include/cru/common/Exception.h index d7e32a5a..06a10030 100644 --- a/include/cru/common/Exception.h +++ b/include/cru/common/Exception.h @@ -1,6 +1,8 @@ #pragma once #include "String.h" +#include <optional> + namespace cru { #ifdef _MSC_VER #pragma warning(disable : 4275) @@ -23,6 +25,9 @@ class CRU_BASE_API Exception : public std::exception { protected: void SetMessage(String message) { message_ = std::move(message); } + void AppendMessage(StringView additional_message); + void AppendMessage(std::optional<StringView> additional_message); + private: String message_; mutable std::string utf8_message_; diff --git a/include/cru/platform/Check.h b/include/cru/platform/Check.h index 8dc4faac..453521cc 100644 --- a/include/cru/platform/Check.h +++ b/include/cru/platform/Check.h @@ -2,7 +2,6 @@ #include "Exception.h" #include "Resource.h" -#include "cru/common/Format.h" #include "cru/common/String.h" #include <memory> @@ -15,10 +14,9 @@ TTarget* CheckPlatform(IPlatformResource* resource, if (resource == nullptr) return nullptr; const auto result = dynamic_cast<TTarget*>(resource); if (result == nullptr) { - throw UnsupportPlatformException(Format( - u"Try to convert resource to target platform failed. Platform id of " - "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); + throw PlatformNotMatchException( + resource->GetPlatformId(), target_platform, + u"Try to convert resource to target platform failed."); } return result; } @@ -31,10 +29,9 @@ std::shared_ptr<TTarget> CheckPlatform(const std::shared_ptr<TSource>& resource, "TSource must be a subclass of INativeResource."); const auto result = std::dynamic_pointer_cast<TTarget>(resource); if (result == nullptr) { - throw UnsupportPlatformException(Format( - u"Try to convert resource to target platform failed. Platform id of " - "resource to convert: {} . Target platform id: {} .", - resource->GetPlatformId(), target_platform)); + throw PlatformNotMatchException( + resource->GetPlatformId(), target_platform, + u"Try to convert resource to target platform failed."); } return result; } diff --git a/include/cru/platform/Exception.h b/include/cru/platform/Exception.h index 7d194d41..d6cda815 100644 --- a/include/cru/platform/Exception.h +++ b/include/cru/platform/Exception.h @@ -4,18 +4,26 @@ #include "cru/common/Exception.h" #include "cru/common/platform/Exception.h" +#include <optional> + namespace cru::platform { // This exception is thrown when a resource is used on another platform. // Of course, you can't mix resources of two different platform. // For example, Win32 Brush (may add in the future) with Direct Painter. -class CRU_PLATFORM_API UnsupportPlatformException : public Exception { +class CRU_PLATFORM_API PlatformNotMatchException : public PlatformException { public: - using Exception::Exception; // inherit constructors + PlatformNotMatchException( + String resource_platform, String target_platform, + std::optional<StringView> additional_message = std::nullopt); + + ~PlatformNotMatchException() override; - CRU_DEFAULT_COPY(UnsupportPlatformException) - CRU_DEFAULT_MOVE(UnsupportPlatformException) + String GetResourcePlatform() const; + String GetTargetPlatform() const; - CRU_DEFAULT_DESTRUCTOR(UnsupportPlatformException) + private: + String resource_platform_; + String target_platform_; }; // This exception is thrown when a resource has been disposed and not usable @@ -31,4 +39,19 @@ class CRU_PLATFORM_API ReuseException : public Exception { CRU_DEFAULT_DESTRUCTOR(ReuseException) }; + +class CRU_PLATFORM_API PlatformUnsupportedException : public PlatformException { + public: + PlatformUnsupportedException(String platform, String operation, + std::optional<StringView> additional_message); + + ~PlatformUnsupportedException() override; + + String GetPlatform() const; + String GetOperation() const; + + private: + String platform_; + String operation_; +}; } // namespace cru::platform diff --git a/include/cru/platform/graphics/SvgGeometry.h b/include/cru/platform/graphics/SvgGeometry.h index 281158f0..742a68ca 100644 --- a/include/cru/platform/graphics/SvgGeometry.h +++ b/include/cru/platform/graphics/SvgGeometry.h @@ -3,8 +3,15 @@ #include "Geometry.h" namespace cru::platform::graphics { -class CRU_PLATFORM_GRAPHICS_API SvgGeometryBuilder : public virtual IGeometryBuilder { +class CRU_PLATFORM_GRAPHICS_API SvgGeometryBuilder + : public Object, + public virtual IGeometryBuilder { + public: + SvgGeometryBuilder(); -}; -} + ~SvgGeometryBuilder() override; + private: + String current_; +}; +} // namespace cru::platform::graphics diff --git a/src/common/Exception.cpp b/src/common/Exception.cpp index b0053bac..e1e3e128 100644 --- a/src/common/Exception.cpp +++ b/src/common/Exception.cpp @@ -19,6 +19,15 @@ const char* Exception::what() const noexcept { return utf8_message_.c_str(); } +void Exception::AppendMessage(StringView additional_message) { + message_ += u" "; + message_ += additional_message; +} + +void Exception::AppendMessage(std::optional<StringView> additional_message) { + if (additional_message) AppendMessage(*additional_message); +} + ErrnoException::ErrnoException(const String& message) : ErrnoException(message, errno) {} diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index 3f85adbc..4779bb57 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -1,6 +1,7 @@ add_library(CruPlatformBase ForDllExport.cpp Color.cpp + Exception.cpp GraphicsBase.cpp ) target_link_libraries(CruPlatformBase PUBLIC CruBase) diff --git a/src/platform/Exception.cpp b/src/platform/Exception.cpp new file mode 100644 index 00000000..7aef3b7f --- /dev/null +++ b/src/platform/Exception.cpp @@ -0,0 +1,44 @@ +#include "cru/platform/Exception.h" +#include "cru/common/Format.h" + +#include <optional> + +namespace cru::platform { +PlatformNotMatchException::PlatformNotMatchException( + String resource_platform, String target_platform, + std::optional<StringView> additional_message) + : resource_platform_(std::move(resource_platform)), + target_platform_(std::move(target_platform)) { + SetMessage( + Format(u"Resource platform '{}' does not match target platform '{}'.", + resource_platform_, target_platform_)); + + AppendMessage(additional_message); +} + +PlatformNotMatchException::~PlatformNotMatchException() {} + +String PlatformNotMatchException::GetResourcePlatform() const { + return resource_platform_; +} + +String PlatformNotMatchException::GetTargetPlatform() const { + return target_platform_; +} + +PlatformUnsupportedException::PlatformUnsupportedException( + String platform, String operation, + std::optional<StringView> additional_message) + : platform_(std::move(platform)), operation_(std::move(operation)) { + SetMessage(Format(u"Operation '{}' is not supported on platform '{}'.", + operation_, platform_)); + AppendMessage(additional_message); +} + +PlatformUnsupportedException::~PlatformUnsupportedException() {} + +String PlatformUnsupportedException::GetPlatform() const { return platform_; } + +String PlatformUnsupportedException::GetOperation() const { return operation_; } + +} // namespace cru::platform diff --git a/src/platform/graphics/SvgGeometry.cpp b/src/platform/graphics/SvgGeometry.cpp index 149c4bb6..b757c520 100644 --- a/src/platform/graphics/SvgGeometry.cpp +++ b/src/platform/graphics/SvgGeometry.cpp @@ -1 +1,7 @@ #include "cru/platform/graphics/SvgGeometry.h" + +namespace cru::platform::graphics { +SvgGeometryBuilder::SvgGeometryBuilder() {} + +SvgGeometryBuilder::~SvgGeometryBuilder() {} +} // namespace cru::platform::graphics |