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 ++ include/cru/ui/DeleteLater.h | 40 --------------------------- include/cru/ui/components/Component.h | 7 +++-- include/cru/ui/controls/Control.h | 4 +-- 6 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 include/cru/platform/gui/DeleteLater.h delete mode 100644 include/cru/ui/DeleteLater.h (limited to 'include') 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_; diff --git a/include/cru/ui/DeleteLater.h b/include/cru/ui/DeleteLater.h deleted file mode 100644 index 95301bc0..00000000 --- a/include/cru/ui/DeleteLater.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "Base.h" - -#include -#include -#include - -namespace cru::ui { -class CRU_UI_API DeleteLaterImpl { - CRU_DEFINE_CLASS_LOG_TAG("cru::ui::DeleteLaterImpl") - public: - DeleteLaterImpl(); - virtual ~DeleteLaterImpl(); - void DeleteLater(); - - private: - bool delete_later_scheduled_; -}; - -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::ui diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index d8966a89..1e002e5f 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -1,6 +1,7 @@ #pragma once #include "../Base.h" -#include "../DeleteLater.h" + +#include namespace cru::ui::components { /** @@ -8,7 +9,9 @@ namespace cru::ui::components { * \remarks Component should respect children's Component::IsDeleteByParent * value and decide whether to delete it. */ -class CRU_UI_API Component : public Object, public DeleteLaterImpl { +class CRU_UI_API Component + : public Object, + public cru::platform::gui::DeleteLaterImpl { public: virtual controls::Control* GetRootControl() = 0; diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index 9e5e86b8..6a85d25a 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -1,6 +1,5 @@ #pragma once #include "../Base.h" -#include "../DeleteLater.h" #include "../events/KeyEventArgs.h" #include "../events/MouseWheelEventArgs.h" #include "../events/UiEvents.h" @@ -9,6 +8,7 @@ #include "../style/StyleRuleSet.h" #include +#include namespace cru::ui::controls { @@ -22,7 +22,7 @@ namespace cru::ui::controls { * The last two methods are totally for convenient control tree management. */ class CRU_UI_API Control : public Object, - public DeleteLaterImpl, + public cru::platform::gui::DeleteLaterImpl, public SelfResolvable { friend class ControlHost; -- cgit v1.2.3