From 1ce8866e5b88da6cc4b7cd4d3440f7d70c7be5db Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Sun, 16 Nov 2025 17:14:20 +0800 Subject: Remove SelfResolvable. --- include/cru/base/Event.h | 19 ++--- include/cru/base/SelfResolvable.h | 153 ---------------------------------- include/cru/ui/components/Component.h | 5 +- include/cru/ui/controls/Control.h | 5 +- include/cru/ui/host/WindowHost.h | 2 +- 5 files changed, 9 insertions(+), 175 deletions(-) delete mode 100644 include/cru/base/SelfResolvable.h (limited to 'include') diff --git a/include/cru/base/Event.h b/include/cru/base/Event.h index 226c2ea4..276c313f 100644 --- a/include/cru/base/Event.h +++ b/include/cru/base/Event.h @@ -1,8 +1,6 @@ #pragma once #include "Base.h" -#include "SelfResolvable.h" - #include #include #include @@ -13,7 +11,7 @@ namespace cru { class EventHandlerRevoker; -class EventBase : public Object, public SelfResolvable { +class EventBase : public Object { friend EventHandlerRevoker; public: @@ -36,28 +34,23 @@ class EventHandlerRevoker { friend EventBase; private: - EventHandlerRevoker(ObjectResolver&& resolver, - EventBase::EventHandlerToken token) - : resolver_(std::move(resolver)), token_(token) {} + EventHandlerRevoker(EventBase* event, EventBase::EventHandlerToken token) + : event_(event), token_(token) {} public: /** * Revoke the registered handler. If the event has already been destroyed or * the handler is already revoked, nothing will be done. */ - void operator()() const { - if (const auto event = resolver_.Resolve()) { - event->RemoveHandler(token_); - } - } + void operator()() const { event_->RemoveHandler(token_); } private: - ObjectResolver resolver_; + EventBase* event_; EventBase::EventHandlerToken token_; }; inline EventHandlerRevoker EventBase::CreateRevoker(EventHandlerToken token) { - return EventHandlerRevoker(CreateResolver(), token); + return EventHandlerRevoker(this, token); } struct IBaseEvent : public virtual Interface { diff --git a/include/cru/base/SelfResolvable.h b/include/cru/base/SelfResolvable.h deleted file mode 100644 index 84fa54f6..00000000 --- a/include/cru/base/SelfResolvable.h +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace cru { -template -class SelfResolvable; - -template -class ObjectResolver { - friend SelfResolvable; - template - friend class ObjectResolver; - - private: - template - using Accessor_ = std::function&)>; - using ThisAccessor_ = Accessor_; - - explicit ObjectResolver(T* o) - : shared_object_ptr_(new void*(o)), - accessor_([](const std::shared_ptr& ptr) { - return static_cast(*ptr); - }) {} - explicit ObjectResolver(std::shared_ptr ptr, ThisAccessor_ accessor) - : shared_object_ptr_(std::move(ptr)), accessor_(std::move(accessor)) {} - - template - static ThisAccessor_ CreateAccessor(Accessor_ parent_accessor) { - return [parent_accessor = - std::move(parent_accessor)](const std::shared_ptr& ptr) { - return static_cast(parent_accessor(ptr)); - }; - } - - public: - template >> - ObjectResolver(const ObjectResolver& other) - : shared_object_ptr_(other.shared_object_ptr_), - accessor_(CreateAccessor(other.accessor_)) {} - - template >> - ObjectResolver(ObjectResolver&& other) - : shared_object_ptr_(std::move(other.shared_object_ptr_)), - accessor_(CreateAccessor(std::move(other.accessor_))) {} - - ObjectResolver(const ObjectResolver&) = default; - ObjectResolver& operator=(const ObjectResolver&) = default; - ObjectResolver(ObjectResolver&&) = default; - ObjectResolver& operator=(ObjectResolver&&) = default; - ~ObjectResolver() = default; - - template >> - ObjectResolver& operator=(const ObjectResolver& other) { - if (this != &other) { - this->shared_object_ptr_ = other.shared_object_ptr_; - this->accessor_ = CreateAccessor(other.accessor_); - } - return *this; - } - - template >> - ObjectResolver& operator=(ObjectResolver&& other) { - if (this != &other) { - this->shared_object_ptr_ = std::move(other.shared_object_ptr_); - this->accessor_ = CreateAccessor(std::move(other.shared_object_ptr_)); - } - return *this; - } - - bool IsValid() const { return this->shared_object_ptr_ != nullptr; } - - T* Resolve() const { - assert(IsValid()); - return this->accessor_(this->shared_object_ptr_); - } - - /** - * @remarks So this class can be used as a functor. - */ - T* operator()() const { return Resolve(); } - - template >> - operator ObjectResolver() const { - return ObjectResolver(*this); - } - - private: - void SetResolvedObject(T* o) { - assert(IsValid()); - *this->shared_object_ptr_ = o; - } - - private: - std::shared_ptr shared_object_ptr_; - std::function&)> accessor_; -}; - -/** - * @remarks - * This class is not copyable and movable since subclass is polymorphic and - * copying is then nonsense. However, you can even delete move capability in - * subclass because it may also be nonsense for subclass. The move capability is - * optional. - * - * Whether this class needs to be thread-safe still has to be considered. - */ -template -class SelfResolvable { - public: - SelfResolvable() : resolver_(CastToSubClass()) {} - SelfResolvable(const SelfResolvable&) = delete; - SelfResolvable& operator=(const SelfResolvable&) = delete; - - // Resolvers to old object will resolve to new object. - SelfResolvable(SelfResolvable&& other) - : resolver_(std::move(other.resolver_)) { - this->resolver_.SetResolvedObject(CastToSubClass()); - } - - // Old resolvers for this object will resolve to nullptr. - // Other's resolvers will now resolve to this. - SelfResolvable& operator=(SelfResolvable&& other) { - if (this != &other) { - this->resolver_ = std::move(other.resolver_); - this->resolver_.SetResolvedObject(CastToSubClass()); - } - return *this; - } - - virtual ~SelfResolvable() { - if (this->resolver_.IsValid()) { - this->resolver_.SetResolvedObject(nullptr); - } - } - - ObjectResolver CreateResolver() { return resolver_; } - - private: - T* CastToSubClass() { return static_cast(this); } - - private: - ObjectResolver resolver_; -}; -} // namespace cru diff --git a/include/cru/ui/components/Component.h b/include/cru/ui/components/Component.h index d44e6728..627e2d3c 100644 --- a/include/cru/ui/components/Component.h +++ b/include/cru/ui/components/Component.h @@ -1,7 +1,6 @@ #pragma once #include "../Base.h" #include "../DeleteLater.h" -#include "cru/base/SelfResolvable.h" namespace cru::ui::components { /** @@ -9,9 +8,7 @@ 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 SelfResolvable, - public DeleteLaterImpl { +class CRU_UI_API Component : public Object, public DeleteLaterImpl { public: Component() = default; ~Component() = default; diff --git a/include/cru/ui/controls/Control.h b/include/cru/ui/controls/Control.h index d70854fe..77f5f392 100644 --- a/include/cru/ui/controls/Control.h +++ b/include/cru/ui/controls/Control.h @@ -4,7 +4,6 @@ #include "../events/UiEvents.h" #include "../render/RenderObject.h" #include "../style/StyleRuleSet.h" -#include "cru/base/SelfResolvable.h" #include "cru/ui/render/MeasureRequirement.h" namespace cru::ui::controls { @@ -18,9 +17,7 @@ namespace cru::ui::controls { * - RemoveChild(Control* child) * The last two methods are totally for convenient control tree management. */ -class CRU_UI_API Control : public Object, - public SelfResolvable, - public DeleteLaterImpl { +class CRU_UI_API Control : public Object, public DeleteLaterImpl { friend class RootControl; CRU_DEFINE_CLASS_LOG_TAG("Control") diff --git a/include/cru/ui/host/WindowHost.h b/include/cru/ui/host/WindowHost.h index 58fd120d..13b06b07 100644 --- a/include/cru/ui/host/WindowHost.h +++ b/include/cru/ui/host/WindowHost.h @@ -16,7 +16,7 @@ class LayoutPaintCycler; struct AfterLayoutEventArgs {}; // The bridge between control tree and native window. -class CRU_UI_API WindowHost : public Object, public SelfResolvable { +class CRU_UI_API WindowHost : public Object { friend controls::Control; CRU_DEFINE_CLASS_LOG_TAG("WindowHost") -- cgit v1.2.3