1 #ifndef __DOMPTR_H__
2 #define __DOMPTR_H__
3 /**
4 * Phoebe DOM Implementation.
5 *
6 * This is a C++ approximation of the W3C DOM model, which follows
7 * fairly closely the specifications in the various .idl files, copies of
8 * which are provided for reference. Most important is this one:
9 *
10 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
11 *
12 * More thorough explanations of the various classes and their algorithms
13 * can be found there.
14 *
15 *
16 * Authors:
17 * Bob Jamison
18 *
19 * Copyright (C) 2006-2008 Bob Jamison
20 *
21 * This library is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU Lesser General Public
23 * License as published by the Free Software Foundation; either
24 * version 2.1 of the License, or (at your option) any later version.
25 *
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
30 *
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 *
35 * =======================================================================
36 * NOTES:
37 *
38 * Notice that many of the classes defined here are pure virtual. In other
39 * words, they are purely unimplemented interfaces. For the implementations
40 * of them, look in domimpl.h and domimpl.cpp.
41 *
42 * Also, note that there is a domptr.cpp file that has a couple of necessary
43 * functions which cannot be in a .h file
44 *
45 */
47 #include <functional>
49 namespace org
50 {
51 namespace w3c
52 {
53 namespace dom
54 {
58 /*#########################################################################
59 ## NodePtr
60 #########################################################################*/
62 /**
63 * A simple Smart Pointer class that handles Nodes and all of its
64 * descendants. This is very similar to shared_ptr, but it is customized
65 * to handle our needs.
66 */
67 template<class T> class Ptr
68 {
69 public:
71 /**
72 * Simple constructor
73 */
74 Ptr()
75 { _ref = 0; }
77 /**
78 * Constructor upon a reference
79 */
80 template<class Y> Ptr(const Ptr<Y> &other)
81 {
82 _ref = other._ref;
83 incrementRefCount(_ref);
84 }
86 /**
87 * Constructor upon a reference
88 */
89 Ptr(T * refArg, bool addRef = true)
90 {
91 _ref = refArg;
92 if(addRef)
93 incrementRefCount(_ref);
94 }
97 /**
98 * Copy constructor
99 */
100 Ptr(const Ptr &other)
101 {
102 _ref = other._ref;
103 incrementRefCount(_ref);
104 }
106 /**
107 * Destructor
108 */
109 virtual ~Ptr()
110 {
111 decrementRefCount(_ref);
112 }
115 /**
116 * Assignment operator
117 */
118 template<class Y> Ptr &operator=(const Ptr<Y> &other)
119 {
120 decrementRefCount(_ref);
121 _ref = other._ref;
122 incrementRefCount(_ref);
123 return *this;
124 }
126 /**
127 * Assignment operator
128 */
129 Ptr &operator=(const Ptr &other)
130 {
131 decrementRefCount(_ref);
132 _ref = other._ref;
133 incrementRefCount(_ref);
134 return *this;
135 }
137 /**
138 * Assignment operator
139 */
140 template<class Y> Ptr &operator=(Y * ref)
141 {
142 decrementRefCount(_ref);
143 _ref = ref;
144 incrementRefCount(_ref);
145 return *this;
146 }
148 /**
149 * Assignment operator
150 */
151 template<class Y> Ptr &operator=(const Y * ref)
152 {
153 decrementRefCount(_ref);
154 _ref = (Y *) ref;
155 incrementRefCount(_ref);
156 return *this;
157 }
159 /**
160 * Return the reference
161 */
162 T * get() const
163 {
164 return _ref;
165 }
167 /**
168 * Dereference operator
169 */
170 T &operator*() const
171 {
172 return *_ref;
173 }
175 /**
176 * Point-to operator
177 */
178 T *operator->() const
179 {
180 return _ref;
181 }
183 /**
184 * NOT bool operator. How to check if we are null without a comparison
185 */
186 bool operator! () const
187 {
188 return (_ref == 0);
189 }
191 /**
192 * Swap what I reference with the other guy
193 */
194 void swap(Ptr &other)
195 {
196 T *tmp = _ref;
197 _ref = other._ref;
198 other._ref = tmp;
199 }
201 //The referenced item
202 T *_ref;
203 };
206 /**
207 * Global definitions. Many of these are used to mimic behaviour of
208 * a real pointer
209 */
211 /**
212 * Equality
213 */
214 template<class T, class U> inline bool
215 operator==(const Ptr<T> &a, const Ptr<U> &b)
216 {
217 return a.get() == b.get();
218 }
220 /**
221 * Inequality
222 */
223 template<class T, class U> inline bool
224 operator!=(const Ptr<T> &a, const Ptr<U> &b)
225 {
226 return a.get() != b.get();
227 }
229 /**
230 * Equality
231 */
232 template<class T> inline bool
233 operator==(const Ptr<T> &a, T * b)
234 {
235 return a.get() == b;
236 }
238 /**
239 * Inequality
240 */
241 template<class T> inline bool
242 operator!=(const Ptr<T> &a, T * b)
243 {
244 return a.get() != b;
245 }
247 /**
248 * Equality
249 */
250 template<class T> inline bool
251 operator==(T * a, const Ptr<T> &b)
252 {
253 return a == b.get();
254 }
256 /**
257 * Inequality
258 */
259 template<class T> inline bool
260 operator!=(T * a, const Ptr<T> &b)
261 {
262 return a != b.get();
263 }
266 /**
267 * Less than
268 */
269 template<class T> inline bool
270 operator<(const Ptr<T> &a, const Ptr<T> &b)
271 {
272 return std::less<T *>()(a.get(), b.get());
273 }
275 /**
276 * Swap
277 */
278 template<class T> void
279 swap(Ptr<T> &a, Ptr<T> &b)
280 {
281 a.swap(b);
282 }
285 /**
286 * Get the pointer globally, for <algo>
287 */
288 template<class T> T *
289 get_pointer(const Ptr<T> &p)
290 {
291 return p.get();
292 }
294 /**
295 * Static cast
296 */
297 template<class T, class U> Ptr<T>
298 static_pointer_cast(const Ptr<U> &p)
299 {
300 return static_cast<T *>(p.get());
301 }
303 /**
304 * Const cast
305 */
306 template<class T, class U> Ptr<T>
307 const_pointer_cast(const Ptr<U> &p)
308 {
309 return const_cast<T *>(p.get());
310 }
312 /**
313 * Dynamic cast
314 */
315 template<class T, class U> Ptr<T>
316 dynamic_pointer_cast(const Ptr<U> &p)
317 {
318 return dynamic_cast<T *>(p.get());
319 }
323 } //namespace dom
324 } //namespace w3c
325 } //namespace org
328 #endif // __DOMPTR_H__
331 /*#########################################################################
332 ## E N D O F F I L E
333 #########################################################################*/