diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-21 14:31:45 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-21 14:31:45 +0800 |
| commit | 3cda35dbcbbe1e3854b880169c0efa0fc7a79264 (patch) | |
| tree | 4fa9cc195146b0e28bb9cb186cdbd011166e6024 /include | |
| parent | 2369893a6e07d1837e5df29bdde210decc951557 (diff) | |
| download | cru-3cda35dbcbbe1e3854b880169c0efa0fc7a79264.tar.gz cru-3cda35dbcbbe1e3854b880169c0efa0fc7a79264.tar.bz2 cru-3cda35dbcbbe1e3854b880169c0efa0fc7a79264.zip | |
Use AutoDestruct to impl EventHandlerRevokerGuard.
Diffstat (limited to 'include')
| -rw-r--r-- | include/cru/base/Event.h | 37 | ||||
| -rw-r--r-- | include/cru/base/Guard.h | 4 | ||||
| -rw-r--r-- | include/cru/base/platform/win/Base.h | 6 | ||||
| -rw-r--r-- | include/cru/platform/gui/UiApplication.h | 10 |
4 files changed, 17 insertions, 40 deletions
diff --git a/include/cru/base/Event.h b/include/cru/base/Event.h index 0bf54475..bdaf3ea6 100644 --- a/include/cru/base/Event.h +++ b/include/cru/base/Event.h @@ -1,11 +1,11 @@ #pragma once #include "Base.h" +#include "Guard.h" #include "SelfResolvable.h" #include <algorithm> #include <cassert> #include <functional> -#include <memory> #include <utility> #include <vector> @@ -142,40 +142,13 @@ class Event : public EventBase, public IEvent<TEventArgs> { ::cru::IEvent<arg_type>* name##Event() { return &name##Event_; } namespace details { -struct EventHandlerRevokerDestroyer { - void operator()(EventHandlerRevoker* p) { - (*p)(); - delete p; - } +struct EventHandlerRevokerCaller { + void operator()(const EventHandlerRevoker& revoker) { revoker(); } }; } // namespace details -// A guard class for event revoker. Automatically revoke it when destroyed. -class EventHandlerRevokerGuard { - public: - EventHandlerRevokerGuard() = default; - explicit EventHandlerRevokerGuard(EventHandlerRevoker&& revoker) - : revoker_(new EventHandlerRevoker(std::move(revoker))) {} - - EventHandlerRevoker Get() { - // revoker is only null when this is moved - // you shouldn't use a moved instance - assert(revoker_); - return *revoker_; - } - - EventHandlerRevoker Release() { return std::move(*revoker_.release()); } - - void Reset() { revoker_.reset(); } - - void Reset(EventHandlerRevoker&& revoker) { - revoker_.reset(new EventHandlerRevoker(std::move(revoker))); - } - - private: - std::unique_ptr<EventHandlerRevoker, details::EventHandlerRevokerDestroyer> - revoker_; -}; +using EventHandlerRevokerGuard = + AutoDestruct<EventHandlerRevoker, details::EventHandlerRevokerCaller>; class EventHandlerRevokerListGuard { public: diff --git a/include/cru/base/Guard.h b/include/cru/base/Guard.h index a23e2574..ae120f5a 100644 --- a/include/cru/base/Guard.h +++ b/include/cru/base/Guard.h @@ -43,7 +43,7 @@ inline AutoFreePtr<T> MakeAutoFree(T* ptr) { return AutoFreePtr<T>(ptr); } -template <typename T, void (*DestructFunc)(T value) noexcept> +template <typename T, typename Destructor> class AutoDestruct { public: AutoDestruct() : value_(std::nullopt), auto_destruct_(false) {} @@ -110,7 +110,7 @@ class AutoDestruct { private: void DoDestruct() { if (auto_destruct_ && value_) { - DestructFunc(*value_); + Destructor{}(*value_); } } diff --git a/include/cru/base/platform/win/Base.h b/include/cru/base/platform/win/Base.h index cdd92541..cd5007e3 100644 --- a/include/cru/base/platform/win/Base.h +++ b/include/cru/base/platform/win/Base.h @@ -55,10 +55,12 @@ inline void CheckWinReturn(BOOL r, std::string_view message = "") { } namespace details { -inline void MyCloseHandle(HANDLE handle) noexcept { ::CloseHandle(handle); } +struct HandleCloser { + void operator()(HANDLE handle) noexcept { ::CloseHandle(handle); } +}; } // namespace details -using Win32Handle = AutoDestruct<HANDLE, details::MyCloseHandle>; +using Win32Handle = AutoDestruct<HANDLE, details::HandleCloser>; struct UniDirectionalWin32PipeResult { Win32Handle read; diff --git a/include/cru/platform/gui/UiApplication.h b/include/cru/platform/gui/UiApplication.h index c4454b2e..7200c617 100644 --- a/include/cru/platform/gui/UiApplication.h +++ b/include/cru/platform/gui/UiApplication.h @@ -91,10 +91,12 @@ struct CRU_PLATFORM_GUI_API IUiApplication : public virtual IPlatformResource { }; namespace details { -inline void CancelTimer(long long id) noexcept { - IUiApplication::GetInstance()->CancelTimer(id); -} +struct TimerCanceler { + void operator()(long long id) { + IUiApplication::GetInstance()->CancelTimer(id); + } +}; } // namespace details -using TimerAutoCanceler = AutoDestruct<long long, details::CancelTimer>; +using TimerAutoCanceler = AutoDestruct<long long, details::TimerCanceler>; } // namespace cru::platform::gui |
