]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/ListRefPtr.h
JavaScriptCore-461.tar.gz
[apple/javascriptcore.git] / wtf / ListRefPtr.h
CommitLineData
b37bf2e1
A
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2005, 2006 Apple Computer, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_ListRefPtr_h
23#define WTF_ListRefPtr_h
24
25#include <wtf/RefPtr.h>
26
27namespace WTF {
28
29 // Specialized version of RefPtr desgined for use in singly-linked lists.
30 // Derefs the list iteratively to avoid recursive derefing that can overflow the stack.
31 template <typename T> class ListRefPtr : public RefPtr<T> {
32 public:
33 ListRefPtr() : RefPtr<T>() {}
34 ListRefPtr(T* ptr) : RefPtr<T>(ptr) {}
35 ListRefPtr(const RefPtr<T>& o) : RefPtr<T>(o) {}
36 // see comment in PassRefPtr.h for why this takes const reference
37 template <typename U> ListRefPtr(const PassRefPtr<U>& o) : RefPtr<T>(o) {}
38
39 ~ListRefPtr() {
40 RefPtr<T> reaper = this->release();
41 while (reaper && reaper->refcount() == 1)
42 reaper = reaper->releaseNext(); // implicitly protects reaper->next, then derefs reaper
43 }
44
45 ListRefPtr& operator=(T* optr) { RefPtr<T>::operator=(optr); return *this; }
46 ListRefPtr& operator=(const RefPtr<T>& o) { RefPtr<T>::operator=(o); return *this; }
47 ListRefPtr& operator=(const PassRefPtr<T>& o) { RefPtr<T>::operator=(o); return *this; }
48 template <typename U> ListRefPtr& operator=(const RefPtr<U>& o) { RefPtr<T>::operator=(o); return *this; }
49 template <typename U> ListRefPtr& operator=(const PassRefPtr<U>& o) { RefPtr<T>::operator=(o); return *this; }
50 };
51
52 template <typename T> inline T* getPtr(const ListRefPtr<T>& p)
53 {
54 return p.get();
55 }
56
57} // namespace WTF
58
59using WTF::ListRefPtr;
60
61#endif // WTF_ListRefPtr_h