Panda3D
Loading...
Searching...
No Matches
nodePointerToBase.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 nodePointerToBase.I
10 * @author drose
11 * @date 2005-05-07
12 */
13
14/**
15 *
16 */
17template<class T>
19NodePointerToBase(To *ptr) {
20 reassign(ptr);
21}
22
23/**
24 *
25 */
26template<class T>
29 reassign(copy);
30}
31
32/**
33 *
34 */
35template<class T>
38 reassign(nullptr);
39}
40
41/**
42 *
43 */
44template<class T>
47 _void_ptr = from._void_ptr;
48 from._void_ptr = nullptr;
49}
50
51/**
52 * This version of reassign is called when a NodePointerTo is assigned to this
53 * Node PointerTo as an rvalue. In this case, we can steal the reference
54 * count from the other PointerTo, without needing to call ref() and unref()
55 * unnecessarily.
56 */
57template<class T>
58INLINE void NodePointerToBase<T>::
59reassign(NodePointerToBase<T> &&from) noexcept {
60 To *old_ptr = (To *)this->_void_ptr;
61
62 this->_void_ptr = from._void_ptr;
63 from._void_ptr = nullptr;
64
65 // Now delete the old pointer.
66 if (old_ptr != nullptr) {
67 node_unref_delete(old_ptr);
68 }
69}
70
71/**
72 * This is the main work of the NodePointerTo family. When the pointer is
73 * reassigned, decrement the old reference count and increment the new one.
74 */
75template<class T>
77reassign(To *ptr) {
78 if (ptr != (To *)_void_ptr) {
79 // First save the old pointer; we won't delete it until we have assigned
80 // the new one. We do this just in case there are cascading effects from
81 // deleting this pointer that might inadvertently delete the new one.
82 // (Don't laugh--it's happened!)
83 To *old_ptr = (To *)_void_ptr;
84
85 _void_ptr = (void *)ptr;
86 if (ptr != nullptr) {
87 ptr->node_ref();
88#ifdef DO_MEMORY_USAGE
90 // Make sure the MemoryUsage record knows what the TypeHandle is, if
91 // we know it ourselves.
92 TypeHandle type = get_type_handle(To);
93 if (type == TypeHandle::none()) {
94 do_init_type(To);
95 type = get_type_handle(To);
96 }
97 if (type != TypeHandle::none()) {
98 MemoryUsage::update_type(ptr, type);
99 }
100 }
101#endif
102 }
103
104 // Now delete the old pointer.
105 if (old_ptr != nullptr) {
106 node_unref_delete(old_ptr);
107 }
108 }
109}
110
111/**
112 *
113 */
114template<class T>
115INLINE void NodePointerToBase<T>::
116reassign(const NodePointerToBase<To> &copy) {
117 reassign((To *)copy._void_ptr);
118}
119
120
121/**
122 * A convenient way to set the NodePointerTo object to NULL. (Assignment to a
123 * NULL pointer also works, of course.)
124 */
125template<class T>
127clear() {
128 reassign(nullptr);
129}
130
131/**
132 * A handy function to output NodePointerTo's as a hex pointer followed by a
133 * reference count.
134 */
135template<class T>
137output(std::ostream &out) const {
138 out << _void_ptr;
139 if (_void_ptr != nullptr) {
140 out << ":" << ((To *)_void_ptr)->get_node_ref_count() << "/"
141 << ((To *)_void_ptr)->get_ref_count();
142 }
143}
static void update_type(ReferenceCount *ptr, TypeHandle type)
Associates the indicated type with the given pointer.
Definition memoryUsage.I:55
static bool get_track_memory_usage()
Returns true if the user has Configured the variable 'track-memory-usage' to true,...
Definition memoryUsage.I:20
This is similar to PointerToBase, but it manages objects of type NodeReferenceCount or NodeCachedRefe...
void clear()
A convenient way to set the NodePointerTo object to NULL.
void output(std::ostream &out) const
A handy function to output NodePointerTo's as a hex pointer followed by a reference count.
TypeHandle is the identifier used to differentiate C++ class types.
Definition typeHandle.h:81
void node_unref_delete(RefCountType *ptr)
This global helper function will unref the given ReferenceCount object, and if the reference count re...