Panda3D
weakReferenceList.cxx
1 // Filename: weakReferenceList.cxx
2 // Created by: drose (27Sep04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "weakReferenceList.h"
16 #include "weakPointerToVoid.h"
17 #include "pnotify.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: WeakReferenceList::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 WeakReferenceList::
25 WeakReferenceList() {
26 }
27 
28 ////////////////////////////////////////////////////////////////////
29 // Function: WeakReferenceList::Destructor
30 // Access: Public
31 // Description: The destructor tells all of the owned references that
32 // we're gone.
33 ////////////////////////////////////////////////////////////////////
36  _lock.acquire();
37  Pointers::iterator pi;
38  for (pi = _pointers.begin(); pi != _pointers.end(); ++pi) {
39  (*pi)->mark_deleted();
40  }
41  _lock.release();
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: WeakReferenceList::add_reference
46 // Access: Public
47 // Description: Intended to be called only by WeakPointerTo (or by
48 // any class implementing a weak reference-counting
49 // pointer), this adds the indicated PointerToVoid
50 // structure to the list of such structures that are
51 // maintaining a weak pointer to this object.
52 //
53 // When the WeakReferenceList destructs (presumably
54 // because its owning object destructs), the pointer
55 // within the PointerToVoid object will be set to NULL.
56 ////////////////////////////////////////////////////////////////////
59  _lock.acquire();
60  bool inserted = _pointers.insert(ptv).second;
61  _lock.release();
62  nassertv(inserted);
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function: WeakReferenceList::clear_reference
67 // Access: Public
68 // Description: Intended to be called only by WeakPointerTo (or by
69 // any class implementing a weak reference-counting
70 // pointer), this removes the indicated PointerToVoid
71 // structure from the list of such structures that are
72 // maintaining a weak pointer to this object.
73 ////////////////////////////////////////////////////////////////////
76  _lock.acquire();
77  Pointers::iterator pi = _pointers.find(ptv);
78  bool valid = (pi != _pointers.end());
79  if (valid) {
80  _pointers.erase(pi);
81  }
82  _lock.release();
83  nassertv(valid);
84 }
void add_reference(WeakPointerToVoid *ptv)
Intended to be called only by WeakPointerTo (or by any class implementing a weak reference-counting p...
This is the specialization of PointerToVoid for weak pointers.
void clear_reference(WeakPointerToVoid *ptv)
Intended to be called only by WeakPointerTo (or by any class implementing a weak reference-counting p...
~WeakReferenceList()
The destructor tells all of the owned references that we're gone.