From 07750774cff03251523edc1b1f585196363b1b65 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 22 Mar 2024 13:09:52 -0400 Subject: [PATCH] AK: Allow creating a MaybeOwned from a MaybeOwned --- AK/MaybeOwned.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/AK/MaybeOwned.h b/AK/MaybeOwned.h index 38e3c20d119..dd87626fb4d 100644 --- a/AK/MaybeOwned.h +++ b/AK/MaybeOwned.h @@ -32,6 +32,12 @@ public: MaybeOwned(MaybeOwned&&) = default; MaybeOwned& operator=(MaybeOwned&&) = default; + template U> + MaybeOwned(MaybeOwned&& other) + : m_handle(downcast(move(other.m_handle))) + { + } + T* ptr() { if (m_handle.template has()) @@ -57,7 +63,22 @@ public: bool is_owned() const { return m_handle.template has>(); } private: - Variant, T*> m_handle; + template + friend class MaybeOwned; + + template + using Handle = Variant, HT*>; + + template + Handle downcast(Handle&& variant) + { + if (variant.template has()) + return variant.template get(); + else + return static_cast&&>(move(variant.template get>())); + } + + Handle m_handle; }; }