2023-01-22 03:24:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/Variant.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class MaybeOwned {
|
2023-05-24 22:12:44 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(MaybeOwned);
|
|
|
|
|
2023-01-22 03:24:18 +00:00
|
|
|
public:
|
|
|
|
template<DerivedFrom<T> U>
|
|
|
|
MaybeOwned(NonnullOwnPtr<U> handle)
|
2023-05-24 22:13:58 +00:00
|
|
|
: m_handle(static_cast<NonnullOwnPtr<T>&&>(move(handle)))
|
2023-01-22 03:24:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is made `explicit` to not accidentally create a non-owning MaybeOwned,
|
|
|
|
// which may not always be intended.
|
|
|
|
explicit MaybeOwned(T& handle)
|
|
|
|
: m_handle(&handle)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:12:44 +00:00
|
|
|
MaybeOwned(MaybeOwned&&) = default;
|
|
|
|
MaybeOwned& operator=(MaybeOwned&&) = default;
|
|
|
|
|
2024-03-22 17:09:52 +00:00
|
|
|
template<DerivedFrom<T> U>
|
|
|
|
MaybeOwned(MaybeOwned<U>&& other)
|
|
|
|
: m_handle(downcast<U, T>(move(other.m_handle)))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-01-22 03:24:18 +00:00
|
|
|
T* ptr()
|
|
|
|
{
|
|
|
|
if (m_handle.template has<T*>())
|
|
|
|
return m_handle.template get<T*>();
|
|
|
|
else
|
|
|
|
return m_handle.template get<NonnullOwnPtr<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
T const* ptr() const
|
|
|
|
{
|
|
|
|
if (m_handle.template has<T*>())
|
|
|
|
return m_handle.template get<T*>();
|
|
|
|
else
|
|
|
|
return m_handle.template get<NonnullOwnPtr<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() { return ptr(); }
|
|
|
|
T const* operator->() const { return ptr(); }
|
|
|
|
|
|
|
|
T& operator*() { return *ptr(); }
|
|
|
|
T const& operator*() const { return *ptr(); }
|
|
|
|
|
2023-11-17 22:56:33 +00:00
|
|
|
bool is_owned() const { return m_handle.template has<NonnullOwnPtr<T>>(); }
|
|
|
|
|
2023-01-22 03:24:18 +00:00
|
|
|
private:
|
2024-03-22 17:09:52 +00:00
|
|
|
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;
|
2023-01-22 03:24:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USING_AK_GLOBALLY
|
|
|
|
using AK::MaybeOwned;
|
|
|
|
#endif
|