WTF
RefPtr.h
Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*- 00002 /* 00003 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 * 00020 */ 00021 00022 #ifndef WTF_RefPtr_h 00023 #define WTF_RefPtr_h 00024 00025 #include <algorithm> 00026 #include "AlwaysInline.h" 00027 #include "PassRefPtr.h" 00028 00029 namespace WTF { 00030 00031 enum PlacementNewAdoptType { PlacementNewAdopt }; 00032 00033 template <typename T> class PassRefPtr; 00034 00035 enum HashTableDeletedValueType { HashTableDeletedValue }; 00036 00037 template <typename T> class RefPtr { 00038 public: 00039 RefPtr() : m_ptr(0) { } 00040 RefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); } 00041 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->ref(); } 00042 // see comment in PassRefPtr.h for why this takes const reference 00043 template <typename U> RefPtr(const PassRefPtr<U>&); 00044 00045 // Special constructor for cases where we overwrite an object in place. 00046 RefPtr(PlacementNewAdoptType) { } 00047 00048 // Hash table deleted values, which are only constructed and never copied or destroyed. 00049 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } 00050 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); } 00051 00052 ~RefPtr() { if (T* ptr = m_ptr) ptr->deref(); } 00053 00054 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); } 00055 00056 T* get() const { return m_ptr; } 00057 00058 void clear() { if (T* ptr = m_ptr) ptr->deref(); m_ptr = 0; } 00059 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; } 00060 00061 T& operator*() const { return *m_ptr; } 00062 ALWAYS_INLINE T* operator->() const { return m_ptr; } 00063 00064 bool operator!() const { return !m_ptr; } 00065 00066 // This conversion operator allows implicit conversion to bool but not to other integer types. 00067 typedef T* RefPtr::*UnspecifiedBoolType; 00068 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; } 00069 00070 RefPtr& operator=(const RefPtr&); 00071 RefPtr& operator=(T*); 00072 RefPtr& operator=(const PassRefPtr<T>&); 00073 template <typename U> RefPtr& operator=(const RefPtr<U>&); 00074 template <typename U> RefPtr& operator=(const PassRefPtr<U>&); 00075 00076 void swap(RefPtr&); 00077 00078 private: 00079 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } 00080 00081 T* m_ptr; 00082 }; 00083 00084 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o) 00085 : m_ptr(o.releaseRef()) 00086 { 00087 } 00088 00089 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o) 00090 { 00091 T* optr = o.get(); 00092 if (optr) 00093 optr->ref(); 00094 T* ptr = m_ptr; 00095 m_ptr = optr; 00096 if (ptr) 00097 ptr->deref(); 00098 return *this; 00099 } 00100 00101 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o) 00102 { 00103 T* optr = o.get(); 00104 if (optr) 00105 optr->ref(); 00106 T* ptr = m_ptr; 00107 m_ptr = optr; 00108 if (ptr) 00109 ptr->deref(); 00110 return *this; 00111 } 00112 00113 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr) 00114 { 00115 if (optr) 00116 optr->ref(); 00117 T* ptr = m_ptr; 00118 m_ptr = optr; 00119 if (ptr) 00120 ptr->deref(); 00121 return *this; 00122 } 00123 00124 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o) 00125 { 00126 T* ptr = m_ptr; 00127 m_ptr = o.releaseRef(); 00128 if (ptr) 00129 ptr->deref(); 00130 return *this; 00131 } 00132 00133 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o) 00134 { 00135 T* ptr = m_ptr; 00136 m_ptr = o.releaseRef(); 00137 if (ptr) 00138 ptr->deref(); 00139 return *this; 00140 } 00141 00142 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o) 00143 { 00144 std::swap(m_ptr, o.m_ptr); 00145 } 00146 00147 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b) 00148 { 00149 a.swap(b); 00150 } 00151 00152 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b) 00153 { 00154 return a.get() == b.get(); 00155 } 00156 00157 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b) 00158 { 00159 return a.get() == b; 00160 } 00161 00162 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b) 00163 { 00164 return a == b.get(); 00165 } 00166 00167 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b) 00168 { 00169 return a.get() != b.get(); 00170 } 00171 00172 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b) 00173 { 00174 return a.get() != b; 00175 } 00176 00177 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b) 00178 { 00179 return a != b.get(); 00180 } 00181 00182 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p) 00183 { 00184 return RefPtr<T>(static_cast<T*>(p.get())); 00185 } 00186 00187 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p) 00188 { 00189 return RefPtr<T>(const_cast<T*>(p.get())); 00190 } 00191 00192 template <typename T> inline T* getPtr(const RefPtr<T>& p) 00193 { 00194 return p.get(); 00195 } 00196 00197 } // namespace WTF 00198 00199 using WTF::RefPtr; 00200 using WTF::static_pointer_cast; 00201 using WTF::const_pointer_cast; 00202 00203 #endif // WTF_RefPtr_h
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:52:36 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:52:36 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.