weakPointerToBase.I 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Filename: weakPointerToBase.I
  2. // Created by: drose (27Sep04)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: WeakPointerToBase::Constructor
  20. // Access: Protected
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. template<class T>
  24. INLINE WeakPointerToBase<T>::
  25. WeakPointerToBase(To *ptr) {
  26. reassign(ptr);
  27. }
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: WeakPointerToBase::Copy Constructor
  30. // Access: Protected
  31. // Description:
  32. ////////////////////////////////////////////////////////////////////
  33. template<class T>
  34. INLINE WeakPointerToBase<T>::
  35. WeakPointerToBase(const PointerToBase<T> &copy) {
  36. reassign(copy);
  37. }
  38. ////////////////////////////////////////////////////////////////////
  39. // Function: WeakPointerToBase::Copy Constructor
  40. // Access: Protected
  41. // Description:
  42. ////////////////////////////////////////////////////////////////////
  43. template<class T>
  44. INLINE WeakPointerToBase<T>::
  45. WeakPointerToBase(const WeakPointerToBase<T> &copy) {
  46. reassign(copy);
  47. }
  48. ////////////////////////////////////////////////////////////////////
  49. // Function: WeakPointerToBase::Destructor
  50. // Access: Protected
  51. // Description:
  52. ////////////////////////////////////////////////////////////////////
  53. template<class T>
  54. INLINE WeakPointerToBase<T>::
  55. ~WeakPointerToBase() {
  56. reassign((To *)NULL);
  57. }
  58. ////////////////////////////////////////////////////////////////////
  59. // Function: WeakPointerToBase::reassign
  60. // Access: Protected
  61. // Description: This is the main work of the PointerTo family. When
  62. // the pointer is reassigned, decrement the old
  63. // reference count and increment the new one.
  64. ////////////////////////////////////////////////////////////////////
  65. template<class T>
  66. void WeakPointerToBase<T>::
  67. reassign(To *ptr) {
  68. if (ptr != (To *)_void_ptr || _ptr_was_deleted) {
  69. To *old_ptr = (To *)_void_ptr;
  70. _void_ptr = (void *)ptr;
  71. if (ptr != (To *)NULL) {
  72. ptr->weak_ref(this);
  73. #ifdef DO_MEMORY_USAGE
  74. if (MemoryUsage::get_track_memory_usage()) {
  75. // Make sure the MemoryUsage record knows what the TypeHandle
  76. // is, if we know it ourselves.
  77. TypeHandle type = get_type_handle(To);
  78. if (type == TypeHandle::none()) {
  79. do_init_type(To);
  80. type = get_type_handle(To);
  81. }
  82. if (type != TypeHandle::none()) {
  83. MemoryUsage::update_type(ptr, type);
  84. }
  85. }
  86. #endif
  87. }
  88. // Now remove the old reference.
  89. if (old_ptr != (To *)NULL && !_ptr_was_deleted) {
  90. old_ptr->weak_unref(this);
  91. }
  92. _ptr_was_deleted = false;
  93. }
  94. }
  95. ////////////////////////////////////////////////////////////////////
  96. // Function: WeakPointerToBase::reassign
  97. // Access: Protected
  98. // Description:
  99. ////////////////////////////////////////////////////////////////////
  100. template<class T>
  101. INLINE void WeakPointerToBase<T>::
  102. reassign(const PointerToBase<To> &copy) {
  103. // This double-casting is a bit of a cheat to get around the
  104. // inheritance issue--it's difficult to declare a template class to
  105. // be a friend.
  106. reassign((To *)((const WeakPointerToBase<To> *)&copy)->_void_ptr);
  107. }
  108. ////////////////////////////////////////////////////////////////////
  109. // Function: WeakPointerToBase::reassign
  110. // Access: Protected
  111. // Description:
  112. ////////////////////////////////////////////////////////////////////
  113. template<class T>
  114. INLINE void WeakPointerToBase<T>::
  115. reassign(const WeakPointerToBase<To> &copy) {
  116. nassertv(!copy.was_deleted());
  117. reassign((To *)copy._void_ptr);
  118. }
  119. #ifndef CPPPARSER
  120. #ifndef WIN32_VC
  121. ////////////////////////////////////////////////////////////////////
  122. // Function: WeakPointerToBase::Equivalence operator
  123. // Access: Public
  124. // Description:
  125. ////////////////////////////////////////////////////////////////////
  126. template<class T>
  127. INLINE bool WeakPointerToBase<T>::
  128. operator == (const To *other) const {
  129. return (To *)_void_ptr == other;
  130. }
  131. ////////////////////////////////////////////////////////////////////
  132. // Function: WeakPointerToBase::Nonequivalence operator
  133. // Access: Public
  134. // Description:
  135. ////////////////////////////////////////////////////////////////////
  136. template<class T>
  137. INLINE bool WeakPointerToBase<T>::
  138. operator != (const To *other) const {
  139. return (To *)_void_ptr != other;
  140. }
  141. ////////////////////////////////////////////////////////////////////
  142. // Function: WeakPointerToBase::Greater-than operator
  143. // Access: Public
  144. // Description:
  145. ////////////////////////////////////////////////////////////////////
  146. template<class T>
  147. INLINE bool WeakPointerToBase<T>::
  148. operator > (const To *other) const {
  149. return (To *)_void_ptr > other;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: WeakPointerToBase::Less-than-or-equal operator
  153. // Access: Public
  154. // Description:
  155. ////////////////////////////////////////////////////////////////////
  156. template<class T>
  157. INLINE bool WeakPointerToBase<T>::
  158. operator <= (const To *other) const {
  159. return (To *)_void_ptr <= other;
  160. }
  161. ////////////////////////////////////////////////////////////////////
  162. // Function: WeakPointerToBase::Greater-than-or-equal operator
  163. // Access: Public
  164. // Description:
  165. ////////////////////////////////////////////////////////////////////
  166. template<class T>
  167. INLINE bool WeakPointerToBase<T>::
  168. operator >= (const To *other) const {
  169. return (To *)_void_ptr >= other;
  170. }
  171. ////////////////////////////////////////////////////////////////////
  172. // Function: WeakPointerToBase::Equivalence operator
  173. // Access: Public
  174. // Description:
  175. ////////////////////////////////////////////////////////////////////
  176. template<class T>
  177. INLINE bool WeakPointerToBase<T>::
  178. operator == (To *other) const {
  179. return (To *)_void_ptr == other;
  180. }
  181. ////////////////////////////////////////////////////////////////////
  182. // Function: WeakPointerToBase::Nonequivalence operator
  183. // Access: Public
  184. // Description:
  185. ////////////////////////////////////////////////////////////////////
  186. template<class T>
  187. INLINE bool WeakPointerToBase<T>::
  188. operator != (To *other) const {
  189. return (To *)_void_ptr != other;
  190. }
  191. ////////////////////////////////////////////////////////////////////
  192. // Function: WeakPointerToBase::Greater-than operator
  193. // Access: Public
  194. // Description:
  195. ////////////////////////////////////////////////////////////////////
  196. template<class T>
  197. INLINE bool WeakPointerToBase<T>::
  198. operator > (To *other) const {
  199. return (To *)_void_ptr > other;
  200. }
  201. ////////////////////////////////////////////////////////////////////
  202. // Function: WeakPointerToBase::Less-than-or-equal operator
  203. // Access: Public
  204. // Description:
  205. ////////////////////////////////////////////////////////////////////
  206. template<class T>
  207. INLINE bool WeakPointerToBase<T>::
  208. operator <= (To *other) const {
  209. return (To *)_void_ptr <= other;
  210. }
  211. ////////////////////////////////////////////////////////////////////
  212. // Function: WeakPointerToBase::Greater-than-or-equal operator
  213. // Access: Public
  214. // Description:
  215. ////////////////////////////////////////////////////////////////////
  216. template<class T>
  217. INLINE bool WeakPointerToBase<T>::
  218. operator >= (To *other) const {
  219. return (To *)_void_ptr >= other;
  220. }
  221. ////////////////////////////////////////////////////////////////////
  222. // Function: WeakPointerToBase::Equivalence operator
  223. // Access: Public
  224. // Description:
  225. ////////////////////////////////////////////////////////////////////
  226. template<class T>
  227. INLINE bool WeakPointerToBase<T>::
  228. operator == (const WeakPointerToBase<To> &other) const {
  229. return (To *)_void_ptr == (To *)other._void_ptr;
  230. }
  231. ////////////////////////////////////////////////////////////////////
  232. // Function: WeakPointerToBase::Nonequivalence operator
  233. // Access: Public
  234. // Description:
  235. ////////////////////////////////////////////////////////////////////
  236. template<class T>
  237. INLINE bool WeakPointerToBase<T>::
  238. operator != (const WeakPointerToBase<To> &other) const {
  239. return (To *)_void_ptr != (To *)other._void_ptr;
  240. }
  241. ////////////////////////////////////////////////////////////////////
  242. // Function: WeakPointerToBase::Greater-than operator
  243. // Access: Public
  244. // Description:
  245. ////////////////////////////////////////////////////////////////////
  246. template<class T>
  247. INLINE bool WeakPointerToBase<T>::
  248. operator > (const WeakPointerToBase<To> &other) const {
  249. return (To *)_void_ptr > (To *)other._void_ptr;
  250. }
  251. ////////////////////////////////////////////////////////////////////
  252. // Function: WeakPointerToBase::Less-than-or-equal operator
  253. // Access: Public
  254. // Description:
  255. ////////////////////////////////////////////////////////////////////
  256. template<class T>
  257. INLINE bool WeakPointerToBase<T>::
  258. operator <= (const WeakPointerToBase<To> &other) const {
  259. return (To *)_void_ptr <= (To *)other._void_ptr;
  260. }
  261. ////////////////////////////////////////////////////////////////////
  262. // Function: WeakPointerToBase::Greater-than-or-equal operator
  263. // Access: Public
  264. // Description:
  265. ////////////////////////////////////////////////////////////////////
  266. template<class T>
  267. INLINE bool WeakPointerToBase<T>::
  268. operator >= (const WeakPointerToBase<To> &other) const {
  269. return (To *)_void_ptr >= (To *)other._void_ptr;
  270. }
  271. ////////////////////////////////////////////////////////////////////
  272. // Function: WeakPointerToBase::Equivalence operator
  273. // Access: Public
  274. // Description:
  275. ////////////////////////////////////////////////////////////////////
  276. template<class T>
  277. INLINE bool WeakPointerToBase<T>::
  278. operator == (const PointerToBase<To> &other) const {
  279. return (To *)_void_ptr == (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  280. }
  281. ////////////////////////////////////////////////////////////////////
  282. // Function: WeakPointerToBase::Nonequivalence operator
  283. // Access: Public
  284. // Description:
  285. ////////////////////////////////////////////////////////////////////
  286. template<class T>
  287. INLINE bool WeakPointerToBase<T>::
  288. operator != (const PointerToBase<To> &other) const {
  289. return (To *)_void_ptr != (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  290. }
  291. ////////////////////////////////////////////////////////////////////
  292. // Function: WeakPointerToBase::Greater-than operator
  293. // Access: Public
  294. // Description:
  295. ////////////////////////////////////////////////////////////////////
  296. template<class T>
  297. INLINE bool WeakPointerToBase<T>::
  298. operator > (const PointerToBase<To> &other) const {
  299. return (To *)_void_ptr > (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  300. }
  301. ////////////////////////////////////////////////////////////////////
  302. // Function: WeakPointerToBase::Less-than-or-equal operator
  303. // Access: Public
  304. // Description:
  305. ////////////////////////////////////////////////////////////////////
  306. template<class T>
  307. INLINE bool WeakPointerToBase<T>::
  308. operator <= (const PointerToBase<To> &other) const {
  309. return (To *)_void_ptr <= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  310. }
  311. ////////////////////////////////////////////////////////////////////
  312. // Function: WeakPointerToBase::Greater-than-or-equal operator
  313. // Access: Public
  314. // Description:
  315. ////////////////////////////////////////////////////////////////////
  316. template<class T>
  317. INLINE bool WeakPointerToBase<T>::
  318. operator >= (const PointerToBase<To> &other) const {
  319. return (To *)_void_ptr >= (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  320. }
  321. #endif // WIN32_VC
  322. ////////////////////////////////////////////////////////////////////
  323. // Function: WeakPointerToBase::Less-than operator
  324. // Access: Public
  325. // Description:
  326. ////////////////////////////////////////////////////////////////////
  327. template<class T>
  328. INLINE bool WeakPointerToBase<T>::
  329. operator < (const To *other) const {
  330. return (To *)_void_ptr < other;
  331. }
  332. ////////////////////////////////////////////////////////////////////
  333. // Function: WeakPointerToBase::Less-than operator
  334. // Access: Public
  335. // Description:
  336. ////////////////////////////////////////////////////////////////////
  337. template<class T>
  338. INLINE bool WeakPointerToBase<T>::
  339. operator < (const WeakPointerToBase<To> &other) const {
  340. return (To *)_void_ptr < (To *)other._void_ptr;
  341. }
  342. ////////////////////////////////////////////////////////////////////
  343. // Function: WeakPointerToBase::Less-than operator
  344. // Access: Public
  345. // Description:
  346. ////////////////////////////////////////////////////////////////////
  347. template<class T>
  348. INLINE bool WeakPointerToBase<T>::
  349. operator < (const PointerToBase<To> &other) const {
  350. return (To *)_void_ptr < (To *)((WeakPointerToBase<To> *)&other)->_void_ptr;
  351. }
  352. #endif // CPPPARSER
  353. ////////////////////////////////////////////////////////////////////
  354. // Function: WeakPointerToBase::clear
  355. // Access: Published
  356. // Description: A convenient way to set the PointerTo object to NULL.
  357. // (Assignment to a NULL pointer also works, of course.)
  358. ////////////////////////////////////////////////////////////////////
  359. template<class T>
  360. INLINE void WeakPointerToBase<T>::
  361. clear() {
  362. reassign((To *)NULL);
  363. }
  364. ////////////////////////////////////////////////////////////////////
  365. // Function: WeakPointerToBase::refresh
  366. // Access: Published
  367. // Description: Informs the WeakPointerTo object that its pointer is
  368. // no longer deleted. This may be used after a
  369. // WeakPointerTo has deleted a deleted pointer, and then
  370. // a new pointer has been reallocated. It's equivalent
  371. // to simply reassigning the pointer to its new
  372. // (i.e. original) value, but has the advantage that it
  373. // is const, so can be used for WeakPointers used as
  374. // keys in STL maps and sets.
  375. ////////////////////////////////////////////////////////////////////
  376. template<class T>
  377. INLINE void WeakPointerToBase<T>::
  378. refresh() const {
  379. ((WeakPointerToBase<T> *)this)->reassign((To *)_void_ptr);
  380. }
  381. ////////////////////////////////////////////////////////////////////
  382. // Function: WeakPointerToBase::output
  383. // Access: Published
  384. // Description: A handy function to output PointerTo's as a hex
  385. // pointer followed by a reference count.
  386. ////////////////////////////////////////////////////////////////////
  387. template<class T>
  388. INLINE void WeakPointerToBase<T>::
  389. output(ostream &out) const {
  390. out << _void_ptr;
  391. if (was_deleted()) {
  392. out << ":deleted";
  393. } else if (_void_ptr != (void *)NULL) {
  394. out << ":" << ((To *)_void_ptr)->get_ref_count();
  395. }
  396. }