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.
This commit is contained in:
Andreas Kling 2019-06-24 09:58:21 +02:00
parent 25e8498dcc
commit bf97b9589d

View file

@ -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 };
};