File indexing completed on 2024-05-12 15:43:39
0001 /* 0002 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 * 0019 */ 0020 0021 #ifndef RefCounted_h 0022 #define RefCounted_h 0023 0024 #include <wtf/Assertions.h> 0025 #include <wtf/Noncopyable.h> 0026 0027 namespace WTF 0028 { 0029 0030 template<class T> class RefCounted : Noncopyable 0031 { 0032 public: 0033 RefCounted(int initialRefCount = 1) 0034 : m_refCount(initialRefCount) 0035 #ifndef NDEBUG 0036 , m_deletionHasBegun(false) 0037 #endif 0038 { 0039 } 0040 0041 void ref() 0042 { 0043 ASSERT(!m_deletionHasBegun); 0044 ++m_refCount; 0045 } 0046 0047 void deref() 0048 { 0049 ASSERT(!m_deletionHasBegun); 0050 ASSERT(m_refCount > 0); 0051 if (m_refCount == 1) { 0052 #ifndef NDEBUG 0053 m_deletionHasBegun = true; 0054 #endif 0055 delete static_cast<T *>(this); 0056 } else { 0057 --m_refCount; 0058 } 0059 } 0060 0061 bool hasOneRef() 0062 { 0063 ASSERT(!m_deletionHasBegun); 0064 return m_refCount == 1; 0065 } 0066 0067 int refCount() const 0068 { 0069 return m_refCount; 0070 } 0071 0072 private: 0073 int m_refCount; 0074 #ifndef NDEBUG 0075 bool m_deletionHasBegun; 0076 #endif 0077 }; 0078 0079 } // namespace WTF 0080 0081 using WTF::RefCounted; 0082 0083 #endif // RefCounted_h