Commit 8176cd45 authored by darin@apple.com's avatar darin@apple.com

Cut down repeated code in RefPtr and RetainPtr by using copy/move-and-swap for assignment operators

https://bugs.webkit.org/show_bug.cgi?id=120223

Reviewed by Anders Carlsson.

* wtf/RefPtr.h: Switch to copy-and-swap in the assignment operators. The move ones already were done
that way.
* wtf/RetainPtr.h: Ditto, except for the move ones too.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@154519 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 067496d0
2013-08-23 Darin Adler <darin@apple.com>
Cut down repeated code in RefPtr and RetainPtr by using copy/move-and-swap for assignment operators
https://bugs.webkit.org/show_bug.cgi?id=120223
Reviewed by Anders Carlsson.
* wtf/RefPtr.h: Switch to copy-and-swap in the assignment operators. The move ones already were done
that way.
* wtf/RetainPtr.h: Ditto, except for the move ones too.
2013-08-23 Oliver Hunt <oliver@apple.com>
Add more validation logic to CompressibleVector
......
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -107,66 +107,60 @@ namespace WTF {
derefIfNotNull(ptr);
}
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr& o)
{
T* optr = o.get();
refIfNotNull(optr);
T* ptr = m_ptr;
m_ptr = optr;
derefIfNotNull(ptr);
RefPtr ptr = o;
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
{
T* optr = o.get();
refIfNotNull(optr);
T* ptr = m_ptr;
m_ptr = optr;
derefIfNotNull(ptr);
RefPtr ptr = o;
swap(ptr);
return *this;
}
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
{
refIfNotNull(optr);
T* ptr = m_ptr;
m_ptr = optr;
derefIfNotNull(ptr);
RefPtr ptr = optr;
swap(ptr);
return *this;
}
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
{
T* ptr = m_ptr;
m_ptr = o.leakRef();
derefIfNotNull(ptr);
RefPtr ptr = o;
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
{
T* ptr = m_ptr;
m_ptr = o.leakRef();
derefIfNotNull(ptr);
RefPtr ptr = o;
swap(ptr);
return *this;
}
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<T>&& o)
template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr&& o)
{
RefPtr<T> ptr = std::move(o);
RefPtr ptr = std::move(o);
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<U>&& o)
{
RefPtr<T> ptr = std::move(o);
RefPtr ptr = std::move(o);
swap(ptr);
return *this;
}
#endif
template<class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
template<class T> inline void RefPtr<T>::swap(RefPtr& o)
{
std::swap(m_ptr, o.m_ptr);
}
......
/*
* Copyright (C) 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
* Copyright (C) 2005, 2006, 2007, 2008, 2010, 2013 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
......@@ -186,74 +186,53 @@ namespace WTF {
return ptr;
}
template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr& o)
{
PtrType optr = o.get();
if (optr)
retain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
release(ptr);
RetainPtr ptr = o;
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
{
PtrType optr = o.get();
if (optr)
retain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
release(ptr);
RetainPtr ptr = o;
swap(ptr);
return *this;
}
template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
{
if (optr)
retain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
release(ptr);
RetainPtr ptr = optr;
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
{
if (optr)
retain(optr);
PtrType ptr = m_ptr;
m_ptr = optr;
if (ptr)
release(ptr);
RetainPtr ptr = optr;
swap(ptr);
return *this;
}
#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<T>&& o)
{
PtrType ptr = m_ptr;
m_ptr = o.leakRef();
if (ptr)
release(ptr);
template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr&& o)
{
RetainPtr ptr = std::move(o);
swap(ptr);
return *this;
}
template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<U>&& o)
{
PtrType ptr = m_ptr;
m_ptr = o.leakRef();
if (ptr)
release(ptr);
RetainPtr ptr = std::move(o);
swap(ptr);
return *this;
}
#endif
template<typename T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
template<typename T> inline void RetainPtr<T>::swap(RetainPtr& o)
{
std::swap(m_ptr, o.m_ptr);
}
......@@ -269,15 +248,15 @@ namespace WTF {
}
template<typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
{
{
return a.get() == b;
}
template<typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b)
{
return a == b.get();
}
template<typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
{
return a.get() != b.get();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment