Panda3D
|
00001 // Filename: weakReferenceList.cxx 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 #include "weakReferenceList.h" 00016 #include "weakPointerToVoid.h" 00017 #include "pnotify.h" 00018 00019 //////////////////////////////////////////////////////////////////// 00020 // Function: WeakReferenceList::Constructor 00021 // Access: Public 00022 // Description: 00023 //////////////////////////////////////////////////////////////////// 00024 WeakReferenceList:: 00025 WeakReferenceList() { 00026 } 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Function: WeakReferenceList::Destructor 00030 // Access: Public 00031 // Description: The destructor tells all of the owned references that 00032 // we're gone. 00033 //////////////////////////////////////////////////////////////////// 00034 WeakReferenceList:: 00035 ~WeakReferenceList() { 00036 _lock.acquire(); 00037 Pointers::iterator pi; 00038 for (pi = _pointers.begin(); pi != _pointers.end(); ++pi) { 00039 (*pi)->mark_deleted(); 00040 } 00041 _lock.release(); 00042 } 00043 00044 //////////////////////////////////////////////////////////////////// 00045 // Function: WeakReferenceList::add_reference 00046 // Access: Public 00047 // Description: Intended to be called only by WeakPointerTo (or by 00048 // any class implementing a weak reference-counting 00049 // pointer), this adds the indicated PointerToVoid 00050 // structure to the list of such structures that are 00051 // maintaining a weak pointer to this object. 00052 // 00053 // When the WeakReferenceList destructs (presumably 00054 // because its owning object destructs), the pointer 00055 // within the PointerToVoid object will be set to NULL. 00056 //////////////////////////////////////////////////////////////////// 00057 void WeakReferenceList:: 00058 add_reference(WeakPointerToVoid *ptv) { 00059 _lock.acquire(); 00060 bool inserted = _pointers.insert(ptv).second; 00061 _lock.release(); 00062 nassertv(inserted); 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: WeakReferenceList::clear_reference 00067 // Access: Public 00068 // Description: Intended to be called only by WeakPointerTo (or by 00069 // any class implementing a weak reference-counting 00070 // pointer), this removes the indicated PointerToVoid 00071 // structure from the list of such structures that are 00072 // maintaining a weak pointer to this object. 00073 //////////////////////////////////////////////////////////////////// 00074 void WeakReferenceList:: 00075 clear_reference(WeakPointerToVoid *ptv) { 00076 _lock.acquire(); 00077 Pointers::iterator pi = _pointers.find(ptv); 00078 bool valid = (pi != _pointers.end()); 00079 if (valid) { 00080 _pointers.erase(pi); 00081 } 00082 _lock.release(); 00083 nassertv(valid); 00084 }