mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 15:10:19 +00:00
AK: Allow creating a MaybeOwned<Superclass> from a MaybeOwned<Subclass>
This commit is contained in:
parent
a72770cdf6
commit
07750774cf
Notes:
sideshowbarker
2024-07-17 06:54:15 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/07750774cf Pull-request: https://github.com/SerenityOS/serenity/pull/23682 Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/Zaggy1024 Reviewed-by: https://github.com/timschumi ✅
1 changed files with 22 additions and 1 deletions
|
@ -32,6 +32,12 @@ public:
|
||||||
MaybeOwned(MaybeOwned&&) = default;
|
MaybeOwned(MaybeOwned&&) = default;
|
||||||
MaybeOwned& operator=(MaybeOwned&&) = default;
|
MaybeOwned& operator=(MaybeOwned&&) = default;
|
||||||
|
|
||||||
|
template<DerivedFrom<T> U>
|
||||||
|
MaybeOwned(MaybeOwned<U>&& other)
|
||||||
|
: m_handle(downcast<U, T>(move(other.m_handle)))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
T* ptr()
|
T* ptr()
|
||||||
{
|
{
|
||||||
if (m_handle.template has<T*>())
|
if (m_handle.template has<T*>())
|
||||||
|
@ -57,7 +63,22 @@ public:
|
||||||
bool is_owned() const { return m_handle.template has<NonnullOwnPtr<T>>(); }
|
bool is_owned() const { return m_handle.template has<NonnullOwnPtr<T>>(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Variant<NonnullOwnPtr<T>, T*> m_handle;
|
template<typename F>
|
||||||
|
friend class MaybeOwned;
|
||||||
|
|
||||||
|
template<typename HT>
|
||||||
|
using Handle = Variant<NonnullOwnPtr<HT>, HT*>;
|
||||||
|
|
||||||
|
template<typename U, typename D>
|
||||||
|
Handle<D> downcast(Handle<U>&& variant)
|
||||||
|
{
|
||||||
|
if (variant.template has<U*>())
|
||||||
|
return variant.template get<U*>();
|
||||||
|
else
|
||||||
|
return static_cast<NonnullOwnPtr<T>&&>(move(variant.template get<NonnullOwnPtr<U>>()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Handle<T> m_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue