Virtual destructor check and unique move into lent protection

This commit was merged in pull request #1.
This commit is contained in:
2024-07-03 11:25:22 +02:00
parent 8297b0b45f
commit c898a3b653
+28
View File
@@ -25,10 +25,24 @@ template<typename T> class lent_ptr;
template<typename T> template<typename T>
using delete_callback = eastl::function<void(T*)>; using delete_callback = eastl::function<void(T*)>;
template<typename T, bool>
struct ptr_traits_passes_virtual_destructor_check
{
static constexpr inline bool value = !std::is_polymorphic<T>::value || std::is_final<T>::value || std::has_virtual_destructor<T>::value;
};
template<typename T>
struct ptr_traits_passes_virtual_destructor_check<T, false> // check not required
{
static constexpr inline bool value = true;
};
template<typename T> template<typename T>
struct ptr_traits struct ptr_traits
{ {
static constexpr inline bool custom_deleter = false; static constexpr inline bool custom_deleter = false;
static constexpr inline bool requires_virtual_destructor_check = true;
}; };
namespace detail namespace detail
@@ -243,6 +257,8 @@ public:
{ {
m_del.destroy(m_ptr); m_del.destroy(m_ptr);
CB_UNIQUE_DEC_REF(); CB_UNIQUE_DEC_REF();
static_assert(ptr_traits_passes_virtual_destructor_check<T, ptr_traits<T>::requires_virtual_destructor_check>::value, "Destructor is not virtual");
} }
unique_ptr(const unique_ptr<T>&) noexcept = delete; unique_ptr(const unique_ptr<T>&) noexcept = delete;
template<typename U> unique_ptr(unique_ptr<U>&& other) noexcept template<typename U> unique_ptr(unique_ptr<U>&& other) noexcept
@@ -396,6 +412,8 @@ public:
} }
~unique_ref() noexcept ~unique_ref() noexcept
{ {
static_assert(ptr_traits_passes_virtual_destructor_check<T, ptr_traits<T>::requires_virtual_destructor_check>::value, "Destructor is not virtual");
m_del.destroy(m_ptr); m_del.destroy(m_ptr);
CB_UNIQUE_DEC_REF(); CB_UNIQUE_DEC_REF();
} }
@@ -556,6 +574,12 @@ public:
{ {
CB_LENT_COPY_CTOR(); CB_LENT_COPY_CTOR();
} }
template<typename U> lent_ptr(unique_ref<U>&& other) = delete;
template<typename U> lent_ptr(unique_ptr<U>&& other) = delete;
template<typename U> lent_ptr& operator = (unique_ref<U>&& other) = delete;
template<typename U> lent_ptr& operator = (unique_ptr<U>&& other) = delete;
template<typename U> lent_ptr(lent_ptr<U>&& other) noexcept template<typename U> lent_ptr(lent_ptr<U>&& other) noexcept
: m_ptr(other.m_ptr) : m_ptr(other.m_ptr)
{ {
@@ -722,6 +746,10 @@ public:
CHECK_NULL; CHECK_NULL;
CB_LENT_COPY_CTOR(); CB_LENT_COPY_CTOR();
} }
template<typename U> lent_ref(unique_ref<U>&& other) = delete;
template<typename U> lent_ref& operator = (unique_ref<U>&& other) = delete;
lent_ref(lent_ref<T>&& other) noexcept lent_ref(lent_ref<T>&& other) noexcept
: m_ptr(other.m_ptr) : m_ptr(other.m_ptr)
{ {