KHtml

shared.h
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 <[email protected]>
5  *
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.
10  *
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.
15  *
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.
20  *
21  */
22 
23 #ifndef SHARED_H
24 #define SHARED_H
25 
26 #include <wtf/SharedPtr.h>
27 
28 namespace khtml
29 {
30 
31 template<class type> class Shared
32 {
33 public:
34  Shared()
35  {
36  _ref = 0; /*counter++;*/
37  }
38  ~Shared()
39  {
40  /*counter--;*/
41  }
42 
43  void ref()
44  {
45  _ref++;
46  }
47  void deref()
48  {
49  if (_ref) {
50  _ref--;
51  }
52  if (!_ref) {
53  delete static_cast<type *>(this);
54  }
55  }
56  bool hasOneRef() //qCDebug(KHTML_LOG) << "ref=" << _ref;
57  {
58  return _ref == 1;
59  }
60 
61  int refCount() const
62  {
63  return _ref;
64  }
65 // static int counter;
66 protected:
67  unsigned int _ref;
68 
69 private:
70  //Avoid the automatic copy constructor
71  Shared(const Shared &);
72  Shared &operator= (const Shared &);
73 };
74 
75 template<class type> class TreeShared
76 {
77 public:
78  TreeShared()
79  {
80  _ref = 0;
81  m_parent = nullptr; /*counter++;*/
82  }
83  TreeShared(type *parent)
84  {
85  _ref = 0;
86  m_parent = parent; /*counter++;*/
87  }
88  virtual ~TreeShared()
89  {
90  /*counter--;*/
91  }
92 
93  virtual void removedLastRef()
94  {
95  delete static_cast<type *>(this);
96  }
97 
98  void ref()
99  {
100  _ref++;
101  }
102  void deref()
103  {
104  if (_ref) {
105  _ref--;
106  }
107  if (!_ref && !m_parent) {
108  removedLastRef();
109  }
110  }
111 
112  // This method decreases the reference count, but never deletes the object.
113  // It should be used when a method may be passed something with the initial
114  // reference count of 0, but still needs to guard it.
115  void derefOnly()
116  {
117  _ref--;
118  }
119 
120  bool hasOneRef() //qCDebug(KHTML_LOG) << "ref=" << _ref;
121  {
122  return _ref == 1;
123  }
124 
125  int refCount() const
126  {
127  return _ref;
128  }
129 // static int counter;
130 
131  void setParent(type *parent)
132  {
133  m_parent = parent;
134  }
135  type *parent() const
136  {
137  return m_parent;
138  }
139 private:
140  //Avoid the automatic copy constructor
141  TreeShared(const TreeShared &);
142  TreeShared &operator= (const TreeShared &);
143 
144  unsigned int _ref;
145 protected:
146  type *m_parent;
147 };
148 
149 //A special pointer for nodes keeping track of the document,
150 //which helps distinguish back links from them to it, in order to break
151 //cycles
152 template <class T> class DocPtr
153 {
154 public:
155  DocPtr() : m_ptr(0) {}
156  DocPtr(T *ptr) : m_ptr(ptr)
157  {
158  if (ptr) {
159  ptr->selfOnlyRef();
160  }
161  }
162  DocPtr(const DocPtr &o) : m_ptr(o.m_ptr)
163  {
164  if (T *ptr = m_ptr) {
165  ptr->selfOnlyRef();
166  }
167  }
168  ~DocPtr()
169  {
170  if (T *ptr = m_ptr) {
171  ptr->selfOnlyDeref();
172  }
173  }
174 
175  template <class U> DocPtr(const DocPtr<U> &o) : m_ptr(o.get())
176  {
177  if (T *ptr = m_ptr) {
178  ptr->selfOnlyRef();
179  }
180  }
181 
182  void resetSkippingRef(T *o)
183  {
184  m_ptr = o;
185  }
186 
187  T *get() const
188  {
189  return m_ptr;
190  }
191 
192  T &operator*() const
193  {
194  return *m_ptr;
195  }
196  T *operator->() const
197  {
198  return m_ptr;
199  }
200 
201  bool operator!() const
202  {
203  return !m_ptr;
204  }
205 
206  // this type conversion operator allows implicit conversion to
207  // bool but not to other integer types
208 
209  typedef T *(DocPtr::*UnspecifiedBoolType)() const;
210  operator UnspecifiedBoolType() const
211  {
212  return m_ptr ? &DocPtr::get : nullptr;
213  }
214 
215  DocPtr &operator=(const DocPtr &);
216  DocPtr &operator=(T *);
217 
218 private:
219  T *m_ptr;
220 };
221 
222 template <class T> DocPtr<T> &DocPtr<T>::operator=(const DocPtr<T> &o)
223 {
224  T *optr = o.m_ptr;
225  if (optr) {
226  optr->selfOnlyRef();
227  }
228  if (T *ptr = m_ptr) {
229  ptr->selfOnlyDeref();
230  }
231  m_ptr = optr;
232  return *this;
233 }
234 
235 template <class T> inline DocPtr<T> &DocPtr<T>::operator=(T *optr)
236 {
237  if (optr) {
238  optr->selfOnlyRef();
239  }
240  if (T *ptr = m_ptr) {
241  ptr->selfOnlyDeref();
242  }
243  m_ptr = optr;
244  return *this;
245 }
246 
247 template <class T> inline bool operator==(const DocPtr<T> &a, const DocPtr<T> &b)
248 {
249  return a.get() == b.get();
250 }
251 
252 template <class T> inline bool operator==(const DocPtr<T> &a, const T *b)
253 {
254  return a.get() == b;
255 }
256 
257 template <class T> inline bool operator==(const T *a, const DocPtr<T> &b)
258 {
259  return a == b.get();
260 }
261 
262 template <class T> inline bool operator!=(const DocPtr<T> &a, const DocPtr<T> &b)
263 {
264  return a.get() != b.get();
265 }
266 
267 template <class T> inline bool operator!=(const DocPtr<T> &a, const T *b)
268 {
269  return a.get() != b;
270 }
271 
272 template <class T> inline bool operator!=(const T *a, const DocPtr<T> &b)
273 {
274  return a != b.get();
275 }
276 
277 } // namespace
278 
279 #endif
This file is part of the HTML rendering engine for KDE.
void ref()
bool operator==(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
void deref()
Type type(const QSqlDatabase &db)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
bool operator!=(const Qt3DRender::QGraphicsApiFilter &reference, const Qt3DRender::QGraphicsApiFilter &sample)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.