webengine/osswebengine/JavaScriptCore/wtf/PassRefPtr.h
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 // -*- mode: c++; c-basic-offset: 4 -*-
       
     2 /*
       
     3  *  This file is part of the KDE libraries
       
     4  *  Copyright (C) 2005, 2006 Apple Computer, Inc.
       
     5  *
       
     6  *  This library is free software; you can redistribute it and/or
       
     7  *  modify it under the terms of the GNU Library General Public
       
     8  *  License as published by the Free Software Foundation; either
       
     9  *  version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  *  This library is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    14  *  Library General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU Library General Public License
       
    17  *  along with this library; see the file COPYING.LIB.  If not, write to
       
    18  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    19  *  Boston, MA 02110-1301, USA.
       
    20  *
       
    21  */
       
    22 
       
    23 #ifndef WTF_PassRefPtr_h
       
    24 #define WTF_PassRefPtr_h
       
    25 
       
    26 namespace WTF {
       
    27 
       
    28     template<typename T> class RefPtr;
       
    29     template<typename T> class PassRefPtr;
       
    30     template <typename T> PassRefPtr<T> adoptRef(T*);
       
    31 
       
    32     template<typename T> 
       
    33     class PassRefPtr
       
    34     {
       
    35     public:
       
    36         PassRefPtr() : m_ptr(0) {}
       
    37         PassRefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
       
    38         // It somewhat breaks the type system to allow transfer of ownership out of
       
    39         // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
       
    40         // temporaries, and we don't really have a need to use real const PassRefPtrs 
       
    41         // anyway.
       
    42         PassRefPtr(const PassRefPtr& o) : m_ptr(o.releaseRef()) {}
       
    43         template <typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.releaseRef()) { }
       
    44 
       
    45         ~PassRefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
       
    46         
       
    47         template <class U> 
       
    48         PassRefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
       
    49         
       
    50         T* get() const { return m_ptr; }
       
    51 
       
    52         T* releaseRef() const { T* tmp = m_ptr; m_ptr = 0; return tmp; }
       
    53 
       
    54         T& operator*() const { return *m_ptr; }
       
    55         T* operator->() const { return m_ptr; }
       
    56         
       
    57         bool operator!() const { return !m_ptr; }
       
    58 
       
    59         // This conversion operator allows implicit conversion to bool but not to other integer types.
       
    60         typedef T* (PassRefPtr::*UnspecifiedBoolType)() const;
       
    61         operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::get : 0; }
       
    62         
       
    63         PassRefPtr& operator=(T*);
       
    64         PassRefPtr& operator=(const PassRefPtr&);
       
    65         template <typename U> PassRefPtr& operator=(const PassRefPtr<U>&);
       
    66         template <typename U> PassRefPtr& operator=(const RefPtr<U>&);
       
    67 
       
    68         friend PassRefPtr adoptRef<T>(T*);
       
    69     private:
       
    70         // adopting constructor
       
    71         PassRefPtr(T* ptr, bool) : m_ptr(ptr) {}
       
    72         mutable T* m_ptr;
       
    73     };
       
    74     
       
    75     template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const RefPtr<U>& o) 
       
    76     {
       
    77         T* optr = o.get();
       
    78         if (optr)
       
    79             optr->ref();
       
    80         T* ptr = m_ptr;
       
    81         m_ptr = optr;
       
    82         if (ptr)
       
    83             ptr->deref();
       
    84         return *this;
       
    85     }
       
    86     
       
    87     template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(T* optr)
       
    88     {
       
    89         if (optr)
       
    90             optr->ref();
       
    91         T* ptr = m_ptr;
       
    92         m_ptr = optr;
       
    93         if (ptr)
       
    94             ptr->deref();
       
    95         return *this;
       
    96     }
       
    97 
       
    98     template <typename T> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<T>& ref)
       
    99     {
       
   100         T* ptr = m_ptr;
       
   101         m_ptr = ref.releaseRef();
       
   102         if (ptr)
       
   103             ptr->deref();
       
   104         return *this;
       
   105     }
       
   106     
       
   107     template <typename T> template <typename U> inline PassRefPtr<T>& PassRefPtr<T>::operator=(const PassRefPtr<U>& ref)
       
   108     {
       
   109         T* ptr = m_ptr;
       
   110         m_ptr = ref.releaseRef();
       
   111         if (ptr)
       
   112             ptr->deref();
       
   113         return *this;
       
   114     }
       
   115     
       
   116     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
       
   117     { 
       
   118         return a.get() == b.get(); 
       
   119     }
       
   120 
       
   121     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b) 
       
   122     { 
       
   123         return a.get() == b.get(); 
       
   124     }
       
   125 
       
   126     template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b) 
       
   127     { 
       
   128         return a.get() == b.get(); 
       
   129     }
       
   130 
       
   131     template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b) 
       
   132     { 
       
   133         return a.get() == b; 
       
   134     }
       
   135     
       
   136     template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b) 
       
   137     {
       
   138         return a == b.get(); 
       
   139     }
       
   140     
       
   141     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b) 
       
   142     { 
       
   143         return a.get() != b.get(); 
       
   144     }
       
   145 
       
   146     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b) 
       
   147     { 
       
   148         return a.get() != b.get(); 
       
   149     }
       
   150 
       
   151     template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b) 
       
   152     { 
       
   153         return a.get() != b.get(); 
       
   154     }
       
   155 
       
   156     template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
       
   157     {
       
   158         return a.get() != b; 
       
   159     }
       
   160 
       
   161     template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b) 
       
   162     { 
       
   163         return a != b.get(); 
       
   164     }
       
   165     
       
   166     template <typename T> inline PassRefPtr<T> adoptRef(T* p)
       
   167     {
       
   168         return PassRefPtr<T>(p, true);
       
   169     }
       
   170 
       
   171     template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p) 
       
   172     { 
       
   173         return adoptRef(static_cast<T*>(p.releaseRef())); 
       
   174     }
       
   175 
       
   176     template <typename T, typename U> inline PassRefPtr<T> const_pointer_cast(const PassRefPtr<U>& p) 
       
   177     { 
       
   178         return adoptRef(const_cast<T*>(p.releaseRef())); 
       
   179     }
       
   180 
       
   181     template <typename T> inline T* getPtr(const PassRefPtr<T>& p)
       
   182     {
       
   183         return p.get();
       
   184     }
       
   185 
       
   186 } // namespace WTF
       
   187 
       
   188 using WTF::PassRefPtr;
       
   189 using WTF::adoptRef;
       
   190 using WTF::static_pointer_cast;
       
   191 using WTF::const_pointer_cast;
       
   192 
       
   193 #endif // WTF_PassRefPtr_h