Panda3D

Loading...
Searching...
No Matches
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 */
21INLINE void WeakPointerToVoid::
22 add_callback(WeakPointerCallback *callback) const {
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 */
34INLINE void WeakPointerToVoid::
35 remove_callback(WeakPointerCallback *callback) const {
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 */
49INLINE 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 */
58INLINE bool WeakPointerToVoid::
59 is_valid_pointer() const {
60 return _weak_ref != nullptr && !_weak_ref->was_deleted();
61}
WeakPointerCallback
Derive from this class and override the callback() method if you want to get an immediate callback fr...
Definition weakPointerCallback.h:24
WeakPointerToVoid::add_callback
void add_callback(WeakPointerCallback *callback) const
Sets a callback that will be made when the pointer is deleted.
Definition weakPointerToVoid.I:22
WeakPointerToVoid::remove_callback
void remove_callback(WeakPointerCallback *callback) const
Removes a previously added callback.
Definition weakPointerToVoid.I:35
WeakPointerToVoid::was_deleted
bool was_deleted() const
Returns true if the object we are pointing to has been deleted, false otherwise.
Definition weakPointerToVoid.I:50
WeakPointerToVoid::is_valid_pointer
bool is_valid_pointer() const
Returns true if the pointer is not null and the object has not been deleted.
Definition weakPointerToVoid.I:59