aboutsummaryrefslogtreecommitdiff
path: root/src/common/platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/platform')
-rw-r--r--src/common/platform/Exception.cpp1
-rw-r--r--src/common/platform/osx/Convert.cpp29
-rw-r--r--src/common/platform/osx/Exception.cpp1
-rw-r--r--src/common/platform/win/Exception.cpp39
4 files changed, 70 insertions, 0 deletions
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<const UInt8*>(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 <optional>
+
+namespace cru::platform::win {
+
+inline String HResultMakeMessage(HRESULT h_result,
+ std::optional<String> 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