diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-14 02:36:06 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-14 02:36:06 +0200 |
commit | 3f6408919ff020a9fcf575a231cbbd8820c40a60 (patch) | |
tree | 570eda8fc11a4b99fc6fa7fc7abce10af417baf3 /AK | |
parent | d5dec1922bd977e92cc86e6e7c0d103ee8a3862d (diff) | |
download | serenity-3f6408919ff020a9fcf575a231cbbd8820c40a60.zip |
AK: Improve smart pointer ergonomics a bit.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/OwnPtr.h | 6 | ||||
-rw-r--r-- | AK/RetainPtr.h | 23 | ||||
-rw-r--r-- | AK/Retained.h | 3 |
3 files changed, 26 insertions, 6 deletions
diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index 1988b7169e..828aca3110 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -66,9 +66,6 @@ public: bool operator!() const { return !m_ptr; } - typedef T* OwnPtr::*UnspecifiedBoolType; - operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : nullptr; } - T* leak_ptr() { T* leakedPtr = m_ptr; @@ -85,6 +82,9 @@ public: T& operator*() { return *m_ptr; } const T& operator*() const { return *m_ptr; } + operator const T*() const { return m_ptr; } + operator T*() { return m_ptr; } + operator bool() { return !!m_ptr; } private: diff --git a/AK/RetainPtr.h b/AK/RetainPtr.h index be92b181f0..b23d070d2e 100644 --- a/AK/RetainPtr.h +++ b/AK/RetainPtr.h @@ -53,6 +53,14 @@ public: return *this; } + template<typename U> + RetainPtr& operator=(Retained<U>&& other) + { + release_if_not_null(m_ptr); + m_ptr = &other.leak_ref(); + return *this; + } + RetainPtr& operator=(T* ptr) { if (m_ptr != ptr) @@ -90,9 +98,6 @@ public: bool operator!() const { return !m_ptr; } - typedef T* RetainPtr::*UnspecifiedBoolType; - operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : nullptr; } - T* leak_ref() { T* leakedPtr = m_ptr; @@ -109,8 +114,20 @@ public: T& operator*() { return *m_ptr; } const T& operator*() const { return *m_ptr; } + operator const T*() const { return m_ptr; } + operator T*() { return m_ptr; } + operator bool() { return !!m_ptr; } + bool operator==(std::nullptr_t) const { return !m_ptr; } + bool operator!=(std::nullptr_t) const { return m_ptr; } + + bool operator==(const RetainPtr& other) const { return m_ptr == other.m_ptr; } + bool operator!=(const RetainPtr& other) const { return m_ptr != other.m_ptr; } + + bool operator==(const T* other) const { return m_ptr == other; } + bool operator!=(const T* other) const { return m_ptr != other; } + bool is_null() const { return !m_ptr; } private: diff --git a/AK/Retained.h b/AK/Retained.h index ebf61bc468..10756d6440 100644 --- a/AK/Retained.h +++ b/AK/Retained.h @@ -107,6 +107,9 @@ public: CALLABLE_WHEN(unconsumed) T& operator*() { ASSERT(m_ptr); return *m_ptr; } CALLABLE_WHEN(unconsumed) const T& operator*() const { ASSERT(m_ptr); return *m_ptr; } + CALLABLE_WHEN(unconsumed) operator T*() { ASSERT(m_ptr); return m_ptr; } + CALLABLE_WHEN(unconsumed) operator const T*() const { ASSERT(m_ptr); return m_ptr; } + private: Retained() { } |