diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/cru/platform/Base.h | 78 | ||||
| -rw-r--r-- | include/cru/platform/Check.h | 36 | ||||
| -rw-r--r-- | include/cru/platform/Exception.h | 55 | ||||
| -rw-r--r-- | include/cru/platform/win/Exception.h | 1 |
4 files changed, 78 insertions, 92 deletions
diff --git a/include/cru/platform/Base.h b/include/cru/platform/Base.h index 9a8540f2..a31e8e08 100644 --- a/include/cru/platform/Base.h +++ b/include/cru/platform/Base.h @@ -1,9 +1,13 @@ #pragma once #include <cru/base/Base.h> +#include <cru/base/Exception.h> #include <string> +#include <optional> +#include <string_view> + #ifdef CRU_IS_DLL #ifdef CRU_PLATFORM_EXPORT_API #define CRU_PLATFORM_API __declspec(dllexport) @@ -20,4 +24,78 @@ struct CRU_PLATFORM_API IPlatformResource : virtual Interface { virtual std::string GetPlatformId() const = 0; virtual std::string GetDebugString(); }; + +// 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 PlatformNotMatchException : public PlatformException { + public: + PlatformNotMatchException( + std::string resource_platform, std::string target_platform, + std::optional<std::string_view> additional_message = std::nullopt); + + ~PlatformNotMatchException() override; + + std::string GetResourcePlatformUtf8() const { return resource_platform_; } + std::string GetTargetPlatformUtf8() const { return target_platform_; } + + private: + std::string resource_platform_; + std::string target_platform_; +}; + +// This exception is thrown when a resource has been disposed and not usable +// again. +// For example, calling Build twice on a GeometryBuilder::Build will lead to +// this exception. +class CRU_PLATFORM_API ReuseException : public Exception { + public: + using Exception::Exception; // inherit constructors + + CRU_DEFAULT_DESTRUCTOR(ReuseException) +}; + +class CRU_PLATFORM_API PlatformUnsupportedException : public PlatformException { + public: + PlatformUnsupportedException( + std::string platform, std::string operation, + std::optional<std::string_view> additional_message); + + ~PlatformUnsupportedException() override; + + std::string GetPlatformUtf8() const { return platform_; } + std::string GetOperationUtf8() const { return operation_; } + + private: + std::string platform_; + std::string operation_; +}; + +template <typename TTarget> +TTarget* CheckPlatform(IPlatformResource* resource, + std::string target_platform) { + if (resource == nullptr) return nullptr; + const auto result = dynamic_cast<TTarget*>(resource); + if (result == nullptr) { + throw PlatformNotMatchException( + resource->GetPlatformId(), target_platform, + "Try to convert resource to target platform failed."); + } + return result; +} + +template <typename TTarget, typename TSource> +std::shared_ptr<TTarget> CheckPlatform(const std::shared_ptr<TSource>& resource, + std::string target_platform) { + if (resource == nullptr) return nullptr; + static_assert(std::is_base_of_v<IPlatformResource, TSource>, + "TSource must be a subclass of IPlatformResource."); + const auto result = std::dynamic_pointer_cast<TTarget>(resource); + if (result == nullptr) { + throw PlatformNotMatchException( + resource->GetPlatformId(), target_platform, + "Try to convert resource to target platform failed."); + } + return result; +} } // namespace cru::platform diff --git a/include/cru/platform/Check.h b/include/cru/platform/Check.h deleted file mode 100644 index 5b6cd2be..00000000 --- a/include/cru/platform/Check.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once -#include "Base.h" -#include "Exception.h" - -#include <memory> -#include <type_traits> - -namespace cru::platform { -template <typename TTarget> -TTarget* CheckPlatform(IPlatformResource* resource, - std::string target_platform) { - if (resource == nullptr) return nullptr; - const auto result = dynamic_cast<TTarget*>(resource); - if (result == nullptr) { - throw PlatformNotMatchException( - resource->GetPlatformId(), target_platform, - "Try to convert resource to target platform failed."); - } - return result; -} - -template <typename TTarget, typename TSource> -std::shared_ptr<TTarget> CheckPlatform(const std::shared_ptr<TSource>& resource, - std::string target_platform) { - if (resource == nullptr) return nullptr; - static_assert(std::is_base_of_v<IPlatformResource, TSource>, - "TSource must be a subclass of IPlatformResource."); - const auto result = std::dynamic_pointer_cast<TTarget>(resource); - if (result == nullptr) { - throw PlatformNotMatchException( - resource->GetPlatformId(), target_platform, - "Try to convert resource to target platform failed."); - } - return result; -} -} // namespace cru::platform diff --git a/include/cru/platform/Exception.h b/include/cru/platform/Exception.h deleted file mode 100644 index f43162d1..00000000 --- a/include/cru/platform/Exception.h +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once -#include "Base.h" -#include "cru/base/Base.h" -#include "cru/base/Exception.h" - -#include <optional> -#include <string_view> - -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 PlatformNotMatchException : public PlatformException { - public: - PlatformNotMatchException( - std::string resource_platform, std::string target_platform, - std::optional<std::string_view> additional_message = std::nullopt); - - ~PlatformNotMatchException() override; - - std::string GetResourcePlatformUtf8() const { return resource_platform_; } - std::string GetTargetPlatformUtf8() const { return target_platform_; } - - private: - std::string resource_platform_; - std::string target_platform_; -}; - -// This exception is thrown when a resource has been disposed and not usable -// again. -// For example, calling Build twice on a GeometryBuilder::Build will lead to -// this exception. -class CRU_PLATFORM_API ReuseException : public Exception { - public: - using Exception::Exception; // inherit constructors - - CRU_DEFAULT_DESTRUCTOR(ReuseException) -}; - -class CRU_PLATFORM_API PlatformUnsupportedException : public PlatformException { - public: - PlatformUnsupportedException( - std::string platform, std::string operation, - std::optional<std::string_view> additional_message); - - ~PlatformUnsupportedException() override; - - std::string GetPlatformUtf8() const { return platform_; } - std::string GetOperationUtf8() const { return operation_; } - - private: - std::string platform_; - std::string operation_; -}; -} // namespace cru::platform diff --git a/include/cru/platform/win/Exception.h b/include/cru/platform/win/Exception.h index 353c2dab..e6c65016 100644 --- a/include/cru/platform/win/Exception.h +++ b/include/cru/platform/win/Exception.h @@ -1,3 +1,2 @@ #pragma once #include "cru/base/platform/win/Exception.h" -#include "cru/platform/Exception.h" |
