diff options
author | Yuqian Yang <crupest@crupest.life> | 2025-09-08 01:46:00 +0800 |
---|---|---|
committer | Yuqian Yang <crupest@crupest.life> | 2025-09-08 21:34:37 +0800 |
commit | 593b658eb1491d4b3103971aba6592aff2765f0e (patch) | |
tree | 526bc88c6a895c215015926c90ff38c106a94604 | |
parent | df550874cd546a85074edc35bebeb3cd0530622b (diff) | |
download | cru-593b658eb1491d4b3103971aba6592aff2765f0e.tar.gz cru-593b658eb1491d4b3103971aba6592aff2765f0e.tar.bz2 cru-593b658eb1491d4b3103971aba6592aff2765f0e.zip |
Fix some compile errors on Windows.
-rw-r--r-- | include/cru/base/String.h | 4 | ||||
-rw-r--r-- | include/cru/base/StringToNumberConverter.h | 3 | ||||
-rw-r--r-- | include/cru/base/platform/win/DebugLogTarget.h | 15 | ||||
-rw-r--r-- | include/cru/base/platform/win/Exception.h | 23 | ||||
-rw-r--r-- | include/cru/base/platform/win/Win32FileStream.h | 27 | ||||
-rw-r--r-- | src/base/SubProcess.cpp | 13 | ||||
-rw-r--r-- | src/base/io/Stream.cpp | 4 | ||||
-rw-r--r-- | src/base/log/StdioLogTarget.cpp | 4 | ||||
-rw-r--r-- | src/base/platform/win/DebugLogTarget.cpp | 6 | ||||
-rw-r--r-- | src/base/platform/win/Exception.cpp | 6 | ||||
-rw-r--r-- | src/base/platform/win/Win32FileStream.cpp | 37 | ||||
-rw-r--r-- | src/platform/gui/win/Window.cpp | 7 | ||||
-rw-r--r-- | src/ui/render/ScrollBar.cpp | 1 |
13 files changed, 54 insertions, 96 deletions
diff --git a/include/cru/base/String.h b/include/cru/base/String.h index 21a3db51..e58f21d3 100644 --- a/include/cru/base/String.h +++ b/include/cru/base/String.h @@ -257,8 +257,8 @@ class CRU_BASE_API String { Index size_ = 0; // not including trailing '\0' Index capacity_ = 0; // always 1 smaller than real buffer size }; - -std::ostream& CRU_BASE_API operator<<(std::ostream& os, const String& value); +CRU_BASE_API +std::ostream& operator<<(std::ostream& os, const String& value); class CRU_BASE_API StringView { public: diff --git a/include/cru/base/StringToNumberConverter.h b/include/cru/base/StringToNumberConverter.h index 758b26c8..051f44d0 100644 --- a/include/cru/base/StringToNumberConverter.h +++ b/include/cru/base/StringToNumberConverter.h @@ -56,8 +56,7 @@ inline bool CRU_BASE_API operator!=(const StringToIntegerResult& left, return !(left == right); } -inline std::ostream& CRU_BASE_API -operator<<(std::ostream& stream, const StringToIntegerResult& result) { +CRU_BASE_API inline std::ostream& operator<<(std::ostream& stream, const StringToIntegerResult& result) { return stream << "StringToIntegerConverterImplResult(" << (result.negate ? "-" : "") << result.value << ")"; } diff --git a/include/cru/base/platform/win/DebugLogTarget.h b/include/cru/base/platform/win/DebugLogTarget.h index 5f000d94..cabfa7e5 100644 --- a/include/cru/base/platform/win/DebugLogTarget.h +++ b/include/cru/base/platform/win/DebugLogTarget.h @@ -1,6 +1,8 @@ #pragma once -#ifdef CRU_PLATFORM_WINDOWS +#ifndef _WIN32 +#error "This file can only be used on Windows." +#endif #include "WinPreConfig.h" @@ -10,15 +12,6 @@ namespace cru::platform::win { class CRU_BASE_API WinDebugLogTarget : public ::cru::log::ILogTarget { public: - WinDebugLogTarget() = default; - - CRU_DELETE_COPY(WinDebugLogTarget) - CRU_DELETE_MOVE(WinDebugLogTarget) - - ~WinDebugLogTarget() = default; - - void Write(::cru::log::LogLevel level, StringView s) override; + void Write(::cru::log::LogLevel level, std::string s) override; }; } // namespace cru::platform::win - -#endif diff --git a/include/cru/base/platform/win/Exception.h b/include/cru/base/platform/win/Exception.h index 04bdd32c..12f3d108 100644 --- a/include/cru/base/platform/win/Exception.h +++ b/include/cru/base/platform/win/Exception.h @@ -1,25 +1,21 @@ #pragma once -#ifndef CRU_PLATFORM_WINDOWS + +#ifndef _WIN32 #error "This file can only be used on Windows." #endif #include "WinPreConfig.h" -#include "../Exception.h" +#include "../../Exception.h" #include <stdexcept> #include <string_view> namespace cru::platform::win { -class CRU_BASE_API HResultError : public platform::PlatformException { +class CRU_BASE_API HResultError : public Exception { public: explicit HResultError(HRESULT h_result); - explicit HResultError(HRESULT h_result, std::string_view message); - - CRU_DEFAULT_COPY(HResultError) - CRU_DEFAULT_MOVE(HResultError) - - ~HResultError() override = default; + HResultError(HRESULT h_result, std::string_view message); HRESULT GetHResult() const { return h_result_; } @@ -35,23 +31,16 @@ inline void ThrowIfFailed(const HRESULT h_result, std::string_view message) { if (FAILED(h_result)) throw HResultError(h_result, message); } -class CRU_BASE_API Win32Error : public platform::PlatformException { +class CRU_BASE_API Win32Error : public Exception { public: // ::GetLastError is automatically called to get the error code. // The same as Win32Error(::GetLastError(), message) explicit Win32Error(String message); Win32Error(DWORD error_code, String message); - CRU_DEFAULT_COPY(Win32Error) - CRU_DEFAULT_MOVE(Win32Error) - - ~Win32Error() override = default; - DWORD GetErrorCode() const { return error_code_; } private: DWORD error_code_; }; } // namespace cru::platform::win - -#endif diff --git a/include/cru/base/platform/win/Win32FileStream.h b/include/cru/base/platform/win/Win32FileStream.h index 961076de..61f1a33d 100644 --- a/include/cru/base/platform/win/Win32FileStream.h +++ b/include/cru/base/platform/win/Win32FileStream.h @@ -14,38 +14,27 @@ class Win32FileStreamPrivate; class CRU_BASE_API Win32FileStream : public io::Stream { public: Win32FileStream(String path, io::OpenFileFlag flags); - - CRU_DELETE_COPY(Win32FileStream) - CRU_DELETE_MOVE(Win32FileStream) - ~Win32FileStream() override; - public: - bool CanSeek() override; - Index Seek(Index offset, SeekOrigin origin = SeekOrigin::Current) override; - - bool CanRead() override; - Index Read(std::byte* buffer, Index offset, Index size) override; - using Stream::Read; - - bool CanWrite() override; - Index Write(const std::byte* buffer, Index offset, Index size) override; - using Stream::Write; - - void Close() override; + protected: + Index DoSeek(Index offset, SeekOrigin origin) override; + Index DoRead(std::byte* buffer, Index offset, Index size) override; + Index DoWrite(const std::byte* buffer, Index offset, Index size) override; + public: String GetPath() const { return path_; } io::OpenFileFlag GetOpenFileFlags() const { return flags_; } details::Win32FileStreamPrivate* GetPrivate_() { return p_; } + CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE + private: - void CheckClosed(); + void DoClose(); private: String path_; io::OpenFileFlag flags_; - bool closed_ = false; details::Win32FileStreamPrivate* p_; }; diff --git a/src/base/SubProcess.cpp b/src/base/SubProcess.cpp index 1133b848..9a6ee64f 100644 --- a/src/base/SubProcess.cpp +++ b/src/base/SubProcess.cpp @@ -7,11 +7,6 @@ #endif namespace cru { - -#ifdef CRU_PLATFORM_UNIX -using ThisPlatformSubProcessImpl = platform::unix::PosixSpawnSubProcessImpl; -#endif - PlatformSubProcess::PlatformSubProcess( SubProcessStartInfo start_info, std::shared_ptr<IPlatformSubProcessImpl> impl) @@ -156,8 +151,14 @@ SubProcessExitResult SubProcess::Call( } SubProcess::SubProcess(SubProcessStartInfo start_info) { + +#ifdef CRU_PLATFORM_UNIX platform_process_.reset(new PlatformSubProcess( - std::move(start_info), std::make_shared<ThisPlatformSubProcessImpl>())); + std::move(start_info), + std::make_shared<platform::unix::PosixSpawnSubProcessImpl>())); +#else + NotImplemented(); +#endif platform_process_->Start(); } diff --git a/src/base/io/Stream.cpp b/src/base/io/Stream.cpp index 9b7ef856..4cb58eca 100644 --- a/src/base/io/Stream.cpp +++ b/src/base/io/Stream.cpp @@ -171,11 +171,11 @@ Index Stream::DoGetSize() { } Index Stream::DoRead(std::byte* buffer, Index offset, Index size) { - throw Exception(u"Stream is readable but DoSeek is not implemented."); + throw Exception(u"Stream is readable but DoRead is not implemented."); } Index Stream::DoWrite(const std::byte* buffer, Index offset, Index size) { - throw Exception(u"Stream is writable but DoSeek is not implemented."); + throw Exception(u"Stream is writable but DoWrite is not implemented."); } void Stream::DoFlush() {} diff --git a/src/base/log/StdioLogTarget.cpp b/src/base/log/StdioLogTarget.cpp index 45203f2c..a17e91b6 100644 --- a/src/base/log/StdioLogTarget.cpp +++ b/src/base/log/StdioLogTarget.cpp @@ -1,5 +1,9 @@ #include "cru/base/log/StdioLogTarget.h" +#ifdef _WIN32 +#include "cru/base/String.h" +#endif + #include <iostream> namespace cru::log { diff --git a/src/base/platform/win/DebugLogTarget.cpp b/src/base/platform/win/DebugLogTarget.cpp index 89bd3d19..afbab620 100644 --- a/src/base/platform/win/DebugLogTarget.cpp +++ b/src/base/platform/win/DebugLogTarget.cpp @@ -1,10 +1,12 @@ #include "cru/base/platform/win/DebugLogTarget.h" +#include "cru/base/String.h" + namespace cru::platform::win { -void WinDebugLogTarget::Write(::cru::log::LogLevel level, StringView s) { +void WinDebugLogTarget::Write(::cru::log::LogLevel level, std::string s) { CRU_UNUSED(level) - String m = s.ToString(); + String m = String::FromUtf8(s); ::OutputDebugStringW(reinterpret_cast<const wchar_t*>(m.c_str())); } } // namespace cru::platform::win diff --git a/src/base/platform/win/Exception.cpp b/src/base/platform/win/Exception.cpp index 5ff6146b..941875f7 100644 --- a/src/base/platform/win/Exception.cpp +++ b/src/base/platform/win/Exception.cpp @@ -14,12 +14,12 @@ inline String HResultMakeMessage(HRESULT h_result, } HResultError::HResultError(HRESULT h_result) - : PlatformException(HResultMakeMessage(h_result, std::nullopt)), + : Exception(HResultMakeMessage(h_result, std::nullopt)), h_result_(h_result) {} HResultError::HResultError(HRESULT h_result, std::string_view additional_message) - : PlatformException(HResultMakeMessage( + : Exception(HResultMakeMessage( h_result, String::FromUtf8(additional_message.data(), additional_message.size()))), h_result_(h_result) {} @@ -33,6 +33,6 @@ Win32Error::Win32Error(String message) : Win32Error(::GetLastError(), message) {} Win32Error::Win32Error(DWORD error_code, String message) - : PlatformException(Win32MakeMessage(error_code, message)), + : Exception(Win32MakeMessage(error_code, message)), error_code_(error_code) {} } // namespace cru::platform::win diff --git a/src/base/platform/win/Win32FileStream.cpp b/src/base/platform/win/Win32FileStream.cpp index 54e6ae45..f3809b3e 100644 --- a/src/base/platform/win/Win32FileStream.cpp +++ b/src/base/platform/win/Win32FileStream.cpp @@ -14,7 +14,8 @@ namespace cru::platform::win { using namespace cru::io; Win32FileStream::Win32FileStream(String path, OpenFileFlag flags) - : path_(std::move(path)), + : Stream(true, true, true), + path_(std::move(path)), flags_(flags), p_(new details::Win32FileStreamPrivate()) { DWORD grfMode = STGM_SHARE_DENY_NONE; @@ -46,15 +47,11 @@ Win32FileStream::Win32FileStream(String path, OpenFileFlag flags) } Win32FileStream::~Win32FileStream() { - Close(); + DoClose(); delete p_; } -bool Win32FileStream::CanSeek() { return true; } - -Index Win32FileStream::Seek(Index offset, SeekOrigin origin) { - CheckClosed(); - +Index Win32FileStream::DoSeek(Index offset, SeekOrigin origin) { DWORD dwOrigin = 0; if (origin == SeekOrigin::Current) { @@ -74,23 +71,13 @@ Index Win32FileStream::Seek(Index offset, SeekOrigin origin) { return n_new_offset.QuadPart; } -bool Win32FileStream::CanRead() { return true; } - -Index Win32FileStream::Read(std::byte* buffer, Index offset, Index size) { - if (size < 0) { - throw Exception(u"Size must be greater than 0."); - } - - CheckClosed(); - +Index Win32FileStream::DoRead(std::byte* buffer, Index offset, Index size) { ULONG n_read; ThrowIfFailed(p_->stream_->Read(buffer + offset, size, &n_read)); return n_read; } -bool Win32FileStream::CanWrite() { return true; } - -Index Win32FileStream::Write(const std::byte* buffer, Index offset, +Index Win32FileStream::DoWrite(const std::byte* buffer, Index offset, Index size) { if (size < 0) { throw Exception(u"Size must be greater than 0."); @@ -104,19 +91,13 @@ Index Win32FileStream::Write(const std::byte* buffer, Index offset, return n_written; } -void Win32FileStream::Close() { - if (closed_) return; +void Win32FileStream::DoClose() { + CRU_STREAM_BEGIN_CLOSE - if (p_->stream_) { + if (p_->stream_) { p_->stream_->Release(); p_->stream_ = nullptr; } - closed_ = true; } - -void Win32FileStream::CheckClosed() { - if (closed_) throw Exception(u"Stream is closed."); -} - } // namespace cru::platform::win diff --git a/src/platform/gui/win/Window.cpp b/src/platform/gui/win/Window.cpp index 5bd14323..eeb2cde4 100644 --- a/src/platform/gui/win/Window.cpp +++ b/src/platform/gui/win/Window.cpp @@ -245,10 +245,9 @@ void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) { auto lg = [](StringView reason) { CRU_LOG_TAG_WARN( - - u"Failed to set cursor because {} when window is visible. (We need to " - u"update cursor if it is inside the window.) Last error code: {}.", - reason, ::GetLastError()); + "Failed to set cursor because {} when window is visible. (We need to " + "update cursor if it is inside the window.) Last error code: {}.", + reason.ToUtf8(), ::GetLastError()); }; ::POINT point; diff --git a/src/ui/render/ScrollBar.cpp b/src/ui/render/ScrollBar.cpp index 326f7504..af2851cd 100644 --- a/src/ui/render/ScrollBar.cpp +++ b/src/ui/render/ScrollBar.cpp @@ -23,6 +23,7 @@ #include <optional> #include <stdexcept> #include <string> +#include <array> namespace cru::ui::render { using namespace std::chrono_literals; |