Panda3D

weakPointerToBase.I

00001 // Filename: weakPointerToBase.I
00002 // Created by:  drose (27Sep04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 
00016 ////////////////////////////////////////////////////////////////////
00017 //     Function: WeakPointerToBase::Constructor
00018 //       Access: Protected
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 template<class T>
00022 INLINE WeakPointerToBase<T>::
00023 WeakPointerToBase(To *ptr) {
00024   reassign(ptr);
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: WeakPointerToBase::Copy Constructor
00029 //       Access: Protected
00030 //  Description:
00031 ////////////////////////////////////////////////////////////////////
00032 template<class T>
00033 INLINE WeakPointerToBase<T>::
00034 WeakPointerToBase(const PointerToBase<T> &copy) {
00035   reassign(copy);
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: WeakPointerToBase::Copy Constructor
00040 //       Access: Protected
00041 //  Description:
00042 ////////////////////////////////////////////////////////////////////
00043 template<class T>
00044 INLINE WeakPointerToBase<T>::
00045 WeakPointerToBase(const WeakPointerToBase<T> &copy) {
00046   reassign(copy);
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: WeakPointerToBase::Destructor
00051 //       Access: Protected
00052 //  Description:
00053 ////////////////////////////////////////////////////////////////////
00054 template<class T>
00055 INLINE WeakPointerToBase<T>::
00056 ~WeakPointerToBase() {
00057   reassign((To *)NULL);
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: WeakPointerToBase::reassign
00062 //       Access: Protected
00063 //  Description: This is the main work of the PointerTo family.  When
00064 //               the pointer is reassigned, decrement the old
00065 //               reference count and increment the new one.
00066 ////////////////////////////////////////////////////////////////////
00067 template<class T>
00068 void WeakPointerToBase<T>::
00069 reassign(To *ptr) {
00070   if (ptr != (To *)_void_ptr || _ptr_was_deleted) {
00071     To *old_ptr = (To *)_void_ptr;
00072 
00073     _void_ptr = (void *)ptr;
00074     if (ptr != (To *)NULL) {
00075       ptr->weak_ref(this);
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 remove the old reference.
00093     if (old_ptr != (To *)NULL && !_ptr_was_deleted) {
00094       old_ptr->weak_unref(this);
00095     }
00096 
00097     _ptr_was_deleted = false;
00098   }
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //     Function: WeakPointerToBase::reassign
00103 //       Access: Protected
00104 //  Description:
00105 ////////////////////////////////////////////////////////////////////
00106 template<class T>
00107 INLINE void WeakPointerToBase<T>::
00108 reassign(const PointerToBase<To> &copy) {
00109   // This double-casting is a bit of a cheat to get around the
00110   // inheritance issue--it's difficult to declare a template class to
00111   // be a friend.
00112   reassign((To *)((const WeakPointerToBase<To> *)&copy)->_void_ptr);
00113 }
00114 
00115 ////////////////////////////////////////////////////////////////////
00116 //     Function: WeakPointerToBase::reassign
00117 //       Access: Protected
00118 //  Description:
00119 ////////////////////////////////////////////////////////////////////
00120 template<class T>
00121 INLINE void WeakPointerToBase<T>::
00122 reassign(const WeakPointerToBase<To> &copy) {
00123   nassertv(!copy.was_deleted());
00124   reassign((To *)copy._void_ptr);
00125 }
00126 
00127 #ifndef CPPPARSER
00128 #ifndef WIN32_VC
00129 ////////////////////////////////////////////////////////////////////
00130 //     Function: WeakPointerToBase::Equivalence operator
00131 //       Access: Public
00132 //  Description:
00133 ////////////////////////////////////////////////////////////////////
00134 template<class T>
00135 INLINE bool WeakPointerToBase<T>::
00136 operator == (const To *other) const {
00137   return (To *)_void_ptr == other;
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //     Function: WeakPointerToBase::Nonequivalence operator
00142 //       Access: Public
00143 //  Description:
00144 ////////////////////////////////////////////////////////////////////
00145 template<class T>
00146 INLINE bool WeakPointerToBase<T>::
00147 operator != (const To *other) const {
00148   return (To *)_void_ptr != other;
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: WeakPointerToBase::Greater-than operator
00153 //       Access: Public
00154 //  Description:
00155 ////////////////////////////////////////////////////////////////////
00156 template<class T>
00157 INLINE bool WeakPointerToBase<T>::
00158 operator > (const To *other) const {
00159   return (To *)_void_ptr > other;
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: WeakPointerToBase::Less-than-or-equal operator
00164 //       Access: Public
00165 //  Description:
00166 ////////////////////////////////////////////////////////////////////
00167 template<class T>
00168 INLINE bool WeakPointerToBase<T>::
00169 operator <= (const To *other) const {
00170   return (To *)_void_ptr <= other;
00171 }
00172 
00173 ////////////////////////////////////////////////////////////////////
00174 //     Function: WeakPointerToBase::Greater-than-or-equal operator
00175 //       Access: Public
00176 //  Description:
00177 ////////////////////////////////////////////////////////////////////
00178 template<class T>
00179 INLINE bool WeakPointerToBase<T>::
00180 operator >= (const To *other) const {
00181   return (To *)_void_ptr >= other;
00182 }
00183 ////////////////////////////////////////////////////////////////////
00184 //     Function: WeakPointerToBase::Equivalence operator
00185 //       Access: Public
00186 //  Description:
00187 ////////////////////////////////////////////////////////////////////
00188 template<class T>
00189 INLINE bool WeakPointerToBase<T>::
00190 operator == (To *other) const {
00191   return (To *)_void_ptr == other;
00192 }
00193 
00194 ////////////////////////////////////////////////////////////////////
00195 //     Function: WeakPointerToBase::Nonequivalence operator
00196 //       Access: Public
00197 //  Description:
00198 ////////////////////////////////////////////////////////////////////
00199 template<class T>
00200 INLINE bool WeakPointerToBase<T>::
00201 operator != (To *other) const {
00202   return (To *)_void_ptr != other;
00203 }
00204 
00205 ////////////////////////////////////////////////////////////////////
00206 //     Function: WeakPointerToBase::Greater-than operator
00207 //       Access: Public
00208 //  Description:
00209 ////////////////////////////////////////////////////////////////////
00210 template<class T>
00211 INLINE bool WeakPointerToBase<T>::
00212 operator > (To *other) const {
00213   return (To *)_void_ptr > other;
00214 }
00215 
00216 ////////////////////////////////////////////////////////////////////
00217 //     Function: WeakPointerToBase::Less-than-or-equal operator
00218 //       Access: Public
00219 //  Description:
00220 ////////////////////////////////////////////////////////////////////
00221 template<class T>
00222 INLINE bool WeakPointerToBase<T>::
00223 operator <= (To *other) const {
00224   return (To *)_void_ptr <= other;
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: WeakPointerToBase::Greater-than-or-equal operator
00229 //       Access: Public
00230 //  Description:
00231 ////////////////////////////////////////////////////////////////////
00232 template<class T>
00233 INLINE bool WeakPointerToBase<T>::
00234 operator >= (To *other) const {
00235   return (To *)_void_ptr >= other;
00236 }
00237 
00238 ////////////////////////////////////////////////////////////////////
00239 //     Function: WeakPointerToBase::Equivalence operator
00240 //       Access: Public
00241 //  Description:
00242 ////////////////////////////////////////////////////////////////////
00243 template<class T>
00244 INLINE bool WeakPointerToBase<T>::
00245 operator == (const WeakPointerToBase<To> &other) const {
00246   return (To *)_void_ptr == (To *)other._void_ptr;
00247 }
00248 
00249 ////////////////////////////////////////////////////////////////////
00250 //     Function: WeakPointerToBase::Nonequivalence operator
00251 //       Access: Public
00252 //  Description:
00253 ////////////////////////////////////////////////////////////////////
00254 template<class T>
00255 INLINE bool WeakPointerToBase<T>::
00256 operator != (const WeakPointerToBase<To> &other) const {
00257   return (To *)_void_ptr != (To *)other._void_ptr;
00258 }
00259 
00260 ////////////////////////////////////////////////////////////////////
00261 //     Function: WeakPointerToBase::Greater-than operator
00262 //       Access: Public
00263 //  Description:
00264 ////////////////////////////////////////////////////////////////////
00265 template<class T>
00266 INLINE bool WeakPointerToBase<T>::
00267 operator > (const WeakPointerToBase<To> &other) const {
00268   return (To *)_void_ptr > (To *)other._void_ptr;
00269 }
00270 
00271 ////////////////////////////////////////////////////////////////////
00272 //     Function: WeakPointerToBase::Less-than-or-equal operator
00273 //       Access: Public
00274 //  Description:
00275 ////////////////////////////////////////////////////////////////////
00276 template<class T>
00277 INLINE bool WeakPointerToBase<T>::
00278 operator <= (const WeakPointerToBase<To> &other) const {
00279   return (To *)_void_ptr <= (To *)other._void_ptr;
00280 }
00281 
00282 ////////////////////////////////////////////////////////////////////
00283 //     Function: WeakPointerToBase::Greater-than-or-equal operator
00284 //       Access: Public
00285 //  Description:
00286 ////////////////////////////////////////////////////////////////////
00287 template<class T>
00288 INLINE bool WeakPointerToBase<T>::
00289 operator >= (const WeakPointerToBase<To> &other) const {
00290   return (To *)_void_ptr >= (To *)other._void_ptr;
00291 }
00292 
00293 ////////////////////////////////////////////////////////////////////
00294 //     Function: WeakPointerToBase::Equivalence operator
00295 //       Access: Public
00296 //  Description:
00297 ////////////////////////////////////////////////////////////////////
00298 template<class T>
00299 INLINE bool WeakPointerToBase<T>::
00300 operator == (const PointerToBase<To> &other) const {
00301   return (To *)_void_ptr == (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00302 }
00303 
00304 ////////////////////////////////////////////////////////////////////
00305 //     Function: WeakPointerToBase::Nonequivalence operator
00306 //       Access: Public
00307 //  Description:
00308 ////////////////////////////////////////////////////////////////////
00309 template<class T>
00310 INLINE bool WeakPointerToBase<T>::
00311 operator != (const PointerToBase<To> &other) const {
00312   return (To *)_void_ptr != (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00313 }
00314 
00315 ////////////////////////////////////////////////////////////////////
00316 //     Function: WeakPointerToBase::Greater-than operator
00317 //       Access: Public
00318 //  Description:
00319 ////////////////////////////////////////////////////////////////////
00320 template<class T>
00321 INLINE bool WeakPointerToBase<T>::
00322 operator > (const PointerToBase<To> &other) const {
00323   return (To *)_void_ptr > (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00324 }
00325 
00326 ////////////////////////////////////////////////////////////////////
00327 //     Function: WeakPointerToBase::Less-than-or-equal operator
00328 //       Access: Public
00329 //  Description:
00330 ////////////////////////////////////////////////////////////////////
00331 template<class T>
00332 INLINE bool WeakPointerToBase<T>::
00333 operator <= (const PointerToBase<To> &other) const {
00334   return (To *)_void_ptr <= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00335 }
00336 
00337 ////////////////////////////////////////////////////////////////////
00338 //     Function: WeakPointerToBase::Greater-than-or-equal operator
00339 //       Access: Public
00340 //  Description:
00341 ////////////////////////////////////////////////////////////////////
00342 template<class T>
00343 INLINE bool WeakPointerToBase<T>::
00344 operator >= (const PointerToBase<To> &other) const {
00345   return (To *)_void_ptr >= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00346 }
00347 #endif  // WIN32_VC
00348 
00349 ////////////////////////////////////////////////////////////////////
00350 //     Function: WeakPointerToBase::Less-than operator
00351 //       Access: Public
00352 //  Description:
00353 ////////////////////////////////////////////////////////////////////
00354 template<class T>
00355 INLINE bool WeakPointerToBase<T>::
00356 operator < (const To *other) const {
00357   return (To *)_void_ptr < other;
00358 }
00359 
00360 ////////////////////////////////////////////////////////////////////
00361 //     Function: WeakPointerToBase::Less-than operator
00362 //       Access: Public
00363 //  Description:
00364 ////////////////////////////////////////////////////////////////////
00365 template<class T>
00366 INLINE bool WeakPointerToBase<T>::
00367 operator < (const WeakPointerToBase<To> &other) const {
00368   return (To *)_void_ptr < (To *)other._void_ptr;
00369 }
00370 
00371 ////////////////////////////////////////////////////////////////////
00372 //     Function: WeakPointerToBase::Less-than operator
00373 //       Access: Public
00374 //  Description:
00375 ////////////////////////////////////////////////////////////////////
00376 template<class T>
00377 INLINE bool WeakPointerToBase<T>::
00378 operator < (const PointerToBase<To> &other) const {
00379   return (To *)_void_ptr < (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
00380 }
00381 
00382 #endif  // CPPPARSER
00383 
00384 
00385 
00386 ////////////////////////////////////////////////////////////////////
00387 //     Function: WeakPointerToBase::clear
00388 //       Access: Published
00389 //  Description: A convenient way to set the PointerTo object to NULL.
00390 //               (Assignment to a NULL pointer also works, of course.)
00391 ////////////////////////////////////////////////////////////////////
00392 template<class T>
00393 INLINE void WeakPointerToBase<T>::
00394 clear() {
00395   reassign((To *)NULL);
00396 }
00397 
00398 ////////////////////////////////////////////////////////////////////
00399 //     Function: WeakPointerToBase::refresh
00400 //       Access: Published
00401 //  Description: Informs the WeakPointerTo object that its pointer is
00402 //               no longer deleted.  This may be used after a
00403 //               WeakPointerTo has deleted a deleted pointer, and then
00404 //               a new pointer has been reallocated.  It's equivalent
00405 //               to simply reassigning the pointer to its new
00406 //               (i.e. original) value, but has the advantage that it
00407 //               is const, so can be used for WeakPointers used as
00408 //               keys in STL maps and sets.
00409 ////////////////////////////////////////////////////////////////////
00410 template<class T>
00411 INLINE void WeakPointerToBase<T>::
00412 refresh() const {
00413   ((WeakPointerToBase<T> *)this)->reassign((To *)_void_ptr);
00414 }
00415 
00416 ////////////////////////////////////////////////////////////////////
00417 //     Function: WeakPointerToBase::output
00418 //       Access: Published
00419 //  Description: A handy function to output PointerTo's as a hex
00420 //               pointer followed by a reference count.
00421 ////////////////////////////////////////////////////////////////////
00422 template<class T>
00423 INLINE void WeakPointerToBase<T>::
00424 output(ostream &out) const {
00425   out << _void_ptr;
00426   if (was_deleted()) {
00427     out << ":deleted";
00428   } else if (_void_ptr != (void *)NULL) {
00429     out << ":" << ((To *)_void_ptr)->get_ref_count();
00430   }
00431 }
 All Classes Functions Variables Enumerations