Panda3D
nodePointerToBase.I
1 // Filename: nodePointerToBase.I
2 // Created by: drose (07May05)
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: NodePointerToBase::Constructor
18 // Access: Protected
19 // Description:
20 ////////////////////////////////////////////////////////////////////
21 template<class T>
23 NodePointerToBase(To *ptr) {
24  reassign(ptr);
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: NodePointerToBase::Copy Constructor
29 // Access: Protected
30 // Description:
31 ////////////////////////////////////////////////////////////////////
32 template<class T>
35  reassign(copy);
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: NodePointerToBase::Destructor
40 // Access: Protected
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 template<class T>
46  reassign((To *)NULL);
47 }
48 
49 #ifdef USE_MOVE_SEMANTICS
50 ////////////////////////////////////////////////////////////////////
51 // Function: NodePointerToBase::Move Constructor
52 // Access: Protected
53 // Description:
54 ////////////////////////////////////////////////////////////////////
55 template<class T>
57 NodePointerToBase(NodePointerToBase<T> &&from) NOEXCEPT {
58  _void_ptr = from._void_ptr;
59  from._void_ptr = (void *)NULL;
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: NodePointerToBase::reassign
64 // Access: Protected
65 // Description: This version of reassign is called when a
66 // NodePointerTo is assigned to this Node PointerTo
67 // as an rvalue. In this case, we can steal the
68 // reference count from the other PointerTo, without
69 // needing to call ref() and unref() unnecessarily.
70 ////////////////////////////////////////////////////////////////////
71 template<class T>
72 INLINE void NodePointerToBase<T>::
73 reassign(NodePointerToBase<T> &&from) NOEXCEPT {
74  To *old_ptr = (To *)this->_void_ptr;
75 
76  this->_void_ptr = from._void_ptr;
77  from._void_ptr = NULL;
78 
79  // Now delete the old pointer.
80  if (old_ptr != (To *)NULL) {
81  node_unref_delete(old_ptr);
82  }
83 }
84 #endif // USE_MOVE_SEMANTICS
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: NodePointerToBase::reassign
88 // Access: Protected
89 // Description: This is the main work of the NodePointerTo family. When
90 // the pointer is reassigned, decrement the old
91 // reference count and increment the new one.
92 ////////////////////////////////////////////////////////////////////
93 template<class T>
95 reassign(To *ptr) {
96  if (ptr != (To *)_void_ptr) {
97  // First save the old pointer; we won't delete it until we have
98  // assigned the new one. We do this just in case there are
99  // cascading effects from deleting this pointer that might
100  // inadvertently delete the new one. (Don't laugh--it's
101  // happened!)
102  To *old_ptr = (To *)_void_ptr;
103 
104  _void_ptr = (void *)ptr;
105  if (ptr != (To *)NULL) {
106  ptr->node_ref();
107 #ifdef DO_MEMORY_USAGE
108  if (MemoryUsage::get_track_memory_usage()) {
109  // Make sure the MemoryUsage record knows what the TypeHandle
110  // is, if we know it ourselves.
111  TypeHandle type = get_type_handle(To);
112  if (type == TypeHandle::none()) {
113  do_init_type(To);
114  type = get_type_handle(To);
115  }
116  if (type != TypeHandle::none()) {
117  MemoryUsage::update_type(ptr, type);
118  }
119  }
120 #endif
121  }
122 
123  // Now delete the old pointer.
124  if (old_ptr != (To *)NULL) {
125  node_unref_delete(old_ptr);
126  }
127  }
128 }
129 
130 ////////////////////////////////////////////////////////////////////
131 // Function: NodePointerToBase::reassign
132 // Access: Protected
133 // Description:
134 ////////////////////////////////////////////////////////////////////
135 template<class T>
136 INLINE void NodePointerToBase<T>::
137 reassign(const NodePointerToBase<To> &copy) {
138  reassign((To *)copy._void_ptr);
139 }
140 
141 
142 ////////////////////////////////////////////////////////////////////
143 // Function: NodePointerToBase::clear
144 // Access: Published
145 // Description: A convenient way to set the NodePointerTo object to NULL.
146 // (Assignment to a NULL pointer also works, of course.)
147 ////////////////////////////////////////////////////////////////////
148 template<class T>
149 INLINE void NodePointerToBase<T>::
150 clear() {
151  reassign((To *)NULL);
152 }
153 
154 ////////////////////////////////////////////////////////////////////
155 // Function: NodePointerToBase::output
156 // Access: Published
157 // Description: A handy function to output NodePointerTo's as a hex
158 // pointer followed by a reference count.
159 ////////////////////////////////////////////////////////////////////
160 template<class T>
161 INLINE void NodePointerToBase<T>::
162 output(ostream &out) const {
163  out << _void_ptr;
164  if (_void_ptr != (void *)NULL) {
165  out << ":" << ((To *)_void_ptr)->get_node_ref_count() << "/"
166  << ((To *)_void_ptr)->get_ref_count();
167  }
168 }
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
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...
void output(ostream &out) const
A handy function to output NodePointerTo&#39;s as a hex pointer followed by a reference count...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85