summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-24 09:58:21 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-24 09:58:21 +0200
commitbf97b9589d8008833091a04a1adbf80cb92275d4 (patch)
tree746dea319ef934c62e482bee097339391bbcd811
parent25e8498dcca6842679dfb7a7885b8965dfbb6c9c (diff)
downloadserenity-bf97b9589d8008833091a04a1adbf80cb92275d4.zip
NonnullRefPtr: Some improvements.
- Delete the default constructor instead of just making it private. It's never valid to create an empty NonnullRefPtr. - Add copy assignment operators. I originally omitted these to force use of .copy_ref() at call sites, but the hassle/gain ratio is minuscule. - Allow calling all the assignment operators in all consumable states. This codifies that it's okay to overwrite a moved-from NonnullRefPtr.
-rw-r--r--AK/NonnullRefPtr.h26
1 files changed, 22 insertions, 4 deletions
diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h
index ca8f22b1ef..c3c3becff9 100644
--- a/AK/NonnullRefPtr.h
+++ b/AK/NonnullRefPtr.h
@@ -95,7 +95,27 @@ public:
#endif
}
- CALLABLE_WHEN(unconsumed)
+ NonnullRefPtr& operator=(const NonnullRefPtr& other)
+ {
+ if (m_ptr != other.m_ptr) {
+ deref_if_not_null(m_ptr);
+ m_ptr = const_cast<T*>(other.ptr());
+ m_ptr->ref();
+ }
+ return *this;
+ }
+
+ template<typename U>
+ NonnullRefPtr& operator=(const NonnullRefPtr<U>& other)
+ {
+ if (m_ptr != other.m_ptr) {
+ deref_if_not_null(m_ptr);
+ m_ptr = const_cast<T*>(static_cast<const T*>(other.ptr()));
+ m_ptr->ref();
+ }
+ return *this;
+ }
+
NonnullRefPtr& operator=(NonnullRefPtr&& other)
{
if (this != &other) {
@@ -106,7 +126,6 @@ public:
}
template<typename U>
- CALLABLE_WHEN(unconsumed)
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other)
{
if (this != static_cast<void*>(&other)) {
@@ -116,7 +135,6 @@ public:
return *this;
}
- CALLABLE_WHEN(unconsumed)
NonnullRefPtr& operator=(T& object)
{
if (m_ptr != &object)
@@ -208,7 +226,7 @@ public:
}
private:
- NonnullRefPtr() {}
+ NonnullRefPtr() = delete;
T* m_ptr { nullptr };
};