Panda3D

pointerToBase.I

00001 // Filename: pointerToBase.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: PointerToBase::Constructor
00018 //       Access: Protected
00019 //  Description:
00020 ////////////////////////////////////////////////////////////////////
00021 template<class T>
00022 INLINE PointerToBase<T>::
00023 PointerToBase(To *ptr) {
00024   reassign(ptr);
00025 }
00026 
00027 ////////////////////////////////////////////////////////////////////
00028 //     Function: PointerToBase::Copy Constructor
00029 //       Access: Protected
00030 //  Description:
00031 ////////////////////////////////////////////////////////////////////
00032 template<class T>
00033 INLINE PointerToBase<T>::
00034 PointerToBase(const PointerToBase<T> &copy) {
00035   reassign(copy);
00036 }
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //     Function: PointerToBase::Destructor
00040 //       Access: Protected
00041 //  Description:
00042 ////////////////////////////////////////////////////////////////////
00043 template<class T>
00044 INLINE PointerToBase<T>::
00045 ~PointerToBase() {
00046   reassign((To *)NULL);
00047 }
00048 
00049 ////////////////////////////////////////////////////////////////////
00050 //     Function: PointerToBase::reassign
00051 //       Access: Protected
00052 //  Description: This is the main work of the PointerTo family.  When
00053 //               the pointer is reassigned, decrement the old
00054 //               reference count and increment the new one.
00055 ////////////////////////////////////////////////////////////////////
00056 template<class T>
00057 INLINE void PointerToBase<T>::
00058 reassign(To *ptr) {
00059   if (ptr != (To *)_void_ptr) {
00060     // First save the old pointer; we won't delete it until we have
00061     // assigned the new one.  We do this just in case there are
00062     // cascading effects from deleting this pointer that might
00063     // inadvertently delete the new one.  (Don't laugh--it's
00064     // happened!)
00065     To *old_ptr = (To *)_void_ptr;
00066 
00067     _void_ptr = (void *)ptr;
00068     if (ptr != (To *)NULL) {
00069       ptr->ref();
00070 #ifdef DO_MEMORY_USAGE
00071       if (MemoryUsage::get_track_memory_usage()) {
00072         update_type(ptr);
00073       }
00074 #endif
00075     }
00076 
00077     // Now delete the old pointer.
00078     if (old_ptr != (To *)NULL) {
00079       unref_delete(old_ptr);
00080     }
00081   }
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //     Function: PointerToBase::reassign
00086 //       Access: Protected
00087 //  Description:
00088 ////////////////////////////////////////////////////////////////////
00089 template<class T>
00090 INLINE void PointerToBase<T>::
00091 reassign(const PointerToBase<To> &copy) {
00092   reassign((To *)copy._void_ptr);
00093 }
00094 
00095 #ifdef DO_MEMORY_USAGE
00096 ////////////////////////////////////////////////////////////////////
00097 //     Function: PointerToBase::update_type
00098 //       Access: Protected
00099 //  Description: Ensures that the MemoryUsage record for the pointer
00100 //               has the right type of object, if we know the type
00101 //               ourselves.
00102 ////////////////////////////////////////////////////////////////////
00103 template<class T>
00104 void PointerToBase<T>::
00105 update_type(To *ptr) {
00106   TypeHandle type = get_type_handle(To);
00107   if (type == TypeHandle::none()) {
00108     do_init_type(To);
00109     type = get_type_handle(To);
00110   }
00111   if (type != TypeHandle::none()) {
00112     MemoryUsage::update_type(ptr, type);
00113   }
00114 }
00115 #endif  // DO_MEMORY_USAGE
00116 
00117 
00118 ////////////////////////////////////////////////////////////////////
00119 //     Function: PointerToBase::clear
00120 //       Access: Published
00121 //  Description: A convenient way to set the PointerTo object to NULL.
00122 //               (Assignment to a NULL pointer also works, of course.)
00123 ////////////////////////////////////////////////////////////////////
00124 template<class T>
00125 INLINE void PointerToBase<T>::
00126 clear() {
00127   reassign((To *)NULL);
00128 }
00129 
00130 ////////////////////////////////////////////////////////////////////
00131 //     Function: PointerToBase::output
00132 //       Access: Published
00133 //  Description: A handy function to output PointerTo's as a hex
00134 //               pointer followed by a reference count.
00135 ////////////////////////////////////////////////////////////////////
00136 template<class T>
00137 INLINE void PointerToBase<T>::
00138 output(ostream &out) const {
00139   out << _void_ptr;
00140   if (_void_ptr != (void *)NULL) {
00141     out << ":" << ((To *)_void_ptr)->get_ref_count();
00142   }
00143 }
 All Classes Functions Variables Enumerations