WTF
RefCounted.h
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Library General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this library; see the file COPYING.LIB. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 * 00019 */ 00020 00021 #ifndef RefCounted_h 00022 #define RefCounted_h 00023 00024 #include <wtf/Assertions.h> 00025 #include <wtf/Noncopyable.h> 00026 00027 namespace WTF { 00028 00029 template<class T> class RefCounted : Noncopyable { 00030 public: 00031 RefCounted(int initialRefCount = 1) 00032 : m_refCount(initialRefCount) 00033 #ifndef NDEBUG 00034 , m_deletionHasBegun(false) 00035 #endif 00036 { 00037 } 00038 00039 void ref() 00040 { 00041 ASSERT(!m_deletionHasBegun); 00042 ++m_refCount; 00043 } 00044 00045 void deref() 00046 { 00047 ASSERT(!m_deletionHasBegun); 00048 ASSERT(m_refCount > 0); 00049 if (m_refCount == 1) { 00050 #ifndef NDEBUG 00051 m_deletionHasBegun = true; 00052 #endif 00053 delete static_cast<T*>(this); 00054 } else 00055 --m_refCount; 00056 } 00057 00058 bool hasOneRef() 00059 { 00060 ASSERT(!m_deletionHasBegun); 00061 return m_refCount == 1; 00062 } 00063 00064 int refCount() const 00065 { 00066 return m_refCount; 00067 } 00068 00069 private: 00070 int m_refCount; 00071 #ifndef NDEBUG 00072 bool m_deletionHasBegun; 00073 #endif 00074 }; 00075 00076 } // namespace WTF 00077 00078 using WTF::RefCounted; 00079 00080 #endif // RefCounted_h
KDE 4.6 API Reference