Home | History | Annotate | Download | only in wtf
      1 /*
      2  *  Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
      3  *
      4  *  This library is free software; you can redistribute it and/or
      5  *  modify it under the terms of the GNU Library General Public
      6  *  License as published by the Free Software Foundation; either
      7  *  version 2 of the License, or (at your option) any later version.
      8  *
      9  *  This library is distributed in the hope that it will be useful,
     10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  *  Library General Public License for more details.
     13  *
     14  *  You should have received a copy of the GNU Library General Public License
     15  *  along with this library; see the file COPYING.LIB.  If not, write to
     16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  *  Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html
     22 
     23 #ifndef WTF_RefPtr_h
     24 #define WTF_RefPtr_h
     25 
     26 #include <algorithm>
     27 #include "FastAllocBase.h"
     28 #include "PassRefPtr.h"
     29 
     30 namespace WTF {
     31 
     32     enum PlacementNewAdoptType { PlacementNewAdopt };
     33 
     34     template<typename T> class PassRefPtr;
     35     template<typename T> class NonNullPassRefPtr;
     36 
     37     enum HashTableDeletedValueType { HashTableDeletedValue };
     38 
     39     template<typename T> class RefPtr {
     40         WTF_MAKE_FAST_ALLOCATED;
     41     public:
     42         ALWAYS_INLINE RefPtr() : m_ptr(0) { }
     43         ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
     44         ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(m_ptr); }
     45         template<typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { refIfNotNull(m_ptr); }
     46 
     47         // See comments in PassRefPtr.h for an explanation of why these takes const references.
     48         template<typename U> RefPtr(const PassRefPtr<U>&);
     49         template<typename U> RefPtr(const NonNullPassRefPtr<U>&);
     50 
     51         // Special constructor for cases where we overwrite an object in place.
     52         ALWAYS_INLINE RefPtr(PlacementNewAdoptType) { }
     53 
     54         // Hash table deleted values, which are only constructed and never copied or destroyed.
     55         RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
     56         bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
     57 
     58         ALWAYS_INLINE ~RefPtr() { derefIfNotNull(m_ptr); }
     59 
     60         T* get() const { return m_ptr; }
     61 
     62         void clear();
     63         PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
     64 
     65         T& operator*() const { return *m_ptr; }
     66         ALWAYS_INLINE T* operator->() const { return m_ptr; }
     67 
     68         bool operator!() const { return !m_ptr; }
     69 
     70         // This conversion operator allows implicit conversion to bool but not to other integer types.
     71         typedef T* (RefPtr::*UnspecifiedBoolType);
     72         operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; }
     73 
     74         RefPtr& operator=(const RefPtr&);
     75         RefPtr& operator=(T*);
     76         RefPtr& operator=(const PassRefPtr<T>&);
     77         RefPtr& operator=(const NonNullPassRefPtr<T>&);
     78 #if !HAVE(NULLPTR)
     79         RefPtr& operator=(std::nullptr_t) { clear(); return *this; }
     80 #endif
     81         template<typename U> RefPtr& operator=(const RefPtr<U>&);
     82         template<typename U> RefPtr& operator=(const PassRefPtr<U>&);
     83         template<typename U> RefPtr& operator=(const NonNullPassRefPtr<U>&);
     84 
     85         void swap(RefPtr&);
     86 
     87         static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
     88 
     89     private:
     90         T* m_ptr;
     91     };
     92 
     93     template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
     94         : m_ptr(o.leakRef())
     95     {
     96     }
     97 
     98     template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const NonNullPassRefPtr<U>& o)
     99         : m_ptr(o.leakRef())
    100     {
    101     }
    102 
    103     template<typename T> inline void RefPtr<T>::clear()
    104     {
    105         T* ptr = m_ptr;
    106         m_ptr = 0;
    107         derefIfNotNull(ptr);
    108     }
    109 
    110     template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
    111     {
    112         T* optr = o.get();
    113         refIfNotNull(optr);
    114         T* ptr = m_ptr;
    115         m_ptr = optr;
    116         derefIfNotNull(ptr);
    117         return *this;
    118     }
    119 
    120     template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
    121     {
    122         T* optr = o.get();
    123         refIfNotNull(optr);
    124         T* ptr = m_ptr;
    125         m_ptr = optr;
    126         derefIfNotNull(ptr);
    127         return *this;
    128     }
    129 
    130     template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
    131     {
    132         refIfNotNull(optr);
    133         T* ptr = m_ptr;
    134         m_ptr = optr;
    135         derefIfNotNull(ptr);
    136         return *this;
    137     }
    138 
    139     template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
    140     {
    141         T* ptr = m_ptr;
    142         m_ptr = o.leakRef();
    143         derefIfNotNull(ptr);
    144         return *this;
    145     }
    146 
    147     template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<T>& o)
    148     {
    149         T* ptr = m_ptr;
    150         m_ptr = o.leakRef();
    151         derefIfNotNull(ptr);
    152         return *this;
    153     }
    154 
    155     template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
    156     {
    157         T* ptr = m_ptr;
    158         m_ptr = o.leakRef();
    159         derefIfNotNull(ptr);
    160         return *this;
    161     }
    162 
    163     template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::operator=(const NonNullPassRefPtr<U>& o)
    164     {
    165         T* ptr = m_ptr;
    166         m_ptr = o.leakRef();
    167         derefIfNotNull(ptr);
    168         return *this;
    169     }
    170 
    171     template<class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
    172     {
    173         std::swap(m_ptr, o.m_ptr);
    174     }
    175 
    176     template<class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
    177     {
    178         a.swap(b);
    179     }
    180 
    181     template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
    182     {
    183         return a.get() == b.get();
    184     }
    185 
    186     template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
    187     {
    188         return a.get() == b;
    189     }
    190 
    191     template<typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
    192     {
    193         return a == b.get();
    194     }
    195 
    196     template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
    197     {
    198         return a.get() != b.get();
    199     }
    200 
    201     template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
    202     {
    203         return a.get() != b;
    204     }
    205 
    206     template<typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
    207     {
    208         return a != b.get();
    209     }
    210 
    211     template<typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
    212     {
    213         return RefPtr<T>(static_cast<T*>(p.get()));
    214     }
    215 
    216     template<typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
    217     {
    218         return RefPtr<T>(const_cast<T*>(p.get()));
    219     }
    220 
    221     template<typename T> inline T* getPtr(const RefPtr<T>& p)
    222     {
    223         return p.get();
    224     }
    225 
    226 } // namespace WTF
    227 
    228 using WTF::RefPtr;
    229 using WTF::static_pointer_cast;
    230 using WTF::const_pointer_cast;
    231 
    232 #endif // WTF_RefPtr_h
    233