1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 07:37:46 +00:00

AK: Delete bad pointer assignment operators and constructors.

We shouldn't allow constructing e.g an OwnPtr from a RefPtr, and similar
conversions. Instead just delete those functions so the compiler whines
loudly if you try to use them.

This patch also deletes constructing OwnPtr from a WeakPtr, even though
that *may* be a valid thing to do, it's sufficiently weird that we can
make the client jump through some hoops if he really wants it. :^)
This commit is contained in:
Andreas Kling 2019-07-11 16:43:20 +02:00
parent 01998a10e3
commit 25e3d46502
3 changed files with 36 additions and 0 deletions

View file

@ -18,6 +18,9 @@
namespace AK { namespace AK {
template<typename T>
class OwnPtr;
template<typename T> template<typename T>
inline void ref_if_not_null(T* ptr) inline void ref_if_not_null(T* ptr)
{ {
@ -93,6 +96,11 @@ public:
#endif #endif
} }
template<typename U>
NonnullRefPtr(const OwnPtr<U>&) = delete;
template<typename U>
NonnullRefPtr& operator=(const OwnPtr<U>&) = delete;
NonnullRefPtr& operator=(const NonnullRefPtr& other) NonnullRefPtr& operator=(const NonnullRefPtr& other)
{ {
if (m_ptr != other.m_ptr) { if (m_ptr != other.m_ptr) {

View file

@ -7,6 +7,13 @@
namespace AK { namespace AK {
template<typename T>
class RefPtr;
template<typename T>
class NonnullRefPtr;
template<typename T>
class WeakPtr;
template<typename T> template<typename T>
class OwnPtr { class OwnPtr {
public: public:
@ -36,6 +43,19 @@ public:
#endif #endif
} }
template<typename U>
OwnPtr(const RefPtr<U>&) = delete;
template<typename U>
OwnPtr(const NonnullRefPtr<U>&) = delete;
template<typename U>
OwnPtr(const WeakPtr<U>&) = delete;
template<typename U>
OwnPtr& operator=(const RefPtr<U>&) = delete;
template<typename U>
OwnPtr& operator=(const NonnullRefPtr<U>&) = delete;
template<typename U>
OwnPtr& operator=(const WeakPtr<U>&) = delete;
OwnPtr& operator=(OwnPtr&& other) OwnPtr& operator=(OwnPtr&& other)
{ {
if (this != &other) { if (this != &other) {

View file

@ -6,6 +6,9 @@
namespace AK { namespace AK {
template<typename T>
class OwnPtr;
template<typename T> template<typename T>
class RefPtr { class RefPtr {
public: public:
@ -86,6 +89,11 @@ public:
} }
RefPtr(std::nullptr_t) {} RefPtr(std::nullptr_t) {}
template<typename U>
RefPtr(const OwnPtr<U>&) = delete;
template<typename U>
RefPtr& operator=(const OwnPtr<U>&) = delete;
RefPtr& operator=(RefPtr&& other) RefPtr& operator=(RefPtr&& other)
{ {
if (this != &other) { if (this != &other) {