AK: Stop allowing implicit downcast with OwnPtr and NonnullOwnPtr

Same issue here as we had with RefPtr and NonnullRefPtr.

Since we can't make copies of an owning pointer, we don't get quite the
same static_ptr_cast<T> here. Instead I've only added a new templated
version of OwnPtr::release_nonnull() in this patch, to solve the only
issue that popped up.

I'm not sure what the best solution here is, but this works for now.
This commit is contained in:
Andreas Kling 2020-04-05 11:32:30 +02:00
parent 1d468ed6d3
commit 4f200def9c
Notes: sideshowbarker 2024-07-19 07:54:21 +09:00
3 changed files with 12 additions and 5 deletions

View File

@ -62,7 +62,7 @@ public:
template<typename U>
RETURN_TYPESTATE(unconsumed)
NonnullOwnPtr(NonnullOwnPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ptr()))
: m_ptr(other.leak_ptr())
{
ASSERT(m_ptr);
}

View File

@ -45,12 +45,12 @@ public:
template<typename U>
OwnPtr(NonnullOwnPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ptr()))
: m_ptr(other.leak_ptr())
{
}
template<typename U>
OwnPtr(OwnPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ptr()))
: m_ptr(other.leak_ptr())
{
}
OwnPtr(std::nullptr_t) {};
@ -148,6 +148,13 @@ public:
return NonnullOwnPtr<T>(NonnullOwnPtr<T>::Adopt, *leak_ptr());
}
template<typename U>
NonnullOwnPtr<U> release_nonnull()
{
ASSERT(m_ptr);
return NonnullOwnPtr<U>(NonnullOwnPtr<U>::Adopt, static_cast<U&>(*leak_ptr()));
}
T* ptr() { return m_ptr; }
const T* ptr() const { return m_ptr; }

View File

@ -93,7 +93,7 @@ public:
if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
auto message = move(m_unprocessed_messages[i]);
m_unprocessed_messages.remove(i);
return message;
return message.template release_nonnull<MessageType>();
}
}
for (;;) {
@ -112,7 +112,7 @@ public:
if (m_unprocessed_messages[i]->message_id() == MessageType::static_message_id()) {
auto message = move(m_unprocessed_messages[i]);
m_unprocessed_messages.remove(i);
return message;
return message.template release_nonnull<MessageType>();
}
}
}