Panda3D
weakPointerToVoid.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file weakPointerToVoid.I
10  * @author drose
11  * @date 2004-09-27
12  */
13 
14 /**
15  * Sets a callback that will be made when the pointer is deleted. Does
16  * nothing if this is a null pointer.
17  *
18  * If the pointer has already been deleted, the callback will be made
19  * immediately.
20  */
21 INLINE void WeakPointerToVoid::
23  if (_weak_ref != nullptr && !_weak_ref->was_deleted()) {
24  _weak_ref->add_callback(callback, _void_ptr);
25  } else if (_void_ptr != nullptr) {
26  callback->wp_callback(_void_ptr);
27  _weak_ref = nullptr;
28  }
29 }
30 
31 /**
32  * Removes a previously added callback.
33  */
34 INLINE void WeakPointerToVoid::
36  if (_weak_ref != nullptr) {
37  _weak_ref->remove_callback(callback);
38  }
39 }
40 
41 /**
42  * Returns true if the object we are pointing to has been deleted, false
43  * otherwise. If this returns true, it means that the pointer can not yet be
44  * reused, but it does not guarantee that it can be safely accessed. See the
45  * lock() method for a safe way to access the underlying pointer.
46  *
47  * This will always return true for a null pointer, unlike is_valid_pointer().
48  */
49 INLINE bool WeakPointerToVoid::
50 was_deleted() const {
51  return _void_ptr != nullptr && (_weak_ref == nullptr || _weak_ref->was_deleted());
52 }
53 
54 /**
55  * Returns true if the pointer is not null and the object has not been
56  * deleted. See was_deleted() for caveats.
57  */
58 INLINE bool WeakPointerToVoid::
60  return _weak_ref != nullptr && !_weak_ref->was_deleted();
61 }
void add_callback(WeakPointerCallback *callback, void *data)
Adds the callback to the list of callbacks that will be called when the underlying pointer is deleted...
bool was_deleted() const
Returns true if the object we are pointing to has been deleted, false otherwise.
bool was_deleted() const
Returns true if the object represented has been deleted, ie.
void remove_callback(WeakPointerCallback *callback) const
Removes a previously added callback.
bool is_valid_pointer() const
Returns true if the pointer is not null and the object has not been deleted.
void add_callback(WeakPointerCallback *callback) const
Sets a callback that will be made when the pointer is deleted.
Derive from this class and override the callback() method if you want to get an immediate callback fr...
void remove_callback(WeakPointerCallback *callback)
Intended to be called only by WeakPointerTo (or by any class implementing a weak reference-counting p...