don't discard iframe children.
[kdelibs.git] / khtml / misc / shared.h
blobf98668d9ae5134cd41d5daeda6ab7c2270bda75a
1 /*
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.
24 #ifndef SHARED_H
25 #define SHARED_H
27 #include <wtf/SharedPtr.h>
29 namespace khtml {
31 template<class type> class Shared
33 public:
34 Shared() { _ref=0; /*counter++;*/ }
35 ~Shared() { /*counter--;*/ }
37 void ref() { _ref++; }
38 void deref() {
39 if(_ref) _ref--;
40 if(!_ref)
41 delete static_cast<type *>(this);
43 bool hasOneRef() { //kDebug(300) << "ref=" << _ref;
44 return _ref==1; }
46 int refCount() const { return _ref; }
47 // static int counter;
48 protected:
49 unsigned int _ref;
52 template<class type> class TreeShared
54 public:
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++; }
62 void deref() {
63 if(_ref) _ref--;
64 if(!_ref && !m_parent) {
65 removedLastRef();
68 bool hasOneRef() { //kDebug(300) << "ref=" << _ref;
69 return _ref==1; }
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; }
76 private:
77 unsigned int _ref;
78 protected:
79 type *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
84 //cycles
85 template <class T> class DocPtr {
86 public:
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 *);
115 private:
116 T *m_ptr;
119 template <class T> DocPtr<T> &DocPtr<T>::operator=(const DocPtr<T> &o)
121 T *optr = o.m_ptr;
122 if (optr)
123 optr->selfOnlyRef();
124 if (T *ptr = m_ptr)
125 ptr->selfOnlyDeref();
126 m_ptr = optr;
127 return *this;
130 template <class T> inline DocPtr<T> &DocPtr<T>::operator=(T *optr)
132 if (optr)
133 optr->selfOnlyRef();
134 if (T *ptr = m_ptr)
135 ptr->selfOnlyDeref();
136 m_ptr = optr;
137 return *this;
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)
147 return a.get() == b;
150 template <class T> inline bool operator==(const T *a, const DocPtr<T> &b)
152 return a == b.get();
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)
162 return a.get() != b;
165 template <class T> inline bool operator!=(const T *a, const DocPtr<T> &b)
167 return a != b.get();
171 } // namespace
173 #endif