RefCount.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "PreCompiled.h"
  24. #include "Exception.h"
  25. #include "Log.h"
  26. #include "RefCount.h"
  27. RefCount::RefCount() :
  28. mRefs(0),
  29. mWeakRefs(0),
  30. mExpired(false)
  31. {
  32. }
  33. RefCounted::RefCounted() :
  34. mRefCount(new RefCount())
  35. {
  36. // Hold a weak ref to self to avoid possible double delete of the refcount
  37. ++(mRefCount->mWeakRefs);
  38. }
  39. RefCounted::~RefCounted()
  40. {
  41. // The errors here should be exceptions instead. However it is not a good practice to throw from destructors
  42. if (mRefCount)
  43. {
  44. #ifdef ENABLE_REFCOUNTED_CHECKS
  45. if (mRefCount->mRefs)
  46. LOGERROR("Destructor called when references still exist");
  47. if (!mRefCount->mWeakRefs)
  48. LOGERROR("Weak reference count mismatch on destruction");
  49. #endif
  50. // Mark object as expired, release the self weak ref and delete the refcount if no other weak refs exist
  51. mRefCount->mExpired = true;
  52. --(mRefCount->mWeakRefs);
  53. if (!mRefCount->mWeakRefs)
  54. delete mRefCount;
  55. mRefCount = 0;
  56. }
  57. else
  58. LOGERROR("Destructor called multiple times");
  59. }
  60. void RefCounted::addRef()
  61. {
  62. ++(mRefCount->mRefs);
  63. }
  64. void RefCounted::releaseRef()
  65. {
  66. #ifdef ENABLE_REFCOUNTED_CHECKS
  67. if (!mRefCount->mRefs)
  68. EXCEPTION("releaseRef() called when object already destroyed");
  69. #endif
  70. --(mRefCount->mRefs);
  71. if (!mRefCount->mRefs)
  72. delete this;
  73. }
  74. unsigned RefCounted::getRefCount() const
  75. {
  76. return mRefCount->mRefs;
  77. }
  78. unsigned RefCounted::getWeakRefCount() const
  79. {
  80. // Subtract one to not return the internally held reference
  81. return mRefCount->mWeakRefs - 1;
  82. }