00001 // Filename: nodePointerToBase.I 00002 // Created by: drose (07May05) 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: NodePointerToBase::Constructor 00018 // Access: Protected 00019 // Description: 00020 //////////////////////////////////////////////////////////////////// 00021 template<class T> 00022 INLINE NodePointerToBase<T>:: 00023 NodePointerToBase(To *ptr) { 00024 reassign(ptr); 00025 } 00026 00027 //////////////////////////////////////////////////////////////////// 00028 // Function: NodePointerToBase::Copy Constructor 00029 // Access: Protected 00030 // Description: 00031 //////////////////////////////////////////////////////////////////// 00032 template<class T> 00033 INLINE NodePointerToBase<T>:: 00034 NodePointerToBase(const NodePointerToBase<T> ©) { 00035 reassign(copy); 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: NodePointerToBase::Destructor 00040 // Access: Protected 00041 // Description: 00042 //////////////////////////////////////////////////////////////////// 00043 template<class T> 00044 INLINE NodePointerToBase<T>:: 00045 ~NodePointerToBase() { 00046 reassign((To *)NULL); 00047 } 00048 00049 //////////////////////////////////////////////////////////////////// 00050 // Function: NodePointerToBase::reassign 00051 // Access: Protected 00052 // Description: This is the main work of the NodePointerTo family. When 00053 // the pointer is reassigned, decrement the old 00054 // reference count and increment the new one. 00055 //////////////////////////////////////////////////////////////////// 00056 template<class T> 00057 void NodePointerToBase<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->node_ref(); 00070 #ifdef DO_MEMORY_USAGE 00071 if (MemoryUsage::get_track_memory_usage()) { 00072 // Make sure the MemoryUsage record knows what the TypeHandle 00073 // is, if we know it ourselves. 00074 TypeHandle type = get_type_handle(To); 00075 if (type == TypeHandle::none()) { 00076 do_init_type(To); 00077 type = get_type_handle(To); 00078 } 00079 if (type != TypeHandle::none()) { 00080 MemoryUsage::update_type(ptr, type); 00081 } 00082 } 00083 #endif 00084 } 00085 00086 // Now delete the old pointer. 00087 if (old_ptr != (To *)NULL) { 00088 node_unref_delete(old_ptr); 00089 } 00090 } 00091 } 00092 00093 //////////////////////////////////////////////////////////////////// 00094 // Function: NodePointerToBase::reassign 00095 // Access: Protected 00096 // Description: 00097 //////////////////////////////////////////////////////////////////// 00098 template<class T> 00099 INLINE void NodePointerToBase<T>:: 00100 reassign(const NodePointerToBase<To> ©) { 00101 reassign((To *)copy._void_ptr); 00102 } 00103 00104 00105 //////////////////////////////////////////////////////////////////// 00106 // Function: NodePointerToBase::clear 00107 // Access: Published 00108 // Description: A convenient way to set the NodePointerTo object to NULL. 00109 // (Assignment to a NULL pointer also works, of course.) 00110 //////////////////////////////////////////////////////////////////// 00111 template<class T> 00112 INLINE void NodePointerToBase<T>:: 00113 clear() { 00114 reassign((To *)NULL); 00115 } 00116 00117 //////////////////////////////////////////////////////////////////// 00118 // Function: NodePointerToBase::output 00119 // Access: Published 00120 // Description: A handy function to output NodePointerTo's as a hex 00121 // pointer followed by a reference count. 00122 //////////////////////////////////////////////////////////////////// 00123 template<class T> 00124 INLINE void NodePointerToBase<T>:: 00125 output(ostream &out) const { 00126 out << _void_ptr; 00127 if (_void_ptr != (void *)NULL) { 00128 out << ":" << ((To *)_void_ptr)->get_node_ref_count() << "/" 00129 << ((To *)_void_ptr)->get_ref_count(); 00130 } 00131 }