From 04d243699cbde40fe69472f4c4df38c36f7942ef Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Tue, 18 Nov 2025 17:31:23 +0800 Subject: Move delete later to platform::gui. --- include/cru/platform/gui/DeleteLater.h | 41 ++++++++++++++++++++++++++++ include/cru/platform/gui/UiApplication.h | 14 ++++++++++ include/cru/platform/gui/win/UiApplication.h | 3 ++ 3 files changed, 58 insertions(+) create mode 100644 include/cru/platform/gui/DeleteLater.h (limited to 'include/cru/platform/gui') diff --git a/include/cru/platform/gui/DeleteLater.h b/include/cru/platform/gui/DeleteLater.h new file mode 100644 index 00000000..c0578974 --- /dev/null +++ b/include/cru/platform/gui/DeleteLater.h @@ -0,0 +1,41 @@ +#pragma once +#include "UiApplication.h" + +#include +#include +#include + +namespace cru::platform::gui { +template +class DeleteLaterImpl { + CRU_DEFINE_CLASS_LOG_TAG("cru::platform::gui::DeleteLaterImpl") + + public: + virtual ~DeleteLaterImpl() {} + + void DeleteLater() { + IUiApplication::GetInstance()->DeleteLater(static_cast(this)); + } +}; + +namespace details { +template +struct DeleteLaterPtrDeleter { + void operator()(T* p) const noexcept { p->DeleteLater(); } +}; +} // namespace details + +template +using DeleteLaterPtr = std::unique_ptr>; + +template +DeleteLaterPtr ToDeleteLaterPtr(std::unique_ptr&& p) { + return DeleteLaterPtr(p.release()); +} + +template +DeleteLaterPtr MakeDeleteLater(Args&&... args) { + return DeleteLaterPtr(new T(std::forward(args)...)); +} + +} // namespace cru::platform::gui diff --git a/include/cru/platform/gui/UiApplication.h b/include/cru/platform/gui/UiApplication.h index 84011275..c4454b2e 100644 --- a/include/cru/platform/gui/UiApplication.h +++ b/include/cru/platform/gui/UiApplication.h @@ -16,6 +16,18 @@ struct INativeWindow; struct IInputMethodContext; struct IClipboard; +class CRU_PLATFORM_GUI_API DeleteLaterPool : public Object { + public: + void Add(Object* object); + + void Clean(); + + private: + // May contain duplicate object pointer. When performing deleting, use a set + // to record deleted objects to avoid double delete. + std::vector pool_; +}; + // The entry point of a ui application. struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { public: @@ -51,6 +63,8 @@ struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { // result in no-op. virtual void CancelTimer(long long id) = 0; + virtual void DeleteLater(Object* object) = 0; + virtual std::vector GetAllWindow() = 0; virtual INativeWindow* CreateWindow() = 0; diff --git a/include/cru/platform/gui/win/UiApplication.h b/include/cru/platform/gui/win/UiApplication.h index b2d1f39b..c5057c4b 100644 --- a/include/cru/platform/gui/win/UiApplication.h +++ b/include/cru/platform/gui/win/UiApplication.h @@ -48,6 +48,8 @@ class CRU_WIN_GUI_API WinUiApplication : public WinNativeResource, std::function action) override; void CancelTimer(long long id) override; + void DeleteLater(Object* object) override; + std::vector GetAllWindow() override; INativeWindow* CreateWindow() override; @@ -79,6 +81,7 @@ class CRU_WIN_GUI_API WinUiApplication : public WinNativeResource, graph_factory_; TimerRegistry> timers_; + DeleteLaterPool delete_later_pool_; std::unique_ptr general_window_class_; std::vector windows_; -- cgit v1.2.3