Panda3D
weakPointerToVoid.I
1 // Filename: weakPointerToVoid.I
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: WeakPointerToVoid::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 INLINE WeakPointerToVoid::
22 WeakPointerToVoid() {
23  _ptr_was_deleted = false;
24  _callback = NULL;
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: WeakPointerToVoid::Destructor
29 // Access: Protected
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 INLINE WeakPointerToVoid::
33 ~WeakPointerToVoid() {
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: WeakPointerToVoid::mark_deleted
38 // Access: Public
39 // Description: This is intended only to be called by the
40 // WeakPointerList destructor. It indicates that the
41 // object that we were pointing to has just been
42 // deleted.
43 ////////////////////////////////////////////////////////////////////
44 INLINE void WeakPointerToVoid::
46  nassertv(!_ptr_was_deleted);
47  _ptr_was_deleted = true;
48  if (_callback != (WeakPointerCallback *)NULL) {
49  _callback->wp_callback(_void_ptr);
50  }
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: WeakPointerToVoid::set_callback
55 // Access: Public
56 // Description: Sets a callback that will be made when the pointer is
57 // deleted. If a previous callback has already been
58 // set, it will be replaced.
59 //
60 // If the pointer has already been deleted, the callback
61 // will be made immediately.
62 ////////////////////////////////////////////////////////////////////
63 INLINE void WeakPointerToVoid::
65  _callback = callback;
66  if (_ptr_was_deleted && _callback != (WeakPointerCallback *)NULL) {
67  _callback->wp_callback(_void_ptr);
68  }
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: WeakPointerToVoid::get_callback
73 // Access: Public
74 // Description: Returns the callback that will be made when the
75 // pointer is deleted, or NULL if no callback has been
76 // set.
77 ////////////////////////////////////////////////////////////////////
79 get_callback() const {
80  return _callback;
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: WeakPointerToVoid::was_deleted
85 // Access: Published
86 // Description: Returns true if the object we are pointing to has
87 // been deleted, false otherwise.
88 ////////////////////////////////////////////////////////////////////
89 INLINE bool WeakPointerToVoid::
90 was_deleted() const {
91  return _ptr_was_deleted;
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: WeakPointerToVoid::is_valid_pointer
96 // Access: Published
97 // Description: Returns true if the pointer is not null and the
98 // object has not been deleted.
99 ////////////////////////////////////////////////////////////////////
100 INLINE bool WeakPointerToVoid::
102  return (_void_ptr != (void *)NULL) && !_ptr_was_deleted;
103 }
bool was_deleted() const
Returns true if the object we are pointing to has been deleted, false otherwise.
WeakPointerCallback * get_callback() const
Returns the callback that will be made when the pointer is deleted, or NULL if no callback has been s...
bool is_valid_pointer() const
Returns true if the pointer is not null and the object has not been deleted.
Derive from this class and override the callback() method if you want to get an immediate callback fr...
void mark_deleted()
This is intended only to be called by the WeakPointerList destructor.
void set_callback(WeakPointerCallback *callback)
Sets a callback that will be made when the pointer is deleted.