WTF
SharedPtr.h
Go to the documentation of this file.00001 // -*- mode: c++; c-basic-offset: 4 -*- 00002 /* 00003 * This file is part of the KDE libraries 00004 * Copyright (C) 2005 Apple Computer, Inc. 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00023 #ifndef WTF_SharedPtr_h 00024 #define WTF_SharedPtr_h 00025 00026 namespace WTF { 00027 00028 // FIXME: Change template name to RefPtr? 00029 template <class T> class SharedPtr 00030 { 00031 public: 00032 SharedPtr() : m_ptr(0) {} 00033 SharedPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); } 00034 SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); } 00035 ~SharedPtr() { if (T *ptr = m_ptr) ptr->deref(); } 00036 00037 template <class U> SharedPtr(const SharedPtr<U> &o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); } 00038 00039 // FIXME: Deprecate in favor of operators below, then remove? 00040 bool isNull() const { return m_ptr == 0; } 00041 bool notNull() const { return m_ptr != 0; } 00042 00043 // FIXME: Deprecate in favor of operator=, then remove? 00044 void reset() { if (T *ptr = m_ptr) ptr->deref(); m_ptr = 0; } 00045 void reset(T *o) { if (o) o->ref(); if (T *ptr = m_ptr) ptr->deref(); m_ptr = o; } 00046 00047 T *get() const { return m_ptr; } 00048 00049 T &operator*() const { return *m_ptr; } 00050 T *operator->() const { return m_ptr; } 00051 00052 bool operator!() const { return m_ptr == 0; } 00053 operator bool() const { return m_ptr != 0; } 00054 00055 SharedPtr &operator=(const SharedPtr &); 00056 SharedPtr &operator=(T *); 00057 00058 private: 00059 T *m_ptr; 00060 00061 operator int() const; // deliberately not implemented; helps prevent operator bool from converting to int accidentally 00062 }; 00063 00064 template <class T> SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &o) 00065 { 00066 T *optr = o.m_ptr; 00067 if (optr) 00068 optr->ref(); 00069 if (T *ptr = m_ptr) 00070 ptr->deref(); 00071 m_ptr = optr; 00072 return *this; 00073 } 00074 00075 template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *optr) 00076 { 00077 if (optr) 00078 optr->ref(); 00079 if (T *ptr = m_ptr) 00080 ptr->deref(); 00081 m_ptr = optr; 00082 return *this; 00083 } 00084 00085 template <class T> inline bool operator==(const SharedPtr<T> &a, const SharedPtr<T> &b) 00086 { 00087 return a.get() == b.get(); 00088 } 00089 00090 template <class T> inline bool operator==(const SharedPtr<T> &a, const T *b) 00091 { 00092 return a.get() == b; 00093 } 00094 00095 template <class T> inline bool operator==(const T *a, const SharedPtr<T> &b) 00096 { 00097 return a == b.get(); 00098 } 00099 00100 template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b) 00101 { 00102 return a.get() != b.get(); 00103 } 00104 00105 template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b) 00106 { 00107 return a.get() != b; 00108 } 00109 00110 template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b) 00111 { 00112 return a != b.get(); 00113 } 00114 00115 template <class T, class U> inline SharedPtr<T> static_pointer_cast(const SharedPtr<U> &p) 00116 { 00117 return SharedPtr<T>(static_cast<T *>(p.get())); 00118 } 00119 00120 template <class T, class U> inline SharedPtr<T> const_pointer_cast(const SharedPtr<U> &p) 00121 { 00122 return SharedPtr<T>(const_cast<T *>(p.get())); 00123 } 00124 00125 } // namespace WTF 00126 00127 using WTF::SharedPtr; 00128 using WTF::static_pointer_cast; 00129 using WTF::const_pointer_cast; 00130 00131 #endif // WTF_SharedPtr_h