File indexing completed on 2024-05-12 15:43:40
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 2005 Apple Computer, Inc. 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 * 0020 */ 0021 0022 #ifndef WTF_SharedPtr_h 0023 #define WTF_SharedPtr_h 0024 0025 namespace WTF 0026 { 0027 0028 // FIXME: Change template name to RefPtr? 0029 template <class T> class SharedPtr 0030 { 0031 public: 0032 SharedPtr() : m_ptr(nullptr) {} 0033 SharedPtr(T *ptr) : m_ptr(ptr) 0034 { 0035 if (ptr) { 0036 ptr->ref(); 0037 } 0038 } 0039 SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) 0040 { 0041 if (T *ptr = m_ptr) { 0042 ptr->ref(); 0043 } 0044 } 0045 ~SharedPtr() 0046 { 0047 if (T *ptr = m_ptr) { 0048 ptr->deref(); 0049 } 0050 } 0051 0052 template <class U> SharedPtr(const SharedPtr<U> &o) : m_ptr(o.get()) 0053 { 0054 if (T *ptr = m_ptr) { 0055 ptr->ref(); 0056 } 0057 } 0058 0059 // FIXME: Deprecate in favor of operators below, then remove? 0060 bool isNull() const 0061 { 0062 return m_ptr == nullptr; 0063 } 0064 bool notNull() const 0065 { 0066 return m_ptr != nullptr; 0067 } 0068 0069 // FIXME: Deprecate in favor of operator=, then remove? 0070 void reset() 0071 { 0072 if (T *ptr = m_ptr) { 0073 ptr->deref(); 0074 } m_ptr = nullptr; 0075 } 0076 void reset(T *o) 0077 { 0078 if (o) { 0079 o->ref(); 0080 } if (T *ptr = m_ptr) { 0081 ptr->deref(); 0082 } m_ptr = o; 0083 } 0084 0085 T *get() const 0086 { 0087 return m_ptr; 0088 } 0089 0090 T &operator*() const 0091 { 0092 return *m_ptr; 0093 } 0094 T *operator->() const 0095 { 0096 return m_ptr; 0097 } 0098 0099 bool operator!() const 0100 { 0101 return m_ptr == nullptr; 0102 } 0103 operator bool() const 0104 { 0105 return m_ptr != nullptr; 0106 } 0107 0108 SharedPtr &operator=(const SharedPtr &); 0109 SharedPtr &operator=(T *); 0110 0111 private: 0112 T *m_ptr; 0113 0114 operator int() const; // deliberately not implemented; helps prevent operator bool from converting to int accidentally 0115 }; 0116 0117 template <class T> SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &o) 0118 { 0119 T *optr = o.m_ptr; 0120 if (optr) { 0121 optr->ref(); 0122 } 0123 if (T *ptr = m_ptr) { 0124 ptr->deref(); 0125 } 0126 m_ptr = optr; 0127 return *this; 0128 } 0129 0130 template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *optr) 0131 { 0132 if (optr) { 0133 optr->ref(); 0134 } 0135 if (T *ptr = m_ptr) { 0136 ptr->deref(); 0137 } 0138 m_ptr = optr; 0139 return *this; 0140 } 0141 0142 template <class T> inline bool operator==(const SharedPtr<T> &a, const SharedPtr<T> &b) 0143 { 0144 return a.get() == b.get(); 0145 } 0146 0147 template <class T> inline bool operator==(const SharedPtr<T> &a, const T *b) 0148 { 0149 return a.get() == b; 0150 } 0151 0152 template <class T> inline bool operator==(const T *a, const SharedPtr<T> &b) 0153 { 0154 return a == b.get(); 0155 } 0156 0157 template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b) 0158 { 0159 return a.get() != b.get(); 0160 } 0161 0162 template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b) 0163 { 0164 return a.get() != b; 0165 } 0166 0167 template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b) 0168 { 0169 return a != b.get(); 0170 } 0171 0172 template <class T, class U> inline SharedPtr<T> static_pointer_cast(const SharedPtr<U> &p) 0173 { 0174 return SharedPtr<T>(static_cast<T *>(p.get())); 0175 } 0176 0177 template <class T, class U> inline SharedPtr<T> const_pointer_cast(const SharedPtr<U> &p) 0178 { 0179 return SharedPtr<T>(const_cast<T *>(p.get())); 0180 } 0181 0182 } // namespace WTF 0183 0184 using WTF::SharedPtr; 0185 using WTF::static_pointer_cast; 0186 using WTF::const_pointer_cast; 0187 0188 #endif // WTF_SharedPtr_h