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