From 2ed65999ef6f3e1156427dd3efe04353ae657882 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 25 Jan 2022 17:30:46 +0800 Subject: ... --- src/common/CMakeLists.txt | 13 ++++++++++++ src/common/io/Resource.cpp | 20 ++++++++++++++++++ src/common/platform/Exception.cpp | 1 + src/common/platform/osx/Convert.cpp | 29 ++++++++++++++++++++++++++ src/common/platform/osx/Exception.cpp | 1 + src/common/platform/win/Exception.cpp | 39 +++++++++++++++++++++++++++++++++++ src/osx/CMakeLists.txt | 2 -- src/osx/Convert.cpp | 33 ----------------------------- src/osx/Exception.cpp | 1 - src/win/CMakeLists.txt | 3 --- src/win/Exception.cpp | 39 ----------------------------------- src/win/HeapDebug.cpp | 9 -------- 12 files changed, 103 insertions(+), 87 deletions(-) create mode 100644 src/common/io/Resource.cpp create mode 100644 src/common/platform/Exception.cpp create mode 100644 src/common/platform/osx/Convert.cpp create mode 100644 src/common/platform/osx/Exception.cpp create mode 100644 src/common/platform/win/Exception.cpp delete mode 100644 src/osx/Convert.cpp delete mode 100644 src/osx/Exception.cpp delete mode 100644 src/win/Exception.cpp delete mode 100644 src/win/HeapDebug.cpp (limited to 'src') diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index d8462abf..0316d9e6 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -7,7 +7,9 @@ add_library(cru_base SHARED String.cpp StringUtil.cpp io/Stream.cpp + io/Resource.cpp io/MemoryStream.cpp + platform/Exception.cpp ) target_compile_definitions(cru_base PRIVATE CRU_BASE_EXPORT_API) target_include_directories(cru_base PUBLIC ${CRU_INCLUDE_DIR}) @@ -20,9 +22,20 @@ if (UNIX) ) endif() +if (APPLE) + find_library(CORE_FOUNDATION CoreFoundation REQUIRED) + target_link_libraries(cru_base PUBLIC ${CORE_FOUNDATION}) + + target_sources(cru_base PRIVATE + platform/osx/Convert.cpp + platform/osx/Exception.cpp + ) +endif() + if (WIN32) target_sources(cru_base PRIVATE io/Win32FileStream.cpp + platform/win/Exception.cpp ) endif() diff --git a/src/common/io/Resource.cpp b/src/common/io/Resource.cpp new file mode 100644 index 00000000..e2ff8004 --- /dev/null +++ b/src/common/io/Resource.cpp @@ -0,0 +1,20 @@ +#include "cru/common/io/Resource.hpp" +#include "cru/common/Exception.hpp" + +#if defined(CRU_PLATFORM_OSX) +#include +#elif defined(CRU_PLATFORM_WINDOWS) +#endif + +namespace cru::io { +std::unique_ptr CreateStreamFromResourcePath(const String& path) { +#if defined(CRU_PLATFORM_OSX) + // CFBundleRef main_bundle = CFBundleGetMainBundle(); + throw Exception(u"Not implemented."); +#elif defined(CRU_PLATFORM_WINDOWS) + throw Exception(u"Not implemented."); +#else + throw Exception(u"Not implemented."); +#endif +} +} // namespace cru::io diff --git a/src/common/platform/Exception.cpp b/src/common/platform/Exception.cpp new file mode 100644 index 00000000..c13c8b1e --- /dev/null +++ b/src/common/platform/Exception.cpp @@ -0,0 +1 @@ +#include "cru/common/platform/Exception.hpp" diff --git a/src/common/platform/osx/Convert.cpp b/src/common/platform/osx/Convert.cpp new file mode 100644 index 00000000..e5105698 --- /dev/null +++ b/src/common/platform/osx/Convert.cpp @@ -0,0 +1,29 @@ +#include "cru/common/platform/osx/Convert.hpp" + +namespace cru::platform::osx { +CFStringRef Convert(const String& string) { + return CFStringCreateWithBytes( + nullptr, reinterpret_cast(string.data()), + string.size() * sizeof(std::uint16_t), kCFStringEncodingUTF16, false); +} + +String Convert(CFStringRef string) { + auto length = CFStringGetLength(string); + + String result; + + for (int i = 0; i < length; i++) { + result.AppendCodePoint(CFStringGetCharacterAtIndex(string, i)); + } + + return result; +} + +CFRange Convert(const Range& range) { + return CFRangeMake(range.position, range.count); +} + +Range Convert(const CFRange& range) { + return Range(range.location, range.length); +} +} // namespace cru::platform::osx diff --git a/src/common/platform/osx/Exception.cpp b/src/common/platform/osx/Exception.cpp new file mode 100644 index 00000000..b02fd458 --- /dev/null +++ b/src/common/platform/osx/Exception.cpp @@ -0,0 +1 @@ +#include "cru/common/platform//osx/Exception.hpp" diff --git a/src/common/platform/win/Exception.cpp b/src/common/platform/win/Exception.cpp new file mode 100644 index 00000000..34ae8955 --- /dev/null +++ b/src/common/platform/win/Exception.cpp @@ -0,0 +1,39 @@ +#include "cru/common/platform/win/Exception.hpp" +#include "cru/common/Format.hpp" + +#include + +namespace cru::platform::win { + +inline String HResultMakeMessage(HRESULT h_result, + std::optional message) { + if (message.has_value()) + return Format(u"HRESULT: {}. Message: {}", h_result, message->WinCStr()); + else + return Format(u"HRESULT: {}.", h_result); +} + +HResultError::HResultError(HRESULT h_result) + : PlatformException(HResultMakeMessage(h_result, std::nullopt)), + h_result_(h_result) {} + +HResultError::HResultError(HRESULT h_result, + std::string_view additional_message) + : PlatformException(HResultMakeMessage( + h_result, String::FromUtf8(additional_message.data(), + additional_message.size()))), + h_result_(h_result) {} + +inline String Win32MakeMessage(DWORD error_code, String message) { + return Format(u"Last error code: {}.\nMessage: {}\n", error_code, + message.WinCStr()); +} + +Win32Error::Win32Error(std::string_view message) + : Win32Error(::GetLastError(), message) {} + +Win32Error::Win32Error(DWORD error_code, std::string_view message) + : PlatformException(Win32MakeMessage( + error_code, String::FromUtf8(message.data(), message.size()))), + error_code_(error_code) {} +} // namespace cru::platform::win diff --git a/src/osx/CMakeLists.txt b/src/osx/CMakeLists.txt index 7f6a7538..ad591478 100644 --- a/src/osx/CMakeLists.txt +++ b/src/osx/CMakeLists.txt @@ -1,6 +1,4 @@ add_library(cru_osx_base SHARED - Convert.cpp - Exception.cpp Resource.cpp ) diff --git a/src/osx/Convert.cpp b/src/osx/Convert.cpp deleted file mode 100644 index 6e9692f2..00000000 --- a/src/osx/Convert.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "cru/osx/Convert.hpp" - -#include "cru/common/StringUtil.hpp" - -#include - -namespace cru::platform::osx { -CFStringRef Convert(const String& string) { - return CFStringCreateWithBytes( - nullptr, reinterpret_cast(string.data()), - string.size() * sizeof(std::uint16_t), kCFStringEncodingUTF16, false); -} - -String Convert(CFStringRef string) { - auto length = CFStringGetLength(string); - - String result; - - for (int i = 0; i < length; i++) { - result.AppendCodePoint(CFStringGetCharacterAtIndex(string, i)); - } - - return result; -} - -CFRange Convert(const Range& range) { - return CFRangeMake(range.position, range.count); -} - -Range Convert(const CFRange& range) { - return Range(range.location, range.length); -} -} // namespace cru::platform::osx diff --git a/src/osx/Exception.cpp b/src/osx/Exception.cpp deleted file mode 100644 index c7eef64b..00000000 --- a/src/osx/Exception.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "cru/osx/Exception.hpp" diff --git a/src/win/CMakeLists.txt b/src/win/CMakeLists.txt index fe2cd635..e190e9a0 100644 --- a/src/win/CMakeLists.txt +++ b/src/win/CMakeLists.txt @@ -1,9 +1,6 @@ add_library(cru_win_base STATIC DebugLogger.hpp StdOutLogger.hpp - - Exception.cpp - HeapDebug.cpp ) target_compile_definitions(cru_win_base PUBLIC UNICODE _UNICODE) # use unicode target_link_libraries(cru_win_base PUBLIC cru_base) diff --git a/src/win/Exception.cpp b/src/win/Exception.cpp deleted file mode 100644 index fb46f184..00000000 --- a/src/win/Exception.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "cru/win/Exception.hpp" -#include "cru/common/Format.hpp" - -#include - -namespace cru::platform::win { - -inline String HResultMakeMessage(HRESULT h_result, - std::optional message) { - if (message.has_value()) - return Format(u"HRESULT: {}. Message: {}", h_result, message->WinCStr()); - else - return Format(u"HRESULT: {}.", h_result); -} - -HResultError::HResultError(HRESULT h_result) - : PlatformException(HResultMakeMessage(h_result, std::nullopt)), - h_result_(h_result) {} - -HResultError::HResultError(HRESULT h_result, - std::string_view additional_message) - : PlatformException(HResultMakeMessage( - h_result, String::FromUtf8(additional_message.data(), - additional_message.size()))), - h_result_(h_result) {} - -inline String Win32MakeMessage(DWORD error_code, String message) { - return Format(u"Last error code: {}.\nMessage: {}\n", error_code, - message.WinCStr()); -} - -Win32Error::Win32Error(std::string_view message) - : Win32Error(::GetLastError(), message) {} - -Win32Error::Win32Error(DWORD error_code, std::string_view message) - : PlatformException(Win32MakeMessage( - error_code, String::FromUtf8(message.data(), message.size()))), - error_code_(error_code) {} -} // namespace cru::platform::win diff --git a/src/win/HeapDebug.cpp b/src/win/HeapDebug.cpp deleted file mode 100644 index b1b9fe1a..00000000 --- a/src/win/HeapDebug.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "cru/win/WinPreConfig.hpp" - -#include - -namespace cru::platform { -void SetupHeapDebug() { - _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); -} -} // namespace cru::platform -- cgit v1.2.3