Ignore:
Timestamp:
Nov 29, 2011, 9:42:14 PM (14 years ago)
Author:
[email protected]
Message:

Add move semantics to RetainPtr
https://bugs.webkit.org/show_bug.cgi?id=73393

Reviewed by Anders Carlsson.

  • wtf/RetainPtr.h:

(WTF::RetainPtr::RetainPtr):
Add a move constructor and move enabled assignment operators
to RetainPtr if the compiler being used supports rvalue
references. If the compiler does not support it, we fallback
to the copy semantics we have always had.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wtf/RetainPtr.h

    r96757 r101448  
    6666        RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
    6767
     68#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
     69        RetainPtr(RetainPtr&& o) : m_ptr(o.leakRef()) { }
     70#endif
     71
    6872        // Hash table deleted values, which are only constructed and never copied or destroyed.
    6973        RetainPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
     
    9195        RetainPtr& operator=(PtrType);
    9296        template<typename U> RetainPtr& operator=(U*);
     97
     98#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
     99        RetainPtr& operator=(RetainPtr&&);
     100        template<typename U> RetainPtr& operator=(RetainPtr<U>&&);
     101#endif
     102
    93103#if !HAVE(NULLPTR)
    94104        RetainPtr& operator=(std::nullptr_t) { clear(); return *this; }
     
    154164        return *this;
    155165    }
    156    
     166
    157167    template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
    158168    {
     
    166176    }
    167177
     178    template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
     179    {
     180        if (optr)
     181            CFRetain(optr);
     182        PtrType ptr = m_ptr;
     183        m_ptr = optr;
     184        if (ptr)
     185            CFRelease(ptr);
     186        return *this;
     187    }
     188
     189#if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
     190    template<typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<T>&& o)
     191    {
     192        adoptCF(leakRef());
     193        return *this;
     194    }
     195   
     196    template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(RetainPtr<U>&& o)
     197    {
     198        adoptCF(leakRef());
     199        return *this;
     200    }
     201#endif
     202
    168203    template<typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
    169204    {
     
    183218            CFRelease(ptr);
    184219    }
    185    
    186     template<typename T> template<typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
    187     {
    188         if (optr)
    189             CFRetain(optr);
    190         PtrType ptr = m_ptr;
    191         m_ptr = optr;
    192         if (ptr)
    193             CFRelease(ptr);
    194         return *this;
    195     }
    196220
    197221    template<typename T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
     
    234258        return a != b.get();
    235259    }
    236    
     260
    237261    template<typename P> struct HashTraits<RetainPtr<P> > : SimpleClassHashTraits<RetainPtr<P> > { };
    238262   
Note: See TracChangeset for help on using the changeset viewer.