Panda3D
weakPointerToBase.I
1 // Filename: weakPointerToBase.I
2 // Created by: drose (27Sep04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: WeakPointerToBase::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 template<class T>
23 WeakPointerToBase(To *ptr) {
24  reassign(ptr);
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: WeakPointerToBase::Copy Constructor
29 // Access: Protected
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 template<class T>
35  reassign(copy);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: WeakPointerToBase::Copy Constructor
40 // Access: Protected
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 template<class T>
46  _void_ptr = copy._void_ptr;
47  _ptr_was_deleted = copy._ptr_was_deleted;
48 
49  if (is_valid_pointer()) {
50  To *ptr = (To *)_void_ptr;
51  ptr->weak_ref(this);
52  }
53 }
54 
55 ////////////////////////////////////////////////////////////////////
56 // Function: WeakPointerToBase::Destructor
57 // Access: Protected
58 // Description:
59 ////////////////////////////////////////////////////////////////////
60 template<class T>
63  reassign((To *)NULL);
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: WeakPointerToBase::reassign
68 // Access: Protected
69 // Description: This is the main work of the PointerTo family. When
70 // the pointer is reassigned, decrement the old
71 // reference count and increment the new one.
72 ////////////////////////////////////////////////////////////////////
73 template<class T>
75 reassign(To *ptr) {
76  if (ptr != (To *)_void_ptr || _ptr_was_deleted) {
77  To *old_ptr = (To *)_void_ptr;
78 
79  _void_ptr = (void *)ptr;
80  if (ptr != (To *)NULL) {
81  ptr->weak_ref(this);
82 #ifdef DO_MEMORY_USAGE
83  if (MemoryUsage::get_track_memory_usage()) {
84  // Make sure the MemoryUsage record knows what the TypeHandle
85  // is, if we know it ourselves.
86  TypeHandle type = get_type_handle(To);
87  if (type == TypeHandle::none()) {
88  do_init_type(To);
89  type = get_type_handle(To);
90  }
91  if (type != TypeHandle::none()) {
92  MemoryUsage::update_type(ptr, type);
93  }
94  }
95 #endif
96  }
97 
98  // Now remove the old reference.
99  if (old_ptr != (To *)NULL && !_ptr_was_deleted) {
100  old_ptr->weak_unref(this);
101  }
102 
103  _ptr_was_deleted = false;
104  }
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: WeakPointerToBase::reassign
109 // Access: Protected
110 // Description:
111 ////////////////////////////////////////////////////////////////////
112 template<class T>
113 INLINE void WeakPointerToBase<T>::
114 reassign(const PointerToBase<To> &copy) {
115  // This double-casting is a bit of a cheat to get around the
116  // inheritance issue--it's difficult to declare a template class to
117  // be a friend.
118  reassign((To *)((const WeakPointerToBase<To> *)&copy)->_void_ptr);
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: WeakPointerToBase::reassign
123 // Access: Protected
124 // Description:
125 ////////////////////////////////////////////////////////////////////
126 template<class T>
127 INLINE void WeakPointerToBase<T>::
128 reassign(const WeakPointerToBase<To> &copy) {
129  nassertv(!copy.was_deleted());
130  reassign((To *)copy._void_ptr);
131 }
132 
133 #ifndef CPPPARSER
134 #ifndef WIN32_VC
135 ////////////////////////////////////////////////////////////////////
136 // Function: WeakPointerToBase::Equivalence operator
137 // Access: Public
138 // Description:
139 ////////////////////////////////////////////////////////////////////
140 template<class T>
141 INLINE bool WeakPointerToBase<T>::
142 operator == (const To *other) const {
143  return (To *)_void_ptr == other;
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: WeakPointerToBase::Nonequivalence operator
148 // Access: Public
149 // Description:
150 ////////////////////////////////////////////////////////////////////
151 template<class T>
152 INLINE bool WeakPointerToBase<T>::
153 operator != (const To *other) const {
154  return (To *)_void_ptr != other;
155 }
156 
157 ////////////////////////////////////////////////////////////////////
158 // Function: WeakPointerToBase::Greater-than operator
159 // Access: Public
160 // Description:
161 ////////////////////////////////////////////////////////////////////
162 template<class T>
163 INLINE bool WeakPointerToBase<T>::
164 operator > (const To *other) const {
165  return (To *)_void_ptr > other;
166 }
167 
168 ////////////////////////////////////////////////////////////////////
169 // Function: WeakPointerToBase::Less-than-or-equal operator
170 // Access: Public
171 // Description:
172 ////////////////////////////////////////////////////////////////////
173 template<class T>
174 INLINE bool WeakPointerToBase<T>::
175 operator <= (const To *other) const {
176  return (To *)_void_ptr <= other;
177 }
178 
179 ////////////////////////////////////////////////////////////////////
180 // Function: WeakPointerToBase::Greater-than-or-equal operator
181 // Access: Public
182 // Description:
183 ////////////////////////////////////////////////////////////////////
184 template<class T>
185 INLINE bool WeakPointerToBase<T>::
186 operator >= (const To *other) const {
187  return (To *)_void_ptr >= other;
188 }
189 ////////////////////////////////////////////////////////////////////
190 // Function: WeakPointerToBase::Equivalence operator
191 // Access: Public
192 // Description:
193 ////////////////////////////////////////////////////////////////////
194 template<class T>
195 INLINE bool WeakPointerToBase<T>::
196 operator == (To *other) const {
197  return (To *)_void_ptr == other;
198 }
199 
200 ////////////////////////////////////////////////////////////////////
201 // Function: WeakPointerToBase::Nonequivalence operator
202 // Access: Public
203 // Description:
204 ////////////////////////////////////////////////////////////////////
205 template<class T>
206 INLINE bool WeakPointerToBase<T>::
207 operator != (To *other) const {
208  return (To *)_void_ptr != other;
209 }
210 
211 ////////////////////////////////////////////////////////////////////
212 // Function: WeakPointerToBase::Greater-than operator
213 // Access: Public
214 // Description:
215 ////////////////////////////////////////////////////////////////////
216 template<class T>
217 INLINE bool WeakPointerToBase<T>::
218 operator > (To *other) const {
219  return (To *)_void_ptr > other;
220 }
221 
222 ////////////////////////////////////////////////////////////////////
223 // Function: WeakPointerToBase::Less-than-or-equal operator
224 // Access: Public
225 // Description:
226 ////////////////////////////////////////////////////////////////////
227 template<class T>
228 INLINE bool WeakPointerToBase<T>::
229 operator <= (To *other) const {
230  return (To *)_void_ptr <= other;
231 }
232 
233 ////////////////////////////////////////////////////////////////////
234 // Function: WeakPointerToBase::Greater-than-or-equal operator
235 // Access: Public
236 // Description:
237 ////////////////////////////////////////////////////////////////////
238 template<class T>
239 INLINE bool WeakPointerToBase<T>::
240 operator >= (To *other) const {
241  return (To *)_void_ptr >= other;
242 }
243 
244 ////////////////////////////////////////////////////////////////////
245 // Function: WeakPointerToBase::Equivalence operator
246 // Access: Public
247 // Description:
248 ////////////////////////////////////////////////////////////////////
249 template<class T>
250 INLINE bool WeakPointerToBase<T>::
251 operator == (const WeakPointerToBase<To> &other) const {
252  return (To *)_void_ptr == (To *)other._void_ptr;
253 }
254 
255 ////////////////////////////////////////////////////////////////////
256 // Function: WeakPointerToBase::Nonequivalence operator
257 // Access: Public
258 // Description:
259 ////////////////////////////////////////////////////////////////////
260 template<class T>
261 INLINE bool WeakPointerToBase<T>::
262 operator != (const WeakPointerToBase<To> &other) const {
263  return (To *)_void_ptr != (To *)other._void_ptr;
264 }
265 
266 ////////////////////////////////////////////////////////////////////
267 // Function: WeakPointerToBase::Greater-than operator
268 // Access: Public
269 // Description:
270 ////////////////////////////////////////////////////////////////////
271 template<class T>
272 INLINE bool WeakPointerToBase<T>::
273 operator > (const WeakPointerToBase<To> &other) const {
274  return (To *)_void_ptr > (To *)other._void_ptr;
275 }
276 
277 ////////////////////////////////////////////////////////////////////
278 // Function: WeakPointerToBase::Less-than-or-equal operator
279 // Access: Public
280 // Description:
281 ////////////////////////////////////////////////////////////////////
282 template<class T>
283 INLINE bool WeakPointerToBase<T>::
284 operator <= (const WeakPointerToBase<To> &other) const {
285  return (To *)_void_ptr <= (To *)other._void_ptr;
286 }
287 
288 ////////////////////////////////////////////////////////////////////
289 // Function: WeakPointerToBase::Greater-than-or-equal operator
290 // Access: Public
291 // Description:
292 ////////////////////////////////////////////////////////////////////
293 template<class T>
294 INLINE bool WeakPointerToBase<T>::
295 operator >= (const WeakPointerToBase<To> &other) const {
296  return (To *)_void_ptr >= (To *)other._void_ptr;
297 }
298 
299 ////////////////////////////////////////////////////////////////////
300 // Function: WeakPointerToBase::Equivalence operator
301 // Access: Public
302 // Description:
303 ////////////////////////////////////////////////////////////////////
304 template<class T>
305 INLINE bool WeakPointerToBase<T>::
306 operator == (const PointerToBase<To> &other) const {
307  return (To *)_void_ptr == (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
308 }
309 
310 ////////////////////////////////////////////////////////////////////
311 // Function: WeakPointerToBase::Nonequivalence operator
312 // Access: Public
313 // Description:
314 ////////////////////////////////////////////////////////////////////
315 template<class T>
316 INLINE bool WeakPointerToBase<T>::
317 operator != (const PointerToBase<To> &other) const {
318  return (To *)_void_ptr != (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
319 }
320 
321 ////////////////////////////////////////////////////////////////////
322 // Function: WeakPointerToBase::Greater-than operator
323 // Access: Public
324 // Description:
325 ////////////////////////////////////////////////////////////////////
326 template<class T>
327 INLINE bool WeakPointerToBase<T>::
328 operator > (const PointerToBase<To> &other) const {
329  return (To *)_void_ptr > (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
330 }
331 
332 ////////////////////////////////////////////////////////////////////
333 // Function: WeakPointerToBase::Less-than-or-equal operator
334 // Access: Public
335 // Description:
336 ////////////////////////////////////////////////////////////////////
337 template<class T>
338 INLINE bool WeakPointerToBase<T>::
339 operator <= (const PointerToBase<To> &other) const {
340  return (To *)_void_ptr <= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
341 }
342 
343 ////////////////////////////////////////////////////////////////////
344 // Function: WeakPointerToBase::Greater-than-or-equal operator
345 // Access: Public
346 // Description:
347 ////////////////////////////////////////////////////////////////////
348 template<class T>
349 INLINE bool WeakPointerToBase<T>::
350 operator >= (const PointerToBase<To> &other) const {
351  return (To *)_void_ptr >= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
352 }
353 #endif // WIN32_VC
354 
355 ////////////////////////////////////////////////////////////////////
356 // Function: WeakPointerToBase::Less-than operator
357 // Access: Public
358 // Description:
359 ////////////////////////////////////////////////////////////////////
360 template<class T>
361 INLINE bool WeakPointerToBase<T>::
362 operator < (const To *other) const {
363  return (To *)_void_ptr < other;
364 }
365 
366 ////////////////////////////////////////////////////////////////////
367 // Function: WeakPointerToBase::Less-than operator
368 // Access: Public
369 // Description:
370 ////////////////////////////////////////////////////////////////////
371 template<class T>
372 INLINE bool WeakPointerToBase<T>::
373 operator < (const WeakPointerToBase<To> &other) const {
374  return (To *)_void_ptr < (To *)other._void_ptr;
375 }
376 
377 ////////////////////////////////////////////////////////////////////
378 // Function: WeakPointerToBase::Less-than operator
379 // Access: Public
380 // Description:
381 ////////////////////////////////////////////////////////////////////
382 template<class T>
383 INLINE bool WeakPointerToBase<T>::
384 operator < (const PointerToBase<To> &other) const {
385  return (To *)_void_ptr < (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
386 }
387 
388 #endif // CPPPARSER
389 
390 
391 
392 ////////////////////////////////////////////////////////////////////
393 // Function: WeakPointerToBase::clear
394 // Access: Published
395 // Description: A convenient way to set the PointerTo object to NULL.
396 // (Assignment to a NULL pointer also works, of course.)
397 ////////////////////////////////////////////////////////////////////
398 template<class T>
399 INLINE void WeakPointerToBase<T>::
400 clear() {
401  reassign((To *)NULL);
402 }
403 
404 ////////////////////////////////////////////////////////////////////
405 // Function: WeakPointerToBase::refresh
406 // Access: Published
407 // Description: Informs the WeakPointerTo object that its pointer is
408 // no longer deleted. This may be used after a
409 // WeakPointerTo has deleted a deleted pointer, and then
410 // a new pointer has been reallocated. It's equivalent
411 // to simply reassigning the pointer to its new
412 // (i.e. original) value, but has the advantage that it
413 // is const, so can be used for WeakPointers used as
414 // keys in STL maps and sets.
415 ////////////////////////////////////////////////////////////////////
416 template<class T>
417 INLINE void WeakPointerToBase<T>::
418 refresh() const {
419  ((WeakPointerToBase<T> *)this)->reassign((To *)_void_ptr);
420 }
421 
422 ////////////////////////////////////////////////////////////////////
423 // Function: WeakPointerToBase::output
424 // Access: Published
425 // Description: A handy function to output PointerTo's as a hex
426 // pointer followed by a reference count.
427 ////////////////////////////////////////////////////////////////////
428 template<class T>
429 INLINE void WeakPointerToBase<T>::
430 output(ostream &out) const {
431  out << _void_ptr;
432  if (was_deleted()) {
433  out << ":deleted";
434  } else if (_void_ptr != (void *)NULL) {
435  out << ":" << ((To *)_void_ptr)->get_ref_count();
436  }
437 }
void refresh() const
Informs the WeakPointerTo object that its pointer is no longer deleted.
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
void clear()
A convenient way to set the PointerTo object to NULL.
This is the base class for PointerTo and ConstPointerTo.
Definition: pointerToBase.h:32
bool was_deleted() const
Returns true if the object we are pointing to has been deleted, false otherwise.
void output(ostream &out) const
A handy function to output PointerTo&#39;s as a hex pointer followed by a reference count.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
This is the base class for PointerTo and ConstPointerTo.