WebKit2/UIProcess/API/cpp/WKRetainPtr.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Apple Inc. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
       
    14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    23  * THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #ifndef WKRetainPtr_h
       
    27 #define WKRetainPtr_h
       
    28 
       
    29 #include <algorithm>
       
    30 
       
    31 namespace WebKit {
       
    32 
       
    33 enum WKAdoptTag { AdoptWK };
       
    34 
       
    35 template <typename T>
       
    36 class WKRetainPtr {
       
    37 public:
       
    38     typedef T PtrType;
       
    39 
       
    40     WKRetainPtr()
       
    41         : m_ptr(0)
       
    42     {
       
    43     }
       
    44 
       
    45     WKRetainPtr(PtrType ptr)
       
    46         : m_ptr(ptr)
       
    47     {
       
    48         if (ptr)
       
    49             WKRetain(ptr);
       
    50     }
       
    51 
       
    52     WKRetainPtr(WKAdoptTag, PtrType ptr)
       
    53         : m_ptr(ptr)
       
    54     {
       
    55     }
       
    56     
       
    57     template <typename U>
       
    58     WKRetainPtr(const WKRetainPtr<U>& o)
       
    59         : m_ptr(o.get())
       
    60     {
       
    61         if (PtrType ptr = m_ptr)
       
    62             WKRetain(ptr);
       
    63     }
       
    64     
       
    65     WKRetainPtr(const WKRetainPtr& o)
       
    66         : m_ptr(o.m_ptr)
       
    67     {
       
    68         if (PtrType ptr = m_ptr)
       
    69             WKRetain(ptr);
       
    70     }
       
    71 
       
    72     ~WKRetainPtr()
       
    73     {
       
    74         if (PtrType ptr = m_ptr)
       
    75             WKRelease(ptr);
       
    76     }
       
    77 
       
    78     PtrType get() const { return m_ptr; }
       
    79     PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
       
    80     
       
    81     PtrType operator->() const { return m_ptr; }
       
    82     bool operator!() const { return !m_ptr; }
       
    83 
       
    84     // This conversion operator allows implicit conversion to bool but not to other integer types.
       
    85     typedef PtrType WKRetainPtr::*UnspecifiedBoolType;
       
    86     operator UnspecifiedBoolType() const { return m_ptr ? &WKRetainPtr::m_ptr : 0; }
       
    87 
       
    88     WKRetainPtr& operator=(const WKRetainPtr&);
       
    89     template <typename U> WKRetainPtr& operator=(const WKRetainPtr<U>&);
       
    90     WKRetainPtr& operator=(PtrType);
       
    91     template <typename U> WKRetainPtr& operator=(U*);
       
    92 
       
    93     void adopt(PtrType);
       
    94     void swap(WKRetainPtr&);
       
    95 
       
    96 private:
       
    97     PtrType m_ptr;
       
    98 };
       
    99 
       
   100 template <typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<T>& o)
       
   101 {
       
   102     PtrType optr = o.get();
       
   103     if (optr)
       
   104         WKRetain(optr);
       
   105     PtrType ptr = m_ptr;
       
   106     m_ptr = optr;
       
   107     if (ptr)
       
   108         WKRelease(ptr);
       
   109     return *this;
       
   110 }
       
   111 
       
   112 template <typename T> template <typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(const WKRetainPtr<U>& o)
       
   113 {
       
   114     PtrType optr = o.get();
       
   115     if (optr)
       
   116         WKRetain(optr);
       
   117     PtrType ptr = m_ptr;
       
   118     m_ptr = optr;
       
   119     if (ptr)
       
   120         WKRelease(ptr);
       
   121     return *this;
       
   122 }
       
   123 
       
   124 template <typename T> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(PtrType optr)
       
   125 {
       
   126     if (optr)
       
   127         WKRetain(optr);
       
   128     PtrType ptr = m_ptr;
       
   129     m_ptr = optr;
       
   130     if (ptr)
       
   131         WKRelease(ptr);
       
   132     return *this;
       
   133 }
       
   134 
       
   135 template <typename T> inline void WKRetainPtr<T>::adopt(PtrType optr)
       
   136 {
       
   137     PtrType ptr = m_ptr;
       
   138     m_ptr = optr;
       
   139     if (ptr)
       
   140         WKRelease(ptr);
       
   141 }
       
   142 
       
   143 template <typename T> template <typename U> inline WKRetainPtr<T>& WKRetainPtr<T>::operator=(U* optr)
       
   144 {
       
   145     if (optr)
       
   146         WKRetain(optr);
       
   147     PtrType ptr = m_ptr;
       
   148     m_ptr = optr;
       
   149     if (ptr)
       
   150         WKRelease(ptr);
       
   151     return *this;
       
   152 }
       
   153 
       
   154 template <class T> inline void WKRetainPtr<T>::swap(WKRetainPtr<T>& o)
       
   155 {
       
   156     std::swap(m_ptr, o.m_ptr);
       
   157 }
       
   158 
       
   159 template <class T> inline void swap(WKRetainPtr<T>& a, WKRetainPtr<T>& b)
       
   160 {
       
   161     a.swap(b);
       
   162 }
       
   163 
       
   164 template <typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
       
   165 { 
       
   166     return a.get() == b.get(); 
       
   167 }
       
   168 
       
   169 template <typename T, typename U> inline bool operator==(const WKRetainPtr<T>& a, U* b)
       
   170 { 
       
   171     return a.get() == b; 
       
   172 }
       
   173 
       
   174 template <typename T, typename U> inline bool operator==(T* a, const WKRetainPtr<U>& b) 
       
   175 {
       
   176     return a == b.get(); 
       
   177 }
       
   178 
       
   179 template <typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, const WKRetainPtr<U>& b)
       
   180 { 
       
   181     return a.get() != b.get(); 
       
   182 }
       
   183 
       
   184 template <typename T, typename U> inline bool operator!=(const WKRetainPtr<T>& a, U* b)
       
   185 {
       
   186     return a.get() != b; 
       
   187 }
       
   188 
       
   189 template <typename T, typename U> inline bool operator!=(T* a, const WKRetainPtr<U>& b)
       
   190 { 
       
   191     return a != b.get(); 
       
   192 }
       
   193 
       
   194 } // namespace WebKit
       
   195 
       
   196 using WebKit::WKRetainPtr;
       
   197 using WebKit::AdoptWK;
       
   198 
       
   199 #endif // WKRetainPtr_h