00001 // Filename: pointerTo.I 00002 // Created by: drose (10Feb99) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: PointerToBase::Constructor 00022 // Access: Protected 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 template<class T> 00026 INLINE PointerToBase<T>:: 00027 PointerToBase(To *ptr) { 00028 _ptr = (To *)NULL; 00029 reassign(ptr); 00030 } 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: PointerToBase::Copy Constructor 00034 // Access: Protected 00035 // Description: 00036 //////////////////////////////////////////////////////////////////// 00037 template<class T> 00038 INLINE PointerToBase<T>:: 00039 PointerToBase(const PointerToBase<T> ©) { 00040 _ptr = (To *)NULL; 00041 reassign(copy); 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: PointerToBase::Destructor 00046 // Access: Protected 00047 // Description: 00048 //////////////////////////////////////////////////////////////////// 00049 template<class T> 00050 INLINE PointerToBase<T>:: 00051 ~PointerToBase() { 00052 reassign((To *)NULL); 00053 } 00054 00055 //////////////////////////////////////////////////////////////////// 00056 // Function: PointerToBase::reassign 00057 // Access: Protected 00058 // Description: This is the main work of the PointerTo family. When 00059 // the pointer is reassigned, decrement the old 00060 // reference count and increment the new one. 00061 //////////////////////////////////////////////////////////////////// 00062 template<class T> 00063 void PointerToBase<T>:: 00064 reassign(To *ptr) { 00065 if (ptr != _ptr) { 00066 // First save the old pointer; we won't delete it until we have 00067 // assigned the new one. We do this just in case there are 00068 // cascading effects from deleting this pointer that might 00069 // inadvertently delete the new one. (Don't laugh--it's 00070 // happened!) 00071 To *old_ptr = _ptr; 00072 00073 _ptr = ptr; 00074 if (_ptr != (To *)NULL) { 00075 _ptr->ref(); 00076 #ifdef DO_MEMORY_USAGE 00077 if (MemoryUsage::get_track_memory_usage()) { 00078 // Make sure the MemoryUsage record knows what the TypeHandle 00079 // is, if we know it ourselves. 00080 TypeHandle type = get_type_handle(To); 00081 if (type == TypeHandle::none()) { 00082 do_init_type(To); 00083 type = get_type_handle(To); 00084 } 00085 if (type != TypeHandle::none()) { 00086 MemoryUsage::update_type(_ptr, type); 00087 } 00088 } 00089 #endif 00090 } 00091 00092 // Now delete the old pointer. 00093 if (old_ptr != (To *)NULL) { 00094 unref_delete(old_ptr); 00095 } 00096 } 00097 } 00098 00099 //////////////////////////////////////////////////////////////////// 00100 // Function: PointerToBase::reassign 00101 // Access: Protected 00102 // Description: 00103 //////////////////////////////////////////////////////////////////// 00104 template<class T> 00105 INLINE void PointerToBase<T>:: 00106 reassign(const PointerToBase<To> ©) { 00107 reassign(copy._ptr); 00108 } 00109 00110 #ifndef CPPPARSER 00111 #ifndef WIN32_VC 00112 //////////////////////////////////////////////////////////////////// 00113 // Function: PointerToBase::Equivalence operator 00114 // Access: Public 00115 // Description: 00116 //////////////////////////////////////////////////////////////////// 00117 template<class T> 00118 INLINE bool PointerToBase<T>:: 00119 operator == (const To *other) const { 00120 return _ptr == other; 00121 } 00122 00123 //////////////////////////////////////////////////////////////////// 00124 // Function: PointerToBase::Nonequivalence operator 00125 // Access: Public 00126 // Description: 00127 //////////////////////////////////////////////////////////////////// 00128 template<class T> 00129 INLINE bool PointerToBase<T>:: 00130 operator != (const To *other) const { 00131 return _ptr != other; 00132 } 00133 00134 //////////////////////////////////////////////////////////////////// 00135 // Function: PointerToBase::Greater-than operator 00136 // Access: Public 00137 // Description: 00138 //////////////////////////////////////////////////////////////////// 00139 template<class T> 00140 INLINE bool PointerToBase<T>:: 00141 operator > (const To *other) const { 00142 return _ptr > other; 00143 } 00144 00145 //////////////////////////////////////////////////////////////////// 00146 // Function: PointerToBase::Less-than-or-equal operator 00147 // Access: Public 00148 // Description: 00149 //////////////////////////////////////////////////////////////////// 00150 template<class T> 00151 INLINE bool PointerToBase<T>:: 00152 operator <= (const To *other) const { 00153 return _ptr <= other; 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: PointerToBase::Greater-than-or-equal operator 00158 // Access: Public 00159 // Description: 00160 //////////////////////////////////////////////////////////////////// 00161 template<class T> 00162 INLINE bool PointerToBase<T>:: 00163 operator >= (const To *other) const { 00164 return _ptr >= other; 00165 } 00166 //////////////////////////////////////////////////////////////////// 00167 // Function: PointerToBase::Equivalence operator 00168 // Access: Public 00169 // Description: 00170 //////////////////////////////////////////////////////////////////// 00171 template<class T> 00172 INLINE bool PointerToBase<T>:: 00173 operator == (To *other) const { 00174 return _ptr == other; 00175 } 00176 00177 //////////////////////////////////////////////////////////////////// 00178 // Function: PointerToBase::Nonequivalence operator 00179 // Access: Public 00180 // Description: 00181 //////////////////////////////////////////////////////////////////// 00182 template<class T> 00183 INLINE bool PointerToBase<T>:: 00184 operator != (To *other) const { 00185 return _ptr != other; 00186 } 00187 00188 //////////////////////////////////////////////////////////////////// 00189 // Function: PointerToBase::Greater-than operator 00190 // Access: Public 00191 // Description: 00192 //////////////////////////////////////////////////////////////////// 00193 template<class T> 00194 INLINE bool PointerToBase<T>:: 00195 operator > (To *other) const { 00196 return _ptr > other; 00197 } 00198 00199 //////////////////////////////////////////////////////////////////// 00200 // Function: PointerToBase::Less-than-or-equal operator 00201 // Access: Public 00202 // Description: 00203 //////////////////////////////////////////////////////////////////// 00204 template<class T> 00205 INLINE bool PointerToBase<T>:: 00206 operator <= (To *other) const { 00207 return _ptr <= other; 00208 } 00209 00210 //////////////////////////////////////////////////////////////////// 00211 // Function: PointerToBase::Greater-than-or-equal operator 00212 // Access: Public 00213 // Description: 00214 //////////////////////////////////////////////////////////////////// 00215 template<class T> 00216 INLINE bool PointerToBase<T>:: 00217 operator >= (To *other) const { 00218 return _ptr >= other; 00219 } 00220 00221 //////////////////////////////////////////////////////////////////// 00222 // Function: PointerToBase::Equivalence operator 00223 // Access: Public 00224 // Description: 00225 //////////////////////////////////////////////////////////////////// 00226 template<class T> 00227 INLINE bool PointerToBase<T>:: 00228 operator == (const PointerToBase<To> &other) const { 00229 return _ptr == other._ptr; 00230 } 00231 00232 //////////////////////////////////////////////////////////////////// 00233 // Function: PointerToBase::Nonequivalence operator 00234 // Access: Public 00235 // Description: 00236 //////////////////////////////////////////////////////////////////// 00237 template<class T> 00238 INLINE bool PointerToBase<T>:: 00239 operator != (const PointerToBase<To> &other) const { 00240 return _ptr != other._ptr; 00241 } 00242 00243 //////////////////////////////////////////////////////////////////// 00244 // Function: PointerToBase::Greater-than operator 00245 // Access: Public 00246 // Description: 00247 //////////////////////////////////////////////////////////////////// 00248 template<class T> 00249 INLINE bool PointerToBase<T>:: 00250 operator > (const PointerToBase<To> &other) const { 00251 return _ptr > other._ptr; 00252 } 00253 00254 //////////////////////////////////////////////////////////////////// 00255 // Function: PointerToBase::Less-than-or-equal operator 00256 // Access: Public 00257 // Description: 00258 //////////////////////////////////////////////////////////////////// 00259 template<class T> 00260 INLINE bool PointerToBase<T>:: 00261 operator <= (const PointerToBase<To> &other) const { 00262 return _ptr <= other._ptr; 00263 } 00264 00265 //////////////////////////////////////////////////////////////////// 00266 // Function: PointerToBase::Greater-than-or-equal operator 00267 // Access: Public 00268 // Description: 00269 //////////////////////////////////////////////////////////////////// 00270 template<class T> 00271 INLINE bool PointerToBase<T>:: 00272 operator >= (const PointerToBase<To> &other) const { 00273 return _ptr >= other._ptr; 00274 } 00275 #endif // WIN32_VC 00276 00277 //////////////////////////////////////////////////////////////////// 00278 // Function: PointerToBase::Less-than operator 00279 // Access: Public 00280 // Description: 00281 //////////////////////////////////////////////////////////////////// 00282 template<class T> 00283 INLINE bool PointerToBase<T>:: 00284 operator < (const To *other) const { 00285 return _ptr < other; 00286 } 00287 00288 //////////////////////////////////////////////////////////////////// 00289 // Function: PointerToBase::Less-than operator 00290 // Access: Public 00291 // Description: 00292 //////////////////////////////////////////////////////////////////// 00293 template<class T> 00294 INLINE bool PointerToBase<T>:: 00295 operator < (const PointerToBase<To> &other) const { 00296 return _ptr < other._ptr; 00297 } 00298 00299 #endif // CPPPARSER 00300 00301 00302 00303 //////////////////////////////////////////////////////////////////// 00304 // Function: PointerToBase::is_null 00305 // Access: Public 00306 // Description: Returns true if the PointerTo is a NULL pointer, 00307 // false otherwise. (Direct comparison to a NULL 00308 // pointer also works.) 00309 //////////////////////////////////////////////////////////////////// 00310 template<class T> 00311 INLINE bool PointerToBase<T>:: 00312 is_null() const { 00313 return (_ptr == NULL); 00314 } 00315 00316 //////////////////////////////////////////////////////////////////// 00317 // Function: PointerToBase::clear 00318 // Access: Public 00319 // Description: A convenient way to set the PointerTo object to NULL. 00320 // (Assignment to a NULL pointer also works, of course.) 00321 //////////////////////////////////////////////////////////////////// 00322 template<class T> 00323 INLINE void PointerToBase<T>:: 00324 clear() { 00325 reassign((To *)NULL); 00326 } 00327 00328 //////////////////////////////////////////////////////////////////// 00329 // Function: PointerToBase::output 00330 // Access: Public 00331 // Description: A handy function to output PointerTo's as a hex 00332 // pointer followed by a reference count. 00333 //////////////////////////////////////////////////////////////////// 00334 template<class T> 00335 INLINE void PointerToBase<T>:: 00336 output(ostream &out) const { 00337 out << (void *)_ptr; 00338 if (_ptr != (To *)NULL) { 00339 out << ":" << _ptr->get_ref_count(); 00340 } 00341 } 00342 00343 00344 //////////////////////////////////////////////////////////////////// 00345 // Function: PointerTo::Constructor 00346 // Access: Public 00347 // Description: 00348 //////////////////////////////////////////////////////////////////// 00349 template<class T> 00350 INLINE PointerTo<T>:: 00351 PointerTo(To *ptr) : PointerToBase<T>(ptr) { 00352 } 00353 00354 //////////////////////////////////////////////////////////////////// 00355 // Function: PointerTo::Copy Constructor 00356 // Access: Public 00357 // Description: 00358 //////////////////////////////////////////////////////////////////// 00359 template<class T> 00360 INLINE PointerTo<T>:: 00361 PointerTo(const PointerTo<T> ©) : 00362 PointerToBase<T>((const PointerToBase<T> &)copy) 00363 { 00364 } 00365 00366 //////////////////////////////////////////////////////////////////// 00367 // Function: PointerTo::Dereference operator 00368 // Access: Public 00369 // Description: 00370 //////////////////////////////////////////////////////////////////// 00371 template<class T> 00372 INLINE TYPENAME PointerTo<T>::To &PointerTo<T>:: 00373 operator *() const { 00374 return *_ptr; 00375 } 00376 00377 //////////////////////////////////////////////////////////////////// 00378 // Function: PointerTo::Member access operator 00379 // Access: Public 00380 // Description: 00381 //////////////////////////////////////////////////////////////////// 00382 template<class T> 00383 INLINE TYPENAME PointerTo<T>::To *PointerTo<T>:: 00384 operator -> () const { 00385 return _ptr; 00386 } 00387 00388 //////////////////////////////////////////////////////////////////// 00389 // Function: PointerTo::Typecast operator 00390 // Access: Public 00391 // Description: We also have the typecast operator to automatically 00392 // convert PointerTo's to the required kind of actual 00393 // pointer. This introduces ambiguities which the 00394 // compiler will resolve one way or the other, but we 00395 // don't care which way it goes because either will be 00396 // correct. 00397 //////////////////////////////////////////////////////////////////// 00398 template<class T> 00399 INLINE PointerTo<T>:: 00400 operator TYPENAME PointerToBase<T>::To *() const { 00401 return _ptr; 00402 } 00403 00404 //////////////////////////////////////////////////////////////////// 00405 // Function: PointerTo::p 00406 // Access: Public 00407 // Description: Returns an ordinary pointer instead of a PointerTo. 00408 // Useful to work around compiler problems, particularly 00409 // for implicit upcasts. 00410 //////////////////////////////////////////////////////////////////// 00411 template<class T> 00412 INLINE TYPENAME PointerTo<T>::To *PointerTo<T>:: 00413 p() const { 00414 return _ptr; 00415 } 00416 00417 //////////////////////////////////////////////////////////////////// 00418 // Function: PointerTo::Assignment operator 00419 // Access: Public 00420 // Description: 00421 //////////////////////////////////////////////////////////////////// 00422 template<class T> 00423 INLINE PointerTo<T> &PointerTo<T>:: 00424 operator = (To *ptr) { 00425 reassign(ptr); 00426 return *this; 00427 } 00428 00429 //////////////////////////////////////////////////////////////////// 00430 // Function: PointerTo::Assignment operator 00431 // Access: Public 00432 // Description: 00433 //////////////////////////////////////////////////////////////////// 00434 template<class T> 00435 INLINE PointerTo<T> &PointerTo<T>:: 00436 operator = (const PointerTo<T> ©) { 00437 reassign((const PointerToBase<T> &)copy); 00438 return *this; 00439 } 00440 00441 //////////////////////////////////////////////////////////////////// 00442 // Function: ConstPointerTo::Constructor 00443 // Access: Public 00444 // Description: 00445 //////////////////////////////////////////////////////////////////// 00446 template<class T> 00447 INLINE ConstPointerTo<T>:: 00448 ConstPointerTo(const To *ptr) : 00449 PointerToBase<T>((ConstPointerTo<T>::To *)ptr) 00450 { 00451 } 00452 00453 //////////////////////////////////////////////////////////////////// 00454 // Function: ConstPointerTo::Copy Constructor 00455 // Access: Public 00456 // Description: 00457 //////////////////////////////////////////////////////////////////// 00458 template<class T> 00459 INLINE ConstPointerTo<T>:: 00460 ConstPointerTo(const PointerTo<T> ©) : 00461 PointerToBase<T>((const PointerToBase<T> &)copy) 00462 { 00463 } 00464 00465 //////////////////////////////////////////////////////////////////// 00466 // Function: ConstPointerTo::Copy Constructor 00467 // Access: Public 00468 // Description: 00469 //////////////////////////////////////////////////////////////////// 00470 template<class T> 00471 INLINE ConstPointerTo<T>:: 00472 ConstPointerTo(const ConstPointerTo<T> ©) : 00473 PointerToBase<T>((const PointerToBase<T> &)copy) 00474 { 00475 } 00476 00477 //////////////////////////////////////////////////////////////////// 00478 // Function: ConstPointerTo::Dereference operator 00479 // Access: Public 00480 // Description: 00481 //////////////////////////////////////////////////////////////////// 00482 template<class T> 00483 INLINE const TYPENAME ConstPointerTo<T>::To &ConstPointerTo<T>:: 00484 operator *() const { 00485 return *_ptr; 00486 } 00487 00488 //////////////////////////////////////////////////////////////////// 00489 // Function: ConstPointerTo::Member access operator 00490 // Access: Public 00491 // Description: 00492 //////////////////////////////////////////////////////////////////// 00493 template<class T> 00494 INLINE const TYPENAME ConstPointerTo<T>::To *ConstPointerTo<T>:: 00495 operator -> () const { 00496 return _ptr; 00497 } 00498 00499 //////////////////////////////////////////////////////////////////// 00500 // Function: ConstPointerTo::Typecast operator 00501 // Access: Public 00502 // Description: We also have the typecast operator to automatically 00503 // convert ConstPointerTo's to the required kind of actual 00504 // pointer. This introduces ambiguities which the 00505 // compiler will resolve one way or the other, but we 00506 // don't care which way it goes because either will be 00507 // correct. 00508 //////////////////////////////////////////////////////////////////// 00509 00510 template<class T> 00511 INLINE ConstPointerTo<T>:: 00512 operator const TYPENAME PointerToBase<T>::To *() const { 00513 return _ptr; 00514 } 00515 00516 //////////////////////////////////////////////////////////////////// 00517 // Function: ConstPointerTo::p 00518 // Access: Public 00519 // Description: Returns an ordinary pointer instead of a ConstPointerTo. 00520 // Useful to work around compiler problems, particularly 00521 // for implicit upcasts. 00522 //////////////////////////////////////////////////////////////////// 00523 template<class T> 00524 INLINE const TYPENAME ConstPointerTo<T>::To *ConstPointerTo<T>:: 00525 p() const { 00526 return _ptr; 00527 } 00528 00529 //////////////////////////////////////////////////////////////////// 00530 // Function: ConstPointerTo::Assignment operator 00531 // Access: Public 00532 // Description: 00533 //////////////////////////////////////////////////////////////////// 00534 template<class T> 00535 INLINE ConstPointerTo<T> &ConstPointerTo<T>:: 00536 operator = (const To *ptr) { 00537 reassign((To *)ptr); 00538 return *this; 00539 } 00540 00541 //////////////////////////////////////////////////////////////////// 00542 // Function: ConstPointerTo::Assignment operator 00543 // Access: Public 00544 // Description: 00545 //////////////////////////////////////////////////////////////////// 00546 template<class T> 00547 INLINE ConstPointerTo<T> &ConstPointerTo<T>:: 00548 operator = (const ConstPointerTo<T> ©) { 00549 reassign((const PointerToBase<T> &)copy); 00550 return *this; 00551 } 00552 00553 //////////////////////////////////////////////////////////////////// 00554 // Function: ConstPointerTo::Assignment operator 00555 // Access: Public 00556 // Description: 00557 //////////////////////////////////////////////////////////////////// 00558 template<class T> 00559 INLINE ConstPointerTo<T> &ConstPointerTo<T>:: 00560 operator = (const PointerTo<T> ©) { 00561 reassign((const PointerToBase<T> &)copy); 00562 return *this; 00563 }