p3dReferenceCount.I 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Filename: p3dReferenceCount.I
  2. // Created by: drose (09Jul09)
  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. // Function: P3DReferenceCount::Constructor
  16. // Access: Public
  17. // Description:
  18. ////////////////////////////////////////////////////////////////////
  19. inline P3DReferenceCount::
  20. P3DReferenceCount() {
  21. _ref_count = 0;
  22. }
  23. ////////////////////////////////////////////////////////////////////
  24. // Function: P3DReferenceCount::Destructor
  25. // Access: Public
  26. // Description:
  27. ////////////////////////////////////////////////////////////////////
  28. inline P3DReferenceCount::
  29. ~P3DReferenceCount() {
  30. assert(_ref_count == 0);
  31. }
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: P3DReferenceCount::ref
  34. // Access: Public
  35. // Description: Explicitly increments the reference count.
  36. ////////////////////////////////////////////////////////////////////
  37. inline void P3DReferenceCount::
  38. ref() const {
  39. ++((P3DReferenceCount *)this)->_ref_count;
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: P3DReferenceCount::unref
  43. // Access: Public
  44. // Description: Explicitly decrements the reference count. Usually,
  45. // you should call p3d_unref_delete() instead.
  46. //
  47. // The return value is true if the new reference count
  48. // is nonzero, false if it is zero.
  49. ////////////////////////////////////////////////////////////////////
  50. inline bool P3DReferenceCount::
  51. unref() const {
  52. return --(((P3DReferenceCount *)this)->_ref_count) != 0;
  53. }
  54. ////////////////////////////////////////////////////////////////////
  55. // Function: P3DReferenceCount::get_ref_count
  56. // Access: Public
  57. // Description: Returns the current reference count.
  58. ////////////////////////////////////////////////////////////////////
  59. inline int P3DReferenceCount::
  60. get_ref_count() const {
  61. return _ref_count;
  62. }
  63. ////////////////////////////////////////////////////////////////////
  64. // Function: p3d_unref_delete
  65. // Description: This global helper function will unref the given
  66. // P3DReferenceCount object, and if the reference count
  67. // reaches zero, automatically delete it.
  68. ////////////////////////////////////////////////////////////////////
  69. template<class RefCountType>
  70. inline void
  71. p3d_unref_delete(RefCountType *ptr) {
  72. if (!ptr->unref()) {
  73. delete ptr;
  74. }
  75. }