2 * This file is part of the DOM implementation for KDE.
3 * Copyright (C) 2005, 2006 Apple Computer, Inc.
4 * Copyright (C) 2002 Lars Knoll
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include <wtf/SharedPtr.h>
31 template<class type
> class Shared
34 Shared() { _ref
=0; /*counter++;*/ }
35 ~Shared() { /*counter--;*/ }
37 void ref() { _ref
++; }
41 delete static_cast<type
*>(this);
43 bool hasOneRef() { //kDebug(300) << "ref=" << _ref;
46 int refCount() const { return _ref
; }
47 // static int counter;
52 template<class type
> class TreeShared
55 TreeShared() { _ref
= 0; m_parent
= 0; /*counter++;*/ }
56 TreeShared( type
*parent
) { _ref
=0; m_parent
= parent
; /*counter++;*/ }
57 virtual ~TreeShared() { /*counter--;*/ }
59 virtual void removedLastRef() { delete static_cast<type
*>(this); }
61 void ref() { _ref
++; }
64 if(!_ref
&& !m_parent
) {
68 bool hasOneRef() { //kDebug(300) << "ref=" << _ref;
71 int refCount() const { return _ref
; }
72 // static int counter;
74 void setParent(type
*parent
) { m_parent
= parent
; }
75 type
*parent() const { return m_parent
; }
82 //A special pointer for nodes keeping track of the document,
83 //which helps distinguish back links from them to it, in order to break
85 template <class T
> class DocPtr
{
87 DocPtr() : m_ptr(0) {}
88 DocPtr(T
*ptr
) : m_ptr(ptr
) { if (ptr
) ptr
->selfOnlyRef(); }
89 DocPtr(const DocPtr
&o
) : m_ptr(o
.m_ptr
) { if (T
*ptr
= m_ptr
) ptr
->selfOnlyRef(); }
90 ~DocPtr() { if (T
*ptr
= m_ptr
) ptr
->selfOnlyDeref(); }
92 template <class U
> DocPtr(const DocPtr
<U
> &o
) : m_ptr(o
.get()) { if (T
*ptr
= m_ptr
) ptr
->selfOnlyRef(); }
94 void resetSkippingRef(T
*o
) { m_ptr
= o
; }
96 T
*get() const { return m_ptr
; }
98 T
&operator*() const { return *m_ptr
; }
99 T
*operator->() const { return m_ptr
; }
101 bool operator!() const { return !m_ptr
; }
103 // this type conversion operator allows implicit conversion to
104 // bool but not to other integer types
106 typedef T
* (DocPtr::*UnspecifiedBoolType
)() const;
107 operator UnspecifiedBoolType() const
109 return m_ptr
? &DocPtr::get
: 0;
112 DocPtr
&operator=(const DocPtr
&);
113 DocPtr
&operator=(T
*);
119 template <class T
> DocPtr
<T
> &DocPtr
<T
>::operator=(const DocPtr
<T
> &o
)
125 ptr
->selfOnlyDeref();
130 template <class T
> inline DocPtr
<T
> &DocPtr
<T
>::operator=(T
*optr
)
135 ptr
->selfOnlyDeref();
140 template <class T
> inline bool operator==(const DocPtr
<T
> &a
, const DocPtr
<T
> &b
)
142 return a
.get() == b
.get();
145 template <class T
> inline bool operator==(const DocPtr
<T
> &a
, const T
*b
)
150 template <class T
> inline bool operator==(const T
*a
, const DocPtr
<T
> &b
)
155 template <class T
> inline bool operator!=(const DocPtr
<T
> &a
, const DocPtr
<T
> &b
)
157 return a
.get() != b
.get();
160 template <class T
> inline bool operator!=(const DocPtr
<T
> &a
, const T
*b
)
165 template <class T
> inline bool operator!=(const T
*a
, const DocPtr
<T
> &b
)