diff options
Diffstat (limited to 'include/cru/platform')
| -rw-r--r-- | include/cru/platform/gui/DeleteLater.h | 41 | ||||
| -rw-r--r-- | include/cru/platform/gui/UiApplication.h | 14 | ||||
| -rw-r--r-- | include/cru/platform/gui/win/UiApplication.h | 3 |
3 files changed, 58 insertions, 0 deletions
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 <cru/base/Guard.h> +#include <memory> +#include <utility> + +namespace cru::platform::gui { +template <typename TSelf> +class DeleteLaterImpl { + CRU_DEFINE_CLASS_LOG_TAG("cru::platform::gui::DeleteLaterImpl") + + public: + virtual ~DeleteLaterImpl() {} + + void DeleteLater() { + IUiApplication::GetInstance()->DeleteLater(static_cast<TSelf*>(this)); + } +}; + +namespace details { +template <typename T> +struct DeleteLaterPtrDeleter { + void operator()(T* p) const noexcept { p->DeleteLater(); } +}; +} // namespace details + +template <typename T> +using DeleteLaterPtr = std::unique_ptr<T, details::DeleteLaterPtrDeleter<T>>; + +template <typename T> +DeleteLaterPtr<T> ToDeleteLaterPtr(std::unique_ptr<T>&& p) { + return DeleteLaterPtr<T>(p.release()); +} + +template <typename T, typename... Args> +DeleteLaterPtr<T> MakeDeleteLater(Args&&... args) { + return DeleteLaterPtr<T>(new T(std::forward<Args>(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<Object*> 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<INativeWindow*> 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<void()> action) override; void CancelTimer(long long id) override; + void DeleteLater(Object* object) override; + std::vector<INativeWindow*> GetAllWindow() override; INativeWindow* CreateWindow() override; @@ -79,6 +81,7 @@ class CRU_WIN_GUI_API WinUiApplication : public WinNativeResource, graph_factory_; TimerRegistry<std::function<void()>> timers_; + DeleteLaterPool delete_later_pool_; std::unique_ptr<WindowClass> general_window_class_; std::vector<WinNativeWindow*> windows_; |
