1 /* 2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21 #ifndef RefCounted_h 22 #define RefCounted_h 23 24 #include <wtf/Assertions.h> 25 #include <wtf/Noncopyable.h> 26 27 namespace WTF 28 { 29 30 template<class T> class RefCounted : Noncopyable 31 { 32 public: 33 RefCounted(int initialRefCount = 1) m_refCount(initialRefCount)34 : m_refCount(initialRefCount) 35 #ifndef NDEBUG 36 , m_deletionHasBegun(false) 37 #endif 38 { 39 } 40 ref()41 void ref() 42 { 43 ASSERT(!m_deletionHasBegun); 44 ++m_refCount; 45 } 46 deref()47 void deref() 48 { 49 ASSERT(!m_deletionHasBegun); 50 ASSERT(m_refCount > 0); 51 if (m_refCount == 1) { 52 #ifndef NDEBUG 53 m_deletionHasBegun = true; 54 #endif 55 delete static_cast<T *>(this); 56 } else { 57 --m_refCount; 58 } 59 } 60 hasOneRef()61 bool hasOneRef() 62 { 63 ASSERT(!m_deletionHasBegun); 64 return m_refCount == 1; 65 } 66 refCount()67 int refCount() const 68 { 69 return m_refCount; 70 } 71 72 private: 73 int m_refCount; 74 #ifndef NDEBUG 75 bool m_deletionHasBegun; 76 #endif 77 }; 78 79 } // namespace WTF 80 81 using WTF::RefCounted; 82 83 #endif // RefCounted_h 84