]>
Commit | Line | Data |
---|---|---|
a90939db JF |
1 | /* |
2 | * This file is part of the DOM implementation for KDE. | |
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 DocPtr_h | |
23 | #define DocPtr_h | |
24 | ||
25 | namespace WebCore { | |
26 | ||
27 | template <class T> class DocPtr { | |
28 | public: | |
29 | DocPtr() : m_ptr(0) {} | |
30 | DocPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->selfOnlyRef(); } | |
31 | DocPtr(const DocPtr &o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->selfOnlyRef(); } | |
32 | ~DocPtr() { if (T *ptr = m_ptr) ptr->selfOnlyDeref(); } | |
33 | ||
34 | template <class U> DocPtr(const DocPtr<U> &o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->selfOnlyRef(); } | |
35 | ||
36 | void resetSkippingRef(T *o) { m_ptr = o; } | |
37 | ||
38 | T *get() const { return m_ptr; } | |
39 | ||
40 | T &operator*() const { return *m_ptr; } | |
41 | T *operator->() const { return m_ptr; } | |
42 | ||
43 | bool operator!() const { return !m_ptr; } | |
44 | ||
45 | // this type conversion operator allows implicit conversion to | |
46 | // bool but not to other integer types | |
47 | ||
48 | typedef T * (DocPtr::*UnspecifiedBoolType)() const; | |
49 | operator UnspecifiedBoolType() const | |
50 | { | |
51 | return m_ptr ? &DocPtr::get : 0; | |
52 | } | |
53 | ||
54 | DocPtr &operator=(const DocPtr &); | |
55 | DocPtr &operator=(T *); | |
56 | ||
57 | private: | |
58 | T *m_ptr; | |
59 | }; | |
60 | ||
61 | template <class T> DocPtr<T> &DocPtr<T>::operator=(const DocPtr<T> &o) | |
62 | { | |
63 | T *optr = o.m_ptr; | |
64 | if (optr) | |
65 | optr->selfOnlyRef(); | |
66 | if (T *ptr = m_ptr) | |
67 | ptr->selfOnlyDeref(); | |
68 | m_ptr = optr; | |
69 | return *this; | |
70 | } | |
71 | ||
72 | template <class T> inline DocPtr<T> &DocPtr<T>::operator=(T *optr) | |
73 | { | |
74 | if (optr) | |
75 | optr->selfOnlyRef(); | |
76 | if (T *ptr = m_ptr) | |
77 | ptr->selfOnlyDeref(); | |
78 | m_ptr = optr; | |
79 | return *this; | |
80 | } | |
81 | ||
82 | template <class T> inline bool operator==(const DocPtr<T> &a, const DocPtr<T> &b) | |
83 | { | |
84 | return a.get() == b.get(); | |
85 | } | |
86 | ||
87 | template <class T> inline bool operator==(const DocPtr<T> &a, const T *b) | |
88 | { | |
89 | return a.get() == b; | |
90 | } | |
91 | ||
92 | template <class T> inline bool operator==(const T *a, const DocPtr<T> &b) | |
93 | { | |
94 | return a == b.get(); | |
95 | } | |
96 | ||
97 | template <class T> inline bool operator!=(const DocPtr<T> &a, const DocPtr<T> &b) | |
98 | { | |
99 | return a.get() != b.get(); | |
100 | } | |
101 | ||
102 | template <class T> inline bool operator!=(const DocPtr<T> &a, const T *b) | |
103 | { | |
104 | return a.get() != b; | |
105 | } | |
106 | ||
107 | template <class T> inline bool operator!=(const T *a, const DocPtr<T> &b) | |
108 | { | |
109 | return a != b.get(); | |
110 | } | |
111 | ||
112 | } // namespace WebCore | |
113 | ||
114 | #endif // DocPtr_h |