File indexing completed on 2024-05-12 15:43:39

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_ListRefPtr_h
0023 #define WTF_ListRefPtr_h
0024 
0025 #include <wtf/RefPtr.h>
0026 
0027 namespace WTF
0028 {
0029 
0030 // Specialized version of RefPtr designed for use in singly-linked lists.
0031 // Derefs the list iteratively to avoid recursive derefing that can overflow the stack.
0032 template <typename T> class ListRefPtr : public RefPtr<T>
0033 {
0034 public:
0035     ListRefPtr() : RefPtr<T>() {}
0036     ListRefPtr(T *ptr) : RefPtr<T>(ptr) {}
0037     ListRefPtr(const RefPtr<T> &o) : RefPtr<T>(o) {}
0038     // see comment in PassRefPtr.h for why this takes const reference
0039     template <typename U> ListRefPtr(const PassRefPtr<U> &o) : RefPtr<T>(o) {}
0040 
0041     ~ListRefPtr()
0042     {
0043         RefPtr<T> reaper = this->release();
0044         while (reaper && reaper->refcount() == 1) {
0045             reaper = reaper->releaseNext();    // implicitly protects reaper->next, then derefs reaper
0046         }
0047     }
0048 
0049     ListRefPtr &operator=(T *optr)
0050     {
0051         RefPtr<T>::operator=(optr);
0052         return *this;
0053     }
0054     ListRefPtr &operator=(const RefPtr<T> &o)
0055     {
0056         RefPtr<T>::operator=(o);
0057         return *this;
0058     }
0059     ListRefPtr &operator=(const PassRefPtr<T> &o)
0060     {
0061         RefPtr<T>::operator=(o);
0062         return *this;
0063     }
0064     template <typename U> ListRefPtr &operator=(const RefPtr<U> &o)
0065     {
0066         RefPtr<T>::operator=(o);
0067         return *this;
0068     }
0069     template <typename U> ListRefPtr &operator=(const PassRefPtr<U> &o)
0070     {
0071         RefPtr<T>::operator=(o);
0072         return *this;
0073     }
0074 };
0075 
0076 } // namespace WTF
0077 
0078 using WTF::ListRefPtr;
0079 
0080 #endif // WTF_ListRefPtr_h
0081