Panda3D
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  */
17 template<class T>
19 NodePointerToBase(To *ptr) {
20  reassign(ptr);
21 }
22 
23 /**
24  *
25  */
26 template<class T>
29  reassign(copy);
30 }
31 
32 /**
33  *
34  */
35 template<class T>
38  reassign(nullptr);
39 }
40 
41 /**
42  *
43  */
44 template<class T>
46 NodePointerToBase(NodePointerToBase<T> &&from) noexcept {
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  */
57 template<class T>
58 INLINE void NodePointerToBase<T>::
59 reassign(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  */
75 template<class T>
77 reassign(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  */
114 template<class T>
115 INLINE void NodePointerToBase<T>::
116 reassign(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  */
125 template<class T>
126 INLINE void NodePointerToBase<T>::
127 clear() {
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  */
135 template<class T>
136 INLINE void NodePointerToBase<T>::
137 output(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 }
void clear()
A convenient way to set the NodePointerTo object to NULL.
This is similar to PointerToBase, but it manages objects of type NodeReferenceCount or NodeCachedRefe...
static void update_type(ReferenceCount *ptr, TypeHandle type)
Associates the indicated type with the given pointer.
Definition: memoryUsage.I:55
void output(std::ostream &out) const
A handy function to output NodePointerTo's as a hex pointer followed by a reference count.
static bool get_track_memory_usage()
Returns true if the user has Configured the variable 'track-memory-usage' to true,...
Definition: memoryUsage.I:20
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...