mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
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:
parent
25e8498dcc
commit
bf97b9589d
1 changed files with 22 additions and 4 deletions
|
@ -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 };
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue