pointerToBase.I 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Filename: pointerToBase.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: PointerToBase::Constructor
  20. // Access: Protected
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. template<class T>
  24. INLINE PointerToBase<T>::
  25. PointerToBase(To *ptr) {
  26. reassign(ptr);
  27. }
  28. ////////////////////////////////////////////////////////////////////
  29. // Function: PointerToBase::Copy Constructor
  30. // Access: Protected
  31. // Description:
  32. ////////////////////////////////////////////////////////////////////
  33. template<class T>
  34. INLINE PointerToBase<T>::
  35. PointerToBase(const PointerToBase<T> &copy) {
  36. reassign(copy);
  37. }
  38. ////////////////////////////////////////////////////////////////////
  39. // Function: PointerToBase::Destructor
  40. // Access: Protected
  41. // Description:
  42. ////////////////////////////////////////////////////////////////////
  43. template<class T>
  44. INLINE PointerToBase<T>::
  45. ~PointerToBase() {
  46. reassign((To *)NULL);
  47. }
  48. ////////////////////////////////////////////////////////////////////
  49. // Function: PointerToBase::reassign
  50. // Access: Protected
  51. // Description: This is the main work of the PointerTo family. When
  52. // the pointer is reassigned, decrement the old
  53. // reference count and increment the new one.
  54. ////////////////////////////////////////////////////////////////////
  55. template<class T>
  56. void PointerToBase<T>::
  57. reassign(To *ptr) {
  58. if (ptr != (To *)_void_ptr) {
  59. // First save the old pointer; we won't delete it until we have
  60. // assigned the new one. We do this just in case there are
  61. // cascading effects from deleting this pointer that might
  62. // inadvertently delete the new one. (Don't laugh--it's
  63. // happened!)
  64. To *old_ptr = (To *)_void_ptr;
  65. _void_ptr = (void *)ptr;
  66. if (ptr != (To *)NULL) {
  67. ptr->ref();
  68. #ifdef DO_MEMORY_USAGE
  69. if (MemoryUsage::get_track_memory_usage()) {
  70. // Make sure the MemoryUsage record knows what the TypeHandle
  71. // is, if we know it ourselves.
  72. TypeHandle type = get_type_handle(To);
  73. if (type == TypeHandle::none()) {
  74. do_init_type(To);
  75. type = get_type_handle(To);
  76. }
  77. if (type != TypeHandle::none()) {
  78. MemoryUsage::update_type(ptr, type);
  79. }
  80. }
  81. #endif
  82. }
  83. // Now delete the old pointer.
  84. if (old_ptr != (To *)NULL) {
  85. unref_delete(old_ptr);
  86. }
  87. }
  88. }
  89. ////////////////////////////////////////////////////////////////////
  90. // Function: PointerToBase::reassign
  91. // Access: Protected
  92. // Description:
  93. ////////////////////////////////////////////////////////////////////
  94. template<class T>
  95. INLINE void PointerToBase<T>::
  96. reassign(const PointerToBase<To> &copy) {
  97. reassign((To *)copy._void_ptr);
  98. }
  99. ////////////////////////////////////////////////////////////////////
  100. // Function: PointerToBase::clear
  101. // Access: Published
  102. // Description: A convenient way to set the PointerTo object to NULL.
  103. // (Assignment to a NULL pointer also works, of course.)
  104. ////////////////////////////////////////////////////////////////////
  105. template<class T>
  106. INLINE void PointerToBase<T>::
  107. clear() {
  108. reassign((To *)NULL);
  109. }
  110. ////////////////////////////////////////////////////////////////////
  111. // Function: PointerToBase::output
  112. // Access: Published
  113. // Description: A handy function to output PointerTo's as a hex
  114. // pointer followed by a reference count.
  115. ////////////////////////////////////////////////////////////////////
  116. template<class T>
  117. INLINE void PointerToBase<T>::
  118. output(ostream &out) const {
  119. out << _void_ptr;
  120. if (_void_ptr != (void *)NULL) {
  121. out << ":" << ((To *)_void_ptr)->get_ref_count();
  122. }
  123. }