1 | // -*- mode: c++; c-basic-offset: 4 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 2005, 2006 Apple Computer, Inc.
|
---|
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 KXMLCORE_REF_PTR_H
|
---|
24 | #define KXMLCORE_REF_PTR_H
|
---|
25 |
|
---|
26 | #include <algorithm>
|
---|
27 |
|
---|
28 | namespace WTF {
|
---|
29 |
|
---|
30 | template <typename T> class PassRefPtr;
|
---|
31 |
|
---|
32 | template <typename T> class RefPtr
|
---|
33 | {
|
---|
34 | public:
|
---|
35 | RefPtr() : m_ptr(0) {}
|
---|
36 | RefPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
|
---|
37 | RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
|
---|
38 | // see comment in PassRefPtr.h for why this takes const reference
|
---|
39 | template <typename U> RefPtr(const PassRefPtr<U>&);
|
---|
40 |
|
---|
41 | ~RefPtr() { if (T *ptr = m_ptr) ptr->deref(); }
|
---|
42 |
|
---|
43 | template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }
|
---|
44 |
|
---|
45 | T *get() const { return m_ptr; }
|
---|
46 |
|
---|
47 | PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
|
---|
48 |
|
---|
49 | T& operator*() const { return *m_ptr; }
|
---|
50 | T *operator->() const { return m_ptr; }
|
---|
51 |
|
---|
52 | bool operator!() const { return !m_ptr; }
|
---|
53 |
|
---|
54 | // This conversion operator allows implicit conversion to bool but not to other integer types.
|
---|
55 | typedef T * (RefPtr::*UnspecifiedBoolType)() const;
|
---|
56 | operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::get : 0; }
|
---|
57 |
|
---|
58 | RefPtr& operator=(const RefPtr&);
|
---|
59 | RefPtr& operator=(T *);
|
---|
60 | RefPtr& operator=(const PassRefPtr<T>&);
|
---|
61 | template <typename U> RefPtr& operator=(const RefPtr<U>&);
|
---|
62 | template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
|
---|
63 |
|
---|
64 | void swap(RefPtr&);
|
---|
65 |
|
---|
66 | private:
|
---|
67 | T *m_ptr;
|
---|
68 | };
|
---|
69 |
|
---|
70 | template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
|
---|
71 | : m_ptr(o.release())
|
---|
72 | {
|
---|
73 | }
|
---|
74 |
|
---|
75 | template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
|
---|
76 | {
|
---|
77 | T* optr = o.get();
|
---|
78 | if (optr)
|
---|
79 | optr->ref();
|
---|
80 | T* ptr = m_ptr;
|
---|
81 | m_ptr = optr;
|
---|
82 | if (ptr)
|
---|
83 | ptr->deref();
|
---|
84 | return *this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
|
---|
88 | {
|
---|
89 | T* optr = o.get();
|
---|
90 | if (optr)
|
---|
91 | optr->ref();
|
---|
92 | T* ptr = m_ptr;
|
---|
93 | m_ptr = optr;
|
---|
94 | if (ptr)
|
---|
95 | ptr->deref();
|
---|
96 | return *this;
|
---|
97 | }
|
---|
98 |
|
---|
99 | template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
|
---|
100 | {
|
---|
101 | if (optr)
|
---|
102 | optr->ref();
|
---|
103 | T* ptr = m_ptr;
|
---|
104 | m_ptr = optr;
|
---|
105 | if (ptr)
|
---|
106 | ptr->deref();
|
---|
107 | return *this;
|
---|
108 | }
|
---|
109 |
|
---|
110 | template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
|
---|
111 | {
|
---|
112 | T* ptr = m_ptr;
|
---|
113 | m_ptr = o.release();
|
---|
114 | if (ptr)
|
---|
115 | ptr->deref();
|
---|
116 | return *this;
|
---|
117 | }
|
---|
118 |
|
---|
119 | template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
|
---|
120 | {
|
---|
121 | T* ptr = m_ptr;
|
---|
122 | m_ptr = o.release();
|
---|
123 | if (ptr)
|
---|
124 | ptr->deref();
|
---|
125 | return *this;
|
---|
126 | }
|
---|
127 |
|
---|
128 | template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
|
---|
129 | {
|
---|
130 | std::swap(m_ptr, o.m_ptr);
|
---|
131 | }
|
---|
132 |
|
---|
133 | template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
|
---|
134 | {
|
---|
135 | a.swap(b);
|
---|
136 | }
|
---|
137 |
|
---|
138 | template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
|
---|
139 | {
|
---|
140 | return a.get() == b.get();
|
---|
141 | }
|
---|
142 |
|
---|
143 | template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
|
---|
144 | {
|
---|
145 | return a.get() == b;
|
---|
146 | }
|
---|
147 |
|
---|
148 | template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
|
---|
149 | {
|
---|
150 | return a == b.get();
|
---|
151 | }
|
---|
152 |
|
---|
153 | template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
|
---|
154 | {
|
---|
155 | return a.get() != b.get();
|
---|
156 | }
|
---|
157 |
|
---|
158 | template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
|
---|
159 | {
|
---|
160 | return a.get() != b;
|
---|
161 | }
|
---|
162 |
|
---|
163 | template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
|
---|
164 | {
|
---|
165 | return a != b.get();
|
---|
166 | }
|
---|
167 |
|
---|
168 | template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
|
---|
169 | {
|
---|
170 | return RefPtr<T>(static_cast<T *>(p.get()));
|
---|
171 | }
|
---|
172 |
|
---|
173 | template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
|
---|
174 | {
|
---|
175 | return RefPtr<T>(const_cast<T *>(p.get()));
|
---|
176 | }
|
---|
177 |
|
---|
178 | } // namespace WTF
|
---|
179 |
|
---|
180 | using WTF::RefPtr;
|
---|
181 | using WTF::static_pointer_cast;
|
---|
182 | using WTF::const_pointer_cast;
|
---|
183 |
|
---|
184 | #endif // KXMLCORE_REF_PTR_H
|
---|